简体   繁体   中英

Why my shell script not working with cron?

I have two shell scripts .

( working one )

$ cat script_nas.sh

#!/bin/bash
for i in `cat nas_filers`
do echo $i
  touch /mnt/config-backup/nas_backup/$i.auditlog.0.$(date '+%Y%m%d')
  ssh -o ConnectTimeout=5 root@$i rdfile /etc/configs/config_saved > /mnt/config-backup/nas_backup/$i.auditlog.0.$(date '+%Y%m%d')
done

other

( not working one )

$ cat script_san.sh

#!/bin/bash
for i in `cat san_filers`
do echo $i
  touch /mnt/config-backup/san_backup/$i.auditlog.0.$(date '+%Y%m%d')
  ssh -o ConnectTimeout=5 root@$i rdfile /etc/configs/config_saved > /mnt/config-backup/san_backup/$i.auditlog.0.$(date '+%Y%m%d')
done

Cron entries are:

$ crontab -l
Filers config save script
0 0 * * * /mnt/config-backup/script_san.sh
0 0 * * * /mnt/config-backup/script_nas.sh
0 0 * * * /mnt/config-backup/delete_file

Script script_san.sh is not working. Outputs are like SAN backup directory

san_backup]# ls -lart alln01-na-exch01a.cisco.com.auditlog*
-rw-r--r-- 1 root root 210083 Mar  1 22:24 alln01-na-exch01a.auditlog.0.20150301
[root@XXXXX san_backup]# pwd
/mnt/config-backup/san_backup

NAS backup directory

nas_backup]# ls -lart rcdn9-25f-filer43b.cisco.com.auditlog*
-rw-r--r-- 1 root root 278730 Feb 26 00:06 rcdn9-25f-filer43b.cisco.com.auditlog.0.20150226
-rw-r--r-- 1 root root 281612 Feb 27 00:17 rcdn9-25f-filer43b.cisco.com.auditlog.0.20150227
-rw-r--r-- 1 root root 284105 Feb 28 00:02 rcdn9-25f-filer43b.cisco.com.auditlog.0.20150228
-rw-r--r-- 1 root root 284101 Mar  1 00:02 rcdn9-25f-filer43b.cisco.com.auditlog.0.20150301
[root@XXXXXXX nas_backup]#

From cron logs I can see that cron is executing both the script but output for script_san.sh is not coming.

From my experience, most of the times script is working manually but not from crontab is because login scripts were not running. Try to add something like source ~/.bash_profile in the begging of script or first line in cron file. Did you try (for debugging) to run the script with at command?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM