Cron how I love thee

Posted: June 4, 2014 in Debian, General, Hacking, Hardware, Raspberry PI, RPi, Software

So carrying on with my sprinkler controller project I need an easy way to run things on the 1st Monday of a month and the 3rd Monday and only then.  Hmmm, I can do that with cron by looking up the dates and entering each day but that is a manual process to look that up and then enter 24 entries in the crontab…. Must be a better way so I go surfing the Internet and what do I find but a nice bash script called run-if-today.  You feed it the day of the week and what week of the month you want the entry to run. Perfect  makes the crontab 2 lines vs 24 lines!

# m h dom mon dow command
00 06 02 06 * sudo /home/pi/bin/
00 06 16 06 * sudo /home/pi/bin/
00 06 07 07 * sudo /home/pi/bin/
00 06 21 07 * sudo /home/pi/bin/
# etc, on and on until the end of the year


# First monday of month jobs
00 06 * * 1 run-if-today 1 Mon && sudo /home/pi/bin/
# Third monday jobs
00 06 * * 1 run-if-today 3 Mon && sudo /home/pi/bin/

Much better, it will run forever on the first and third Monday. Yea.

So credit where credit is due: I found this on github cron-last-sunday it’s under an MIT license and Copyright (c) 2014 Manuel Gutierrez.  Nice work.  I had a friend Debianize the script into a nice easily installable Debian package, which I need to figure out where to stash it so others can install it on their RPi, because it really simplifies using cron for certain conditions.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s