Cron is a software utility that allows us to schedule tasks (cronjobs) to run periodically, at fixed times or at pre-determined intervals.
These tasks can serve any desired function. Some common applications include periodically backing up user data or calling an API endpoint.
In this article, I’m going to cover the basics of setting up and running cronjobs. Although I use a macOS (Mojave) system, the examples should work on Linux systems, provided all the necessary dependencies are installed.
If you are on a linux system, this article will assume you already have all the dependencies installed.
In order to display current crobjobs, open the terminal and enter the following command:
If you’ve never done this before, the output should be empty. Let’s change this by actually creating a shell script that will contain the job to be executed periodically.
Now let’s create a cronjob. To keep this simple, we will create a simple bash script that prints “Hello, world!” to a file. Create a bash script in the directory of your choice with the following code:
#!/bin/bashecho "Hello, world" >> ~/Desktop/job.txt
Save this script as ‘job.sh’ and open a terminal window in the script’s directory. Run the script by with the following command:
A new file named ‘job.txt’ should have appeared in the directory with the expected output. Run the file several times and you should notice the script append to the file.
Running this manually every single time doesn’t seem practical. So let’s use cron to make this process run automatically at a certain intervals.
Before we can get cron to execute this bash script in the background, we first have to make sure the script is executable. We can achieve this with the following command:
chmod +x job.sh
Make sure you run this command in the same directory as the script. If not, make sure you specify the path of the script.
Now, we can add the job to the crontab. Using the following command:
This gets us into crontab’s edit mode. If your default editor is Vi, press ‘i’ in order to enter insert mode. Now type the following line:
* * * * * ~/Desktop/job.sh
The second part of this command “~/Desktop/job.sh” is the path to the shell script we’d like to execute. In my case, this resides in the desktop folder. Be sure to specify the exact path your script is in.
Notice the “* * * * *” at the beginning of the statement. This is the actual schedule condition for the job’s execution.
Each position, from left to right, represents minutes, hours, day of the month, month and day of the week respectively. An asterisk(*) simply means “every”. So, in this case, we are executing this job every minute of every hour of every day of the month, every month and every day of the week.
Here’s an excellent article with an in-depth explanation on formatting crontab commands.
For now, let’s save this by pressing the escape key and then typing “:wq” in order to save and quit.
You should notice the file getting appended to every minute now. Great the cronjob works!
This was by no means an in-depth look at cronjobs and their power. However, I do think it serves as a sufficient starting guide on setting them up. In a future article, I will demonstrate how to execute python scripts while keeping track of terminal output.
If you enjoyed this article, consider following my personal website for early access to my content before it gets published on Medium (don’t worry, it’s still free with no annoying pop-up ads!). Also, feel free to comment on this post. I’d love to hear your thoughts!
Originally published at https://kelvinmwinuka.com on October 8, 2020.