3

I have a cron job that runs every X mins and that I would like to have a way to "signal" it not to do anything if needed.
Is there a standard way for this? The only idea I had is to check some directory for the existence of a file and if it is there exit. Does this approach make sense? Are there any other approaches?

1 Answer 1

4

When a cronjob is set correctly, it always runs. You must "signal" your script or program, not cron itself.

In your case, you can check existence of files easily. Making a wrapper script wrapper.sh:

#!/bin/bash

if [ -e /path/to/file1 ] && [ -e /path/to/file2 ]
then
    exit 1
fi

# Your script goes here

Then you must configure your cronjob to run wrapper.sh. It will check that your files exist, and if so it will exit, else run your script as normal.

6
  • But why should I put the code in the wrapper and not inside the script?
    – Jim
    Commented Apr 8, 2014 at 18:47
  • You don't actually need a wrapper (or to edit the script), you can just put the file test in the crontab itself—cron runs it through a shell.
    – derobert
    Commented Apr 8, 2014 at 18:50
  • @Jim and derobert: It depends on your implementation, each have its own advantages. If you use an wrapper script, you can also run an binary program, or you don't need to change cronjob entry when you change your program.
    – cuonglm
    Commented Apr 8, 2014 at 18:55
  • I like the way @Gnouc does this because it doesn't clutter the crontab, and it leaves whatever script you want this behavior to apply to untouched. Also, if you decide later to add scripts, you don't have to rerun the wrapper, just add your script to it. Commented Apr 8, 2014 at 19:18
  • 1) But if I add the check inside the script how does it clutter the crontab? 2) What is the 2 files check?
    – Jim
    Commented Apr 8, 2014 at 19:25

You must log in to answer this question.

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