Automate Tasks Using Cronjobs

Blog post by Ionuț Alixăndroae

Ionut Alixandroae

January 11, 2022

4 min read

What is cron?

Oftentimes we need to perform the same tasks multiple times in a month, or even in a single day.

Software wise, such a task could mean an API call, a script performing a certain lookup job, or even a database interrogation culminating in a CSV report. The situations or the needs are infinite, really!

But what if the job is more than a simple read/write operation and it is something that needs to happen every hour, or every 10 minutes?

Well, a very well known technical solution, and also very utilized, is cron.

What is cron? 🤔

Cron (also known as cron job) is a command-line utility, or a job scheduler on Unix-like operating systems.

As simple as that! 💡

Basically, in only one line, a person can set up a scheduler on a machine (server, VM, even your own laptop), and this scheduler will take a command to execute at the time indicated in the setup.

The beautiful thing is that you can basically register the server to execute the job anytime you want, even every second, so you have full control! 💪

Why and when do I need cronjobs?

Personally, I used cron jobs for many many things. I scheduled jobs for database ingestion or database updates, for situations when I had to read from databases, and based on specific triggers to send automatic emails, when specific files in a folder were updated that required my attention, or really for keeping a huge system (huge database with dozens of API endpoints, multiple back-ends and multiple front-end components) permanently updated.

Set up a cron job

In order to register or set up a cron job, you have to open a terminal and type

$ crontab -e

This will open your personal crontab (cron configurationfile).

And just add the command on the first line and then save the updates by pressing :q! and then the Enter key.

A cronjob looks like this

# minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0-7) command
* * * * * command

Cronjob for Python files

If you want to register a Python script to run every minute, you would set up the job like this

* * * * * python path/to/script.py

Or every 10 minutes

*/10 * * * * python path/to/script.py

But the command is not limited to Python only, it can be almost anything.

Cronjob for JavaScript files

Set up a job to run a JavaScript file using NodeJs, every morning at 06:00AM

0 6 * * * node path/to/script.js

Cronjob for Java files

Set up a job to run a Java file using Java, every Monday and Friday at 09:00AM and 15:00 PM

0 9,15 * * 1,5 java path/to/script.java

Cronjob for bash files

Set up a job to run a bash file, every Tuesday of the month, every month, every 5 minutes between 08:00 AM and 18:00 PM

*/5 8-18 * * 2 echo path/to/script.sh

There is also a great website where you can play around with different cronjobs setups and get real time feedback on the output. You can access it at https://crontab.guru/

Conclusions

This was a short post, but, in my opinion, a good to know one.

Cronjobs are really like a must have Swiss army knife in your developer tool belt. It's simple and effective and can help anyone a great deal!

You don't even have to be a developer or a software engineer to understand how this working and to register a couple of automated tasks that will make your life way easier! 🚀