Cron job not executing script properly
-
I have 3 scripts that I want to run at different times. Logged in as root, I setup a cronjob with:
crontab -e
The contents of the crontab file is:
0 7 * * 6 /opt/scripts/script_1.sh >/dev/null 2>&1 0 0 * * * /opt/scripts/script_2.sh >/dev/null 2>&1 0 6 * * * /opt/scripts/script_3.sh >/dev/null 2>&1
Permissions on all script files are:
-rwxr-xr-x 1 root root
Each script runs when running it manually.
The scripts are not running. On another system, I run only script 1 via cron, and it works perfectly. However on this server, the only difference (at least with the crontab file) is that I have multiple entries.
Is there anything obvious that anyone sees that could be an issue?
-
Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like
#!/bin/bash export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
In script itself.
-
@triple9 said in Cron job not executing script properly:
Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like
#!/bin/bash export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
In script itself.
Yes, I have #!/bin/bash in each script. I will put in:
export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
and see what happens. Thanks
-
@triple9 said in Cron job not executing script properly:
Do you have #!/bin/bash in the scripts? Other thing that could cause problem is missing PATH variable. Put something like
#!/bin/bash export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
In script itself.
Well that did the trick. Thanks!
-
You're welcome.
-
@triple9 Good job getting @fuznutz04 sorted.
Cron runs in it's own user-space. Which has very little already defined.... like PATH.
If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)
-
@travisdh1 said in Cron job not executing script properly:
@triple9 Good job getting @fuznutz04 sorted.
Cron runs in it's own user-space. Which has very little already defined.... like PATH.
If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)
Well that's great to know. So is it safe to assume for every script that I want to run with cron, should have this defined in each script?
#!/bin/bash export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
-
@fuznutz04 said in Cron job not executing script properly:
@travisdh1 said in Cron job not executing script properly:
@triple9 Good job getting @fuznutz04 sorted.
Cron runs in it's own user-space. Which has very little already defined.... like PATH.
If a script runs fine when run manually but fails in cron, it's almost always a missing environment variable (PATH being the most common.)
Well that's great to know. So is it safe to assume for every script that I want to run with cron, should have this defined in each script?
#!/bin/bash export PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin
It's very likely, yes.
-
@travisdh1 Ok, thanks for the explanation.