Running Script in Crontab--Reboot: Command Not Found

Running script in crontab--reboot: command not found

cron jobs run with a very basic environment setup; among other things, the default PATH is just /usr/bin:/bin. It does not use the user's regular shell setup. There are several ways to solve this:

  • Use the full path in the script (i.e. /sbin/reboot).
  • Set PATH in the script before using reboot (i.e. PATH=/usr/bin:/bin:/usr/sbin:/sbin).
  • Set PATH in the crontab before the entry for your script (syntax is the same as in the script).

crontab @reboot does not execute bash script when server is rebooted

My problem was that the crontab did not have a full environment. I made the script it was pointing to source my .bashrc.

@reboot /home/user/www/example.com/bin/server

./server does . /home/user/.bashrc to get a working environment.

@reboot is not working in CRON

Take a look at the systemd.service manpage. It describes how to configure systemd to manage a service. I am sure you will find examples for your system in /usr/lib/systemd/system or similar paths.

In your case, the service would look somewhat like this:

[Unit]
Description=Unturned Game Server

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/bin/bash /home/steam/start.sh
Type=simple
User=steam
Group=steam
WorkingDirectory=/home/steam
Restart=on-failure

Put this in a file /etc/systemd/system/unturned.service. Then run systemctl daemon-reload (once, and whenever you change unturned.service to tell systemd to re-read the configuration) and systemctl start unturned.service to start the game server.

If that works as expected, you can use systemctl enable unturned.service to make sure it starts at boot.

A few notes on the options used:

  • If start.sh is not supposed to run as user/group steam, edit appropriately.
  • WantedBy in the Install section tells systemd which "target" (see man systemd.target) pulls the service in when you enable it using systemctl enable.
  • Restart defines under which circumstances systemd will automatically restart the service. There are more restart-related options, which you may or may not want to change; see the systemd.service man page.

Why 'reboot' operation does not work with crontab?

So I started having this issue as well, recently too.

I've been doing a little digging, and so far what I have found is the following:

  • Placing 'reboot' in a SU crontab does nothing, but placing '/sbin/reboot' does successfully reboot the system
  • This is untrue for a User crontab, neither 'reboot' nor '/sbin/reboot' functions.

So this is a temporary fix that can get your system working for now, but I'm going to keep digging.

EDIT:
There's something more going on here, it doesn't seem to just be a su related problem. I passed my password plaintext to 'sudo systemctl reboot' and it didn't fire.

Cron jobs run ok as script but not @reboot

Fixed: The django and streaming screens where relying on some environment variables which are not loaded with cron. See here where-can-i-set-environment-variables-that-crontab-will-use for more details.

I was able to get logging working for the screens by following this gnu-screen-logtstamp-string and this save-screen-program-output-to-a-file .

My updated /home/myapp/reboot.sh:

#!/bin/bash
SHELL=/bin/bash

echo "$(date) Starting reboot.sh"

# Load environment variables
source /etc/profile.d/env.sh

start_django () {
echo "$(date) Entering start_django"
screen -c /home/myapp/django_cron_log.conf -L -S django -dm bash -c 'cd /home/myapp && /usr/local/bin/gunicorn myapp.wsgi:application --bind 0.0.0.0:8000'
echo "$(date) Exiting start_django"
}

start_stream () {
echo "$(date) Entering start_stream"
screen -c /home/myapp/stream_cron_log.conf -L -S streaming -dm bash -c 'cd /home/myapp/data_api && python3 api_stream_client.py && exec bash'
echo "$(date) Exiting start_stream"
}

if screen -list | grep -q "No Sockets found"; then
echo "$(date) No screens available"

echo "$(date) Starting django"
start_django

echo "$(date) Starting stream"
start_stream
else
if screen -list | grep -q "django"; then
echo "$(date) django exists; not starting django"
else
echo "$(date) django does not exist - starting now"
start_django
fi

if screen -list | grep -q "streaming"; then
echo "$(date) stream exists; not starting stream"
else
echo "$(date) stream does not exist - starting now"
start_stream
fi
fi

The -c /home/myapp/*_cron_log.conf -L in screen will be removed for production.

@reboot cronjob not executing

Mark Roberts pointed out a few things I'd done wrong.

Namely, the spaces here

MAIL = root
HOME = /

Get rid of those spaces..

Next, having Cron configuration fixed to email every minute.. instead of what I had :

*/1 * * * * /home/user/tester.py

Seems to me Lubuntu doesn't support the @Reboot Cron syntax.

Crontab not starting command on reboot

Quite often, if something runs fine from the command line but not as a cron job, it's because the PATH is set differently for cron jobs. The PATH is set quite conservatively to make sure that the wrong thing doesn't get run, which would cause security issues.

You could start by putting the full path to the ibeacon program in the shell script (i.e., run it as

/path/to/ibeacon scan | ...

rather than just

ibeacon scan | ...

The script is also being run as root, so if ibeacon is in your own bin directory, it won't be found without the full path.



Related Topics



Leave a reply



Submit