Solved contab SHELL variable question

fred974

Daemon

Reaction score: 36
Messages: 1,584

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
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,672
Messages: 30,574

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

gkontos

Daemon

Reaction score: 480
Messages: 2,153

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
 

phoenix

Administrator
Staff member
Administrator
Moderator

Reaction score: 1,259
Messages: 4,099

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

gkontos

Daemon

Reaction score: 480
Messages: 2,153

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.
 
OP
OP
fred974

fred974

Daemon

Reaction score: 36
Messages: 1,584

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
 
Top