Ben Packard
Ben Packard

Reputation: 26476

Crontab job as a service

I have a script that pulls some data from a web service and populates a mysql database. The idea is that this runs every minute, so I added a cron job to execute the script.

However, I would like the ability to occasionally suspend and re-start the job without modifying my crontab.

What is the best practice for achieving this? Or should I not really be using crontab to schedule something that I want to occasionally suspend?

I am considering an implementation where a global variable is set, and checked inside the script. But I thought I would canvas for more apt solutions first. The simpler the better - I am new to both scripting and ruby.

Upvotes: 1

Views: 167

Answers (1)

jolivier
jolivier

Reputation: 7635

If I were you my script would look at a static switch, like you said with your global variable, but test for a file existence instead of a global variable. This seems clean to me.

Another solution is to have a service not using crontab but calling your script every minute. This service would be like other services in /etc/init.d or (/etc/rc.d depending on your distribution) and have start, stop and restart commands as other services.

These 2 solutions can be mixed:

  1. the service only create or delete the switching file, and the crontab line is always active.
  2. Or your service directly edits the crontab like this, but I prefer not editing the crontab via a script and the described technique in the article is not atomic (if you change your crontab between the reading and the writting by the script your change is lost).

So at your place I would go for 1.

Upvotes: 2

Related Questions