(Solved) bash script from inside /etc/crontab

Issues related to applications and software problems
Post Reply
lightman47
Posts: 851
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

(Solved) bash script from inside /etc/crontab

Post by lightman47 » 2019/01/13 20:55:01

I have a lot of scripts that are called from /etc/crontab. All work great except a recent one. I'm going to keep this very brief, but all machines run updated CentOS 7.

I have a server that is responsible for waking up a machine (on different days) to run backups, updates, and other chores. This is done via 'ether-wake' statements in the server /etc/crontab and works quite well that way. Because I often wish to wake a machine manually to work on it and not have to remember & type the MAC each time, I wrote a single bash script named wakeup.sh that takes a {machine name} parameter, assigns the appropriate MAC to a variable which is then used with the ether-wake command. The script works beautifully ... well, until I tried to run it from inside /etc/crontab. The script runs fine and without error (verified /var/log/cron), but the appropriate machine doesn't wake up. So, I then included an "echo" in my script

Code: Select all

echo
sudo ether-wake $MAC
echo
echo "Sent wake signal to $machineName at $MAC."
echo
In /etc/crontab (for testing - not shown here), I redirected the script output to a text file. At the appropriate time in crontab (run as root), the text file was created and contained the proper machine name and MAC address.

Question is:
WHY, from /etc/crontab, does it all seem to not work. The very same crontab command pasted to the command line DOES wake the machine!
- crontab entries (with modified MACs and machine names) with my failing/testing entries commented:

Code: Select all

# wake-up videoBox for Tues and Sat backup(s)
20 18 * * mon root ether-wake 0a:1b:21:05:f1:17
20 18 * * fri root ether-wake 0a:1b:21:05:f1:17
## 20 18 * * mon root bash /scripts/wakeup.sh videoBox >> /mnt/4tb1/logs/wakeup.log
## 20 18 * * fri root bash /scripts/wakeup.sh videoBox >> /mnt/4tb1/logs/wakeup.log
## TESTING ENTRY
## 35 13 * * * root bash /scripts/wakeup.sh videoBox >> /mnt/4tb1/logs/wakeup.log
What am I missing here? I even tried "quotes" in the crontab.

Code: Select all

20 18 * * mon root bash "/scripts/wakeup.sh videoBox"
I feel like something very basic (stupid) is going on here.

Thank you.
Last edited by lightman47 on 2019/01/15 12:29:40, edited 1 time in total.

User avatar
TrevorH
Forum Moderator
Posts: 24804
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: bash script from inside /etc/crontab

Post by TrevorH » 2019/01/13 21:04:07

Cron runs things with a very restrictive environment set up. For example, if you cron /usr/bin/env > /tmp/env.txt and then read it once it's run, you'll see that PATH is set to

PATH=/usr/bin:/bin

ether-wake is in /sbin which isn't there. If you intend to run stuff from cron it's good practice to always include the full path to anything you intend to execute.
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

lightman47
Posts: 851
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: bash script from inside /etc/crontab

Post by lightman47 » 2019/01/13 21:08:01

Wow - thank you! Will fiddle with this.

EDIT 2019 01 15 -

That was it. Thank you!

Post Reply