LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2012, 07:16 PM   #1
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Rep: Reputation: 0
Angry 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
 
Old 03-21-2012, 07:34 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Hi, welcome to LQ!

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


Cheers,
Tink
 
Old 03-21-2012, 07:35 PM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 03-21-2012, 07:40 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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.
 
Old 03-22-2012, 12:06 PM   #5
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by evo2 View Post
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
 
Old 03-22-2012, 12:56 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What about crons log?
 
Old 03-22-2012, 01:15 PM   #7
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by Tinkster View Post
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
 
Old 03-22-2012, 01:28 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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?
 
Old 03-22-2012, 01:41 PM   #9
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by Tinkster View Post
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 View Post
- 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?
 
Old 03-22-2012, 02:54 PM   #10
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

did you run the mysqlbackup from "take_backup" with full path too ?
Code:
for example:
/sbin/mysqldump ....
 
Old 03-22-2012, 03:58 PM   #11
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by lithos View Post
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. (
 
Old 03-22-2012, 06:45 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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.
 
Old 03-22-2012, 07:30 PM   #13
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by chrism01 View Post
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.

Last edited by ymnoor21; 03-22-2012 at 07:35 PM. Reason: Spelling
 
Old 03-22-2012, 07:37 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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
 
Old 03-22-2012, 08:02 PM   #15
ymnoor21
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by chrism01 View Post
OK; next check where bash is
Code:
which bash

- it gives /bin/bash for my system too


Quote:
Originally Posted by chrism01 View Post
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 View Post
Code:
chmod a+x mytest.sh
- did the permission settings.

Quote:
Originally Posted by chrism01 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find in bash script returns "missing argument to `-exec'" while cmd runs fine zhjim Programming 11 01-31-2012 08:56 AM
[SOLVED] Bash script problem "No such file or directory" cnmoore Programming 23 03-31-2011 03:55 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
[SOLVED] "!#/bin/bash: No such file or directory"? When running a script C_Blade Linux - Newbie 12 04-08-2010 10:15 PM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration