Automating stuff on your computer with cron jobs (with any language)
Paul Bricout
Posted on December 15, 2019
This post was made on mac, works also on Linux, for windows you might want to use scheduled tasks instead.
Hey, in this tutorial I will explain how to automate stuff by running a script at a specific time on your computer using the magic of cron jobs. I'll take the example of cleaning your download folder, but you can be creative and go crazy with this!
Also, note that I'm using python as an example because I think that it fits this purpose well, but you could use any language!
1) What are Cron Jobs?
A Cron Job is a command specific to Unix-like systems (e.g. Linux or macOS) that allows to schedule future tasks to be repeated at a certain interval. Very useful for automating things on your server (such as a CI pipeline for example), it can also be used to automate boring things too.
These intervals are coded following this scheme:
Examples:
-
* * * * 1 \<command\>
: Executes the command every minutes on Mondays -
*/5 * * * * \<command\>
: Executes the command every 5 minutes -
0 */6 * * * \<command\>
: Executes the command every 6 hours at 00 minutes.
These commands can be very customizable, go to Crontab Guru to play around and find you perfect interval.
2) How to set up a cron job?
So as an example I made a python script that looks for file that haven't been modified since three weeks and deletes them, it also shows a desktop notification when running.
If you want to do the same copy this file and put it in your download folder. It works on mac, but could easily be modified for linux (or windows).
Please also note that Python isn't my main language, I just use it for small scripts. 😅
Once you have downloaded this, go to your command line and type:
env EDITOR=nano crontab -e
This will open a text file in the nano editor. Then write:
0 */4 * * * (cd ~/Downloads/ && python cleanup.py)
This will automate the script and run it every 4 hours at 00 minutes. If you want to add more scripts to run, just add line and writ another command! Once you're done hit control+O
and enter
to save and control+X
to exit.
And here you go, the scripts is running without you needing to do anything:
3) Further commands with crontab:
- To list all current cronjobs:
crontab -l
- To add a line or edit or delete a particular line use the previous
env EDITOR=nano crontab -e
command and edit the line, save and you're good to go. - To erase everything use:
crontab -r
And here we are, you know everything you need to start automating the boring tasks on your computer! Here my example was pretty simple, but using more complex scripts and a bit of networking you could do crazy things, like automating your whole file system, organizing your mails (using GMail's API), and many other things!
This is my first tutorial ever, so please feel free to leave a constructive comment on what I could have done better for the next ones.
Posted on December 15, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.