0

I have an Upstart init script that manages a python script on the server. The pre-start and pre-stop scripts append a timestamped entry into a log file. There is a cronjob to stop and start the service once an hour. When I start or stop the service as root these entries get added to the log file, but when my cronjob runs the entries are not added.

Upstart script:

/etc/init/some_scriptd.conf

description "Manages some_script"
author      "me"

start on runlevel [2345]
stop on shutdown
respawn
respawn limit 10 10

script
    exec /usr/bin/python3.4 /usr/bin/some_script.py
end script

pre-start script
    echo "[`date +%Y-%m-%d%l:%M:%S%p`] Some Script Starting" >> /var/log/some_scriptd.log
end script

pre-stop script
    echo "[`date +%Y-%m-%d%l:%M:%S%p`] Some Script Stopping" >> /var/log/some_scriptd.log
end script

Cronjob:

crontab -l -u root

# Stop and start the some_scriptd service once an hour
30 * * * * initctl stop some_scriptd && initctl start some_scriptd

I've checked /var/log/cron and I can see the cronjob being run as scheduled.

Feb 15 09:30:01 my_server CROND[16371]: (root) CMD (initctl stop some_scriptd && initctl start some_scriptd)

The log file does not show entries from the cronjob, only from when I manually stop and start the service as root.

cat /var/log/some_scriptd.log

[2019-02-15 8:58:43AM] Some Script Stopping
[2019-02-15 8:58:43AM] Some Script Starting

Update: I checked the pid of the running python script before and after the cronjob ran and it remained the same. This indicates to me that the initctl command was not successful at all. It wasn't a matter of just the pre-start and pre-stop scripts failing.

  • I've encountered issues before with cronjobs not running as expected, and the solution then was to add a shell script wrapper that defines the $PATH variable. I can do this again if necessary, but I'd like to explore alternate solutions from the community. – bmccord Feb 15 '19 at 17:55
  • Your Linux distribution is either already past end of life or will be end of life within a few weeks. Consider upgrading to a currently supported Linux distribution, which uses systemd and thus already creates log entries when a service starts and stops. – Michael Hampton Feb 15 '19 at 20:06
  • This is a legacy server that can not be upgraded to a newer distribution, so I have to find a solution given the tools available. – bmccord Feb 15 '19 at 20:39
0

I ended up solving this problem with a shell script wrapper that sets the PATH of the cronjob to match the PATH of the root user.

/usr/bin/some_scriptd_wrapper.sh

#!/bin/bash
PATH="/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
initctl stop harvestd && initctl start harvestd

Cronjob:

# Stop and start the some_scriptd service once an hour
30 * * * * /usr/bin/some_scriptd_wrapper.sh

The problem appears to be a difference between the root users PATH and the PATH used by the cronjob.

I created a simple cronjob to find nail this down:

0 * * * * echo $PATH > /tmp/cronpath.txt

Then I compared the two PATHs and located the initctl command:

# cat /tmp/cronpath.txt
/usr/bin:/bin

# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

# locate initctl
/sbin/initctl

initctl is in /sbin which is not in the cronjob's PATH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.