we have a multi cron jobs runing everyday on different hours. In sometime script is running too long and block database in time where we set up a new one. The problem is that, a new database is no longer creating from script(we must do all manual).

How can i stop/ freez/terminate all jobs on for example 4:00 am; wait until new database setup fine and run it again?

  • which database system? PostgreSQL? MySQL? SQLite? Something else? – Wouter Verhelst Mar 27 '17 at 16:27
  • We use a PostgreSQL database – Martin Gerlach Mar 27 '17 at 16:46

In PostgreSQL, you can check what other processes are running by way of the pg_stat_activity system view. If you can recognize your long-running process from that, you could start your cron job by checking that table. If it has an entry for the long-running process, wait. You can poll the database every so often, or you could use the LISTEN/NOTIFY to signal that the database is now ready (this may require changes to both scripts though)

The advantage of this method is that it works if both scripts aren't running on the same machine, but it may be a bit more complex to set up properly.


Modify your database setup process to drop a canary file into /var/run (e. g. /var/run/db_freeze) upon startup, and remove it upon completion.

Modify your cron jobs to immediately terminate upon startup if that file exists.

For example, for the long-running job:

# long-running DB setup job
touch /var/run/db_freeze
trap 'rm -f /var/run/db_freeze' EXIT

And for the cron jobs:

if [[ -f /var/run/db_freeze ]]; then
    #  Bail due to process lock from long-running job
    exit 2
exit 0

If you want to wait for the longer process to finish, and then kick it off, rather than exiting, you can wait for the canary to go away:

while [[ -f /var/run/db_freeze ]]; do
    sleep 30

Note, however, that without working on locking the cron jobs themselves, if the long process goes longer than one cron cycle, you could end up with two concurrent instances of the cron job waiting to jump into action.

  • Thx, i try it tomorrow. – Martin Gerlach Mar 27 '17 at 19:27
  • That actually makes the cron job exit, not wait, when the other process is still running. I agree that that might be better, but it's not what the OP asked ;-) – Wouter Verhelst Mar 27 '17 at 21:16
  • I will add the other way as an alternative option. – DopeGhoti Mar 27 '17 at 22:13

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.