Solved contab SHELL variable question

Hi,

I have a frew script that run automatically via crontab.
So far all my script was written in sh shell but I now have a bash script and I get the following error:
Code:
/bin/sh: root: not found
crontab -l
Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

#
#minute (0-59)
#|   hour (0-23)
#|   |    day of the month (1-31)
#|   |    |   month of the year (1-12 or Jan-Dec)
#|   |    |   |   day of the week (0-6 with 0=Sun or Sun-Sat)
#|   |    |   |   |   commands
#|   |    |   |   |   |

# run the backup scripts at 4:30am
30   4    *   *   *   /bin/sh /root/myScripts/jailsync.sh  >> /var/log/myScripts/jailsync.sh 2>&1

# Automated ZFS backups (hourly):
0    *    *   *   *   root /root/myScripts/zfs-snapshot.sh zroot hourly 25
I figure that the issue here is with the very first line. Is it possible ti have both
SHELL=/bin/sh
SHELL=/usr/local/bin/bash
like this?
SHELL=/bin/sh:/usr/local/bin/bash/usr/local/bin/bash
 
Is it possible ti have both
No:
Code:
#!/bin/sh

MYVAR="foo"
MYVAR="bar"

echo ${MYVAR}
Guess what the script outputs?

like this?
SHELL=/bin/sh:/usr/local/bin/bash/usr/local/bin/bash
That only works for path type variables (like PATH or LD_PRELOAD) because they are processed (split up using ':' as a delimiter).

Try it and you'll know it doesn't work:
env MYVAR=/bin/sh:/usr/local/bin/bash exec $MYVAR

But that SHELL variable there is not the problem. You just need to use the correct shebang line in your bash(1) script:
Code:
#!/usr/local/bin/bash

{...}

But the error you're getting is because you're confusing the syntax of the system's /etc/crontab and a user's crontab -e (root is just a user).
The line should read:
Code:
# Automated ZFS backups (hourly):
0    *    *   *   *   /root/myScripts/zfs-snapshot.sh zroot hourly 25
Note the absence of the user account (root in this case).
 
I think the problem in this case is here:

30 4 * * * /bin/sh /root/myScripts/jailsync.sh >> /var/log/myScripts/jailsync.sh 2>&1

should be:

30 4 * * * root /root/myScripts/jailsync.sh >> /var/log/myScripts/jailsync.sh 2>&1
 
No, gkontos, you have that backwards. :) The error message is saying cron can't find the "root" binary to run, which means this is a user crontab, and the syntax is wrong. Removing the "root" from the command line will make it work (as listed by SirDice above).
 
If I am not mistaken, the syntax for /etc/crontab goes like this: when, who, what

SirDice suggested not to use /etc/crontab which is correct since it might get overwritten in a future update.
 
Thank you all
I used crontab -e and not /etc/crontab as your suggestion.
I updated the path with the bash binary and removed the 'root' from the command.
All good now.
Code:
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin/bash
# Automated ZFS backups (hourly):
0    *    *   *   *   /usr/local/bin/bash /root/myScripts/zfs-snapshot.sh zroot hourly 25
 
Back
Top