LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   crontab script execution - different behaviour user-cronfile <-> system-wide cronfile (http://www.linuxquestions.org/questions/linux-software-2/crontab-script-execution-different-behaviour-user-cronfile-system-wide-cronfile-791707/)

resigned 02-26-2010 05:39 AM

crontab script execution - different behaviour user-cronfile <-> system-wide cronfile
 
Hello everybody.

I stumbled upon the following:

Script iptables_reset.sh

Code:

#!/bin/bash

IPTABLES="$(which iptables)"

$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F
$IPTABLES -X

exit 0

This works if I edit the system-wide crontab in /etc/crontab like this:

Code:

0 12 * * * root /root/iptables/iptables_reset.sh
But it won't work, if i edit roots user-cronfile by using crontab(1):
Code:

crontab -l
# m h  dom mon dow  command
0 12 * * * /root/iptables/iptables_reset.sh

The log entry is the same for system-wide and user-cronfile:
Code:

/USR/SBIN/CRON[5135]: (root) CMD (/root/iptables/iptables_reset.sh)
I tried different things and found out, if i don't use the "$(which iptables)" substitution in the script, it also works with the user-cronfile.
What i can't figure out is why. Maybe someone can enlighten me?!

Berhanie 02-26-2010 06:01 PM

maybe the user cron doesn't have a correct PATH.
if so, you just have to define PATH somewhere (the script or the crontab).

chrism01 02-28-2010 11:24 PM

Non-root user wouldn't have that in their path...

Berhanie 03-01-2010 10:35 AM

the user is root ;).

chrism01 03-01-2010 08:55 PM

Really, the main point is that the default env settings for cron are minimal; this sort of issue, even for quite innocuous cmds/files is common.
The general rule is (search LQ) to always use full paths to all cmds/files required if running under cron.
Other options include sourcing the relevant user's .bash_profile/.bashrc files or set the cron env vars: http://adminschoice.com/crontab-quic...Crontab%20file


All times are GMT -5. The time now is 07:12 PM.