What I Learned Yesterday #15

Today’s article will be about crontab, a tool in Unix operating systems for scheduling jobs. Actually Wikipedia explains it better:

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos).

What I like about cron is its simplicity: you just write a line of code and your script will be running on its own. This is the structure of the that magical line of code:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

So basically this example will run python script to download some data every 10 minutes:

*/10 * * * * /usr/bin/python3 ~/sandbox/get_files.py

I won’t invent a wheel by explaining how it works in detail, common issues, tip & tricks as there are a lot of info about it – the article from Wikipedia actually is really good. And not only Wikipedia. All the issues I had were resolved by Google query “How [to do something] in cron”.

I love Google, you can find anything nowadays and what you do with that information now is completely depends on you, so excuses like “I don’t know how to do this or that” just disappear. The only thing remains is … action, execution.

Leave a Reply

Your email address will not be published. Required fields are marked *