LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash says "no such file or directory" when crontab runs the shell script (http://www.linuxquestions.org/questions/linux-newbie-8/bash-says-no-such-file-or-directory-when-crontab-runs-the-shell-script-935780/)

ymnoor21 03-21-2012 08:16 PM

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

Tinkster 03-21-2012 08:34 PM

Hi, welcome to LQ!

Simply specify the full path to take_backup in the PHP script.


Cheers,
Tink

evo2 03-21-2012 08:35 PM

Hi,

did you try putting the full path to "take_backup" in your php script?
Eg.

Code:

<?php
exec("bash /path/to/take_backup");
?>

Also, why did you wrap your shell script in a php script? Wouldn't it make more sense to have a cron job like:

Code:

*/5 * * * * /path/to/take_backup
Evo2.

chrism01 03-21-2012 08:40 PM

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.

ymnoor21 03-22-2012 01:06 PM

Quote:

Originally Posted by evo2 (Post 4633090)
Hi,

did you try putting the full path to "take_backup" in your php script?
Eg.

Code:

<?php
exec("bash /path/to/take_backup");
?>

Also, why did you wrap your shell script in a php script? Wouldn't it make more sense to have a cron job like:

Code:

*/5 * * * * /path/to/take_backup
Evo2.

Uhu, nothing is working. I've tried this:

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

Tinkster 03-22-2012 01:56 PM

What about crons log?

ymnoor21 03-22-2012 02:15 PM

Quote:

Originally Posted by Tinkster (Post 4633777)
What about crons log?

CRON LOG OUTPUT:
[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

Tinkster 03-22-2012 02:28 PM

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?

ymnoor21 03-22-2012 02:41 PM

Quote:

Originally Posted by Tinkster (Post 4633804)
Just for good measure also check
grep -ril cron /var/log/


/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:

Originally Posted by Tinkster (Post 4633804)
- 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?

- What do you mean?

lithos 03-22-2012 03:54 PM

Hi,

did you run the mysqlbackup from "take_backup" with full path too ?
Code:

for example:
/sbin/mysqldump ....


ymnoor21 03-22-2012 04:58 PM

Quote:

Originally Posted by lithos (Post 4633876)
Hi,

did you run the mysqlbackup from "take_backup" with full path too ?
Code:

for example:
/sbin/mysqldump ....


Yes I did try this too:
/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. :((

chrism01 03-22-2012 07:45 PM

I think we need to start from the beginning; try
Code:

*/5 * * * * echo $(date) > /tmp/cron_test.log
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
Can we also see the contents of
Code:

/etc/cron.deny
/etc/cron.allow

if they exist.

ymnoor21 03-22-2012 08:30 PM

Quote:

Originally Posted by chrism01 (Post 4634011)
I think we need to start from the beginning; try
Code:

*/5 * * * * echo $(date) > /tmp/cron_test.log

Thu Mar 22 17:22:01 PDT 2012

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
Right now, I have redirected it to email me in the crontab:
MAILTO=myemail@mydomain.com
*/5 * * * * echo $(date) > /tmp/cron_test.log


Can we also see the contents of
Code:

/etc/cron.deny
/etc/cron.allow

if they exist.

/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.

chrism01 03-22-2012 08:37 PM

OK; next check where bash is
Code:

which bash
gives me '/bin/bash' on my system, so I'd construct a shell file (using a Linux editor such as vi/vim) called mytest.sh
Code:

#!/bin/bash
echo "in shell file"
echo $(date) > /tmp/cron_test.log

Code:

chmod a+x mytest.sh
Code:

#grab curr dir path info
pwd

and put that in cron
Code:

*/5 * * * * <put pwd output here>/mytest.sh > /tmp/cron_test.log 2>&1

ymnoor21 03-22-2012 09:02 PM

Quote:

Originally Posted by chrism01 (Post 4634048)
OK; next check where bash is
Code:

which bash


- it gives /bin/bash for my system too


Quote:

Originally Posted by chrism01 (Post 4634048)
gives me '/bin/bash' on my system, so I'd construct a shell file (using a Linux editor such as vi/vim) called mytest.sh
Code:

#!/bin/bash
echo "in shell file"
echo $(date) > /tmp/cron_test.log



- I used vi to create mytest.sh and pasted your code above


Quote:

Originally Posted by chrism01 (Post 4634048)
Code:

chmod a+x mytest.sh

- did the permission settings.

Quote:

Originally Posted by chrism01 (Post 4634048)
Code:

#grab curr dir path info
pwd

and put that in cron
Code:

*/5 * * * * <put pwd output here>/mytest.sh > /tmp/cron_test.log 2>&1

- did that in my crontab

- 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 11:29 AM.