2

Suppose I have the following cron entry:

* * * * * /bin/date

Now suppose that I want scripts that run both before and after the cron job runs.

I could modify the cron entry to look like this:

* * * * * /bin/prehook ; /bin/date ; /bin/posthook

Or if I wanted the exit code of the prehook to determine whether or not the date command runs, I could do this:

* * * * * /bin/prehook && /bin/date ; /bin/posthook

However, I'm looking for a solution that I might be able to apply globally to all cron jobs without editing every single crontab; I see this as analogous to pre-commit and post-commit hooks that you see in many version control systems.

Pete
  • 21
  • 2

2 Answers2

0

I don't think there exists an easy solution using a standard cron implementation like Vixie cron unmodified.

Wrapping all the jobs in a single crontab should be achievable quite easily by setting SHELL in the crontab to a custom wrapper script, but there doesn't seem to be a way to globally configure the default shell used.

If moving away to another cron implementation is an option, then eg. bcron seems to use a global wrapper script for similar purposes. But I have no idea how alive and usable this project is.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41
0

The system I use is that a crontab entry always consists of a single script name, such as:

3  1  *  *  *  /home/me/bin/Cron/daily

The script in /home/me/bin/Cron/daily is actually a link to a single script, runcron, which looks at what it is called (e.g. daily). It sets my environment (since cron does not set more than the very barest of minimal environments), and then runs /home/me/bin/daily. If (when) I need to debug cron, or add pre-hooks or post-hooks, I can do it globally (by modifying /home/me/bin/Cron/runcron) or locally (by making /home/me/bin/Cron/daily temporarily into a modified copy of the normal runcron script). This flexibility is very useful on those occasions when I need it, and doesn't involve any change in the actual crontab entry.

My scripts do relay arguments if they're provided. I've never found sufficient reason to use that functionality in the long term, though. I have entries for daily, weekday, weekly and monthly. (See also Where can I set environment variables that crontab will use?)

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Your solution modifies cron; ideally I wouldn't have to change every single cron entry. But the wrapper solution may be the way I need to go. – Pete May 02 '12 at 13:35