Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer
    
But why should I put the code in the wrapper and not inside the script? –  Jim Apr 8 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 Apr 8 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 Apr 8 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. –  David Wilkins Apr 8 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 Apr 8 at 19:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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