Automating stuff on your computer with cron jobs (with any language)
Tags: tutorial, productivity, dev, python
Published: 2019-12-15 12:15:45

---
title: Automating stuff on your computer with cron jobs (with any language)
published: true
description: Ever wanted to automate all these boring tasks that you do manually on your computer? Let's see how we can be lazier using cron jobs.
tags: tutorial,productivity,dev,python
---

**_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: 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/fl1mrzbakyngm0q3zj4c.png)

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](https://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. 😅*


{% gist https://gist.github.com/Brumor/dfd5dac7d88ee880f6c68771fe038f7e %}

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: 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ixjamd3rbyv2dwrgc65q.png)


## 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.