0

I am on CentOS 7, and if I put:

* * * * * python script.py

It works.

If I put:

@reboot python script.py

Then it says it cannot connect to MySQL.

If I run it from command line too, it works.

The script only does not work if I do @reboot. Is it possible cronjob is starting before MySQL does?

1
  • What are you trying to do?
    – caskey
    Apr 24, 2015 at 7:20

4 Answers 4

0

Yes, it is possible (and very probable).

A crude solution is to instruct your script to sleep some time. For example, change your cron line as below:

* * * * * sleep 60; python script.py

This will wait 60 seconds, which should give MySQL time to start.

If you had to be absolutely sure that mysql process started before your script runs, you can write a shell wrapper script like this (let us call it custom_wrapper):

#!/bin/bash
while true; do
  running=`ps ax | grep mysql`;
  if [ $running -eq 0 ]; then
    python script.py
    exit $?
  fi
  sleep 5
done

Then, set is as executable (chmod +x custom_wrapper) and modify your cron line as below: * * * * * custom_wrapper

The custom wrapper script wait for MySQL process to become active, then execute your python script and exits.

0

Yes, the cron daemon could quite easily startup before mysql has completed (or even begun) its startup process.

0

Yes, this is absolutely possible. Cron is an important service on every system and will likely start before something like MySQL.

While it might be possible to alter the order, this has the potential to screw up your system if you are not extra careful. A better alternative would be to just add a delay to your cron script so it waits long enough that MySQL will be running (e.g. 60 sec.).

0

I get the feeling that you're trying to use cron as a process supervisor. (@reboot is once while * * * * * is every minute).

Better idea 1:

Use systemd (I think that what's EL 7 systems are using, I've yet to really play with it myself).

Better idea 2:

Have a init-script that starts the service and manages a lock file (/var/log/subsys/SERVICE would be best, as an init-script is expected to manage that on a RHEL box (well, at least prior to 7, not sure if that is still true in 7).

Then, from crond (or whatever configuration management system you are using, such as cfengine, puppet, chef, etc.) call /sbin/service SERVICE ensure-started (where the ensure-started would be something added by you. I can't remember if condrestart is meant to do that or not....).

PS. documentation for init-scripts can be found in /usr/share/doc/sysvinit* or similar on RHEL systems.

1
  • With systemd timer-units you can set dependencies that need to be fulfilled before the timer-unit can run. With this you slowly come in the area former only ruled by commercial schedulers like Autosys and Maestro.
    – hspaans
    Apr 24, 2015 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .