Cron FTW


Linux, being the awesome blank canvas that it is, has lots great things. But my newest favorite: cron. By running crontab -e your crontab will be opened in your $EDITOR1. From here you get to decipher crontab timing logic. OoO Spooky! Jokes aside, it’s really not too bad. The following table outlines how it works:

Cron:mhdommondow
English:MinuteHourDay of monthMonthDay of week
Value:0-590-240-310-120-6 (0 = Sun)

This means that if your crontab looks like this:

0 0 * * * reboot

Your computer will reboot at midnight every day. Crontab.guru makes this really easy to figure out.

For business tasks, here’s a pretty helpful one. It also shows off the range feature!

0 0 * * 1-5 xyz

Which translates to: “At midnight, M-F, run xyz”

It’s probably worth noting that you can do dumb stuff such as:

0 0 31 2 * xyz

Which runs on the 31st day of February.

I recently used this to download a YouTube video, following the creators release schedule, which I would then re-host on a local machine. I did this because my phone broke, so I had to use an old phone without a sim card in it (I need any phone for work because of 2FA codes.) But I get my news from a YouTuber. So this way I could just create a shortcut on my homescreen to the ip where I was re-hosting the video, and download it in the morning while going out the door. Then I could listen to the downloaded file on my way to work in the car! Pretty nifty! So my crontab looked something like this:

0 23 * * 1-6 /usr/bin/download_video

(Read as: At 11p.m., Monday thru Saturday, run the “download_video” script.)

Footnotes

  1. The variable $EDITOR is commonly set to nano, a cli text editor. But you can reassign it to something useful like EDITOR=nvim in your .bashrc


Tags: