bash says "no such file or directory" when crontab runs the shell script
Hey Guys,
I have a problem that I need your help. - I created a shell script like this (take_backup): #!/bin/bash mysqldump -u dbuser -pdbpass dbname > dbname_backup.sql - Also I created a php file (backup.php) to execute the shell script <?php exec("bash take_backup"); ?> - My crontab looks like this: MAILTO=myemail@mydomain.com */5 * * * * php /path/to/backup.php - The shell script runs perfectly ok when I run the backup.php file from browser which uses the "take_backup" shell script behind. But my problem is I want to run the php file from the crontab and it doesn't get executed. Infact it emails me back with "bash: take_backup: No such file or directory" FYI: - "which bash" returns: /bin/bash - I noticed that crontab is just not executing bash command in the backup.php file, other commands are working, ex: exec("ls -l"); Any reason??? Please help!!!! Thanks Yamin |
Hi, welcome to LQ!
Simply specify the full path to take_backup in the PHP script. Cheers, Tink |
Hi,
did you try putting the full path to "take_backup" in your php script? Eg. Code:
<?php Code:
*/5 * * * * /path/to/take_backup |
Expanding on good advice above; the cron env is minimal, which means you should specify the complete PATH to all cmds/files used, or source a file that defines them.
Also, forget the php in cron, just call the script as per evo2's suggestion. |
Quote:
Configured crontab with full path of the shell script (according to your suggestion). But with no luck. I've tried followings in the crontab: A. MAILTO=myemail@mydomain.com */5 * * * * bash /path/to/take_backup Output of A: I didn't receive any Cron Daemon email B. MAILTO=myemail@mydomain.com */5 * * * * /path/to/take_backup Output of B: I didn't receive any Cron Daemon email C. MAILTO=myemail@mydomain.com */5 * * * * ./path/to/take_backup Output of C: /bin/sh: ./path/to/take_backup: No such file or directory (in the Cron Daemon email) Am I missing something? Any help would be very much appreciated... Thanks Yamin |
What about crons log?
|
Quote:
[TRUNCATED BEFORE...] Mar 22 10:00:01 www crond[1543]: (root) RELOAD (/var/spool/cron/root) Mar 22 10:00:01 www CROND[6801]: (root) CMD (./path/to/take_backup) Mar 22 10:00:01 www CROND[6802]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 10:01:01 www CROND[6825]: (root) CMD (run-parts /etc/cron.hourly) Mar 22 10:01:01 www CROND[6826]: (root) CMD (run-parts /etc/cron.hourly) Mar 22 10:01:01 www CROND[6827]: (root) CMD (./path/to/take_backup) Mar 22 10:01:01 www run-parts(/etc/cron.hourly)[6825]: starting 0anacron Mar 22 10:01:01 www run-parts(/etc/cron.hourly)[6826]: starting 0anacron Mar 22 10:01:01 www anacron[6850]: Anacron started on 2012-03-22 Mar 22 10:01:01 www anacron[6850]: Normal exit (0 jobs run) Mar 22 10:01:01 www anacron[6852]: Anacron started on 2012-03-22 Mar 22 10:01:01 www anacron[6852]: Normal exit (0 jobs run) Mar 22 10:01:01 www run-parts(/etc/cron.hourly)[6855]: finished 0anacron Mar 22 10:01:01 www run-parts(/etc/cron.hourly)[6856]: finished 0anacron Mar 22 10:01:30 www crontab[6864]: (root) BEGIN EDIT (root) Mar 22 10:01:36 www crontab[6864]: (root) REPLACE (root) Mar 22 10:01:36 www crontab[6864]: (root) END EDIT (root) Mar 22 10:02:01 www crond[1543]: (root) RELOAD (/var/spool/cron/root) Mar 22 10:06:29 www crontab[6934]: (root) BEGIN EDIT (root) Mar 22 10:06:35 www crontab[6934]: (root) END EDIT (root) Mar 22 10:10:01 www CROND[6986]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 10:20:01 www CROND[7126]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 10:30:01 www CROND[7241]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 10:40:01 www CROND[7260]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 10:50:01 www CROND[7280]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 11:00:01 www CROND[7319]: (root) CMD (/usr/lib64/sa/sa1 -S DISK 1 1) Mar 22 11:01:01 www CROND[7329]: (root) CMD (run-parts /etc/cron.hourly) Mar 22 11:01:01 www CROND[7330]: (root) CMD (run-parts /etc/cron.hourly) Mar 22 11:01:01 www run-parts(/etc/cron.hourly)[7330]: starting 0anacron Mar 22 11:01:01 www run-parts(/etc/cron.hourly)[7329]: starting 0anacron Mar 22 11:01:01 www anacron[7352]: Anacron started on 2012-03-22 Mar 22 11:01:01 www anacron[7354]: Anacron started on 2012-03-22 Mar 22 11:01:01 www anacron[7354]: Normal exit (0 jobs run) Mar 22 11:01:01 www run-parts(/etc/cron.hourly)[7356]: finished 0anacron Mar 22 11:01:01 www run-parts(/etc/cron.hourly)[7357]: finished 0anacron Mar 22 11:01:01 www anacron[7352]: Normal exit (0 jobs run) Seems like 0anacron is running as well. But don't think 0anacron is conflicting mine, as my crontab frequency is just about every five minutes. What do you think? Also, my OS is: CentOS Linux release 6.0 (Final) So I checked the cron log's default file location: /var/log and did this: vi /var/log/cron (which is the latest) Thanks Yamin |
Just for good measure also check
grep -ril cron /var/log/ I'd expect to see a line for your user, somewhere. Can you match the attempts that had errors w/ any cron times in the system logs? |
Quote:
/var/log/cron /var/log/cron-20120226 /var/log/cron-20120311 /var/log/audit/audit.log /var/log/btmp-20120301 /var/log/cron-20120318 /var/log/anaconda.yum.log /var/log/anaconda.log /var/log/cron-20120304 /var/log/secure /var/log/vsftpd.log Quote:
|
Hi,
did you run the mysqlbackup from "take_backup" with full path too ? Code:
for example: |
Quote:
/var/lib/mysqldump -u username -ppassword database > database_backup.sql You know what. Funny thing is if I just run the php script via browser with this following shell command: <?php exec("bash /path/to/take_backup"); ?> then the database backup does gets created. I just dont understand why would the same php script or direct invoking of the shell script (take_backup) wont work from crontab. :(( |
I think we need to start from the beginning; try
Code:
*/5 * * * * echo $(date) > /tmp/cron_test.log Code:
mailx Code:
/etc/cron.deny |
Quote:
Remember that if cron has an issue, it will email the owner and/or root acct, which you can check by logging in as the job owner and as root and using Code:
mailx MAILTO=myemail@mydomain.com */5 * * * * echo $(date) > /tmp/cron_test.log Can we also see the contents of Code:
/etc/cron.deny /etc/cron.deny file exist but nothing in it. /etc/cron.allow file do not exist. FYI, crontab works perfectly for me (Ex: I tested one of my hourly email reminder program with cron), it just cant run with any shell script. |
OK; next check where bash is
Code:
which bash Code:
#!/bin/bash Code:
chmod a+x mytest.sh Code:
#grab curr dir path info Code:
*/5 * * * * <put pwd output here>/mytest.sh > /tmp/cron_test.log 2>&1 |
Quote:
- it gives /bin/bash for my system too Quote:
- I used vi to create mytest.sh and pasted your code above Quote:
Quote:
- Output of the /tmp/cron_test.log file: Thu Mar 22 17:57:01 PDT 2012 |
All times are GMT -5. The time now is 08:37 AM. |