Ludovic Frank - Freelance developer

Cron tasks, perfect for performing repetitive tasks at specific times...

ionicons-v5-k Ludovic Frank Oct 30, 2022
114 reads Level: Beginner

Hello there,
Today we're going to talk about Cron, a tool that lets you create scheduled tasks on a Linux server.
It's a very useful tool for automating repetitive tasks, such as sending e-mails, backing up files and so on.
For beginners, creating Cron tasks can be a little complicated, but don't panic... with the right tools and knowledge, it's pretty straightforward.

What is Cron?

Cron is the Linux job scheduling system.
It allows you to launch tasks at specific times, or at regular time intervals.
It can be found in all Linux distributions (as far as I know, maybe some exotic things don't use Cron, but I'd be surprised).
Personally, I use it in my projects, for example to send automatic e-mails, or to process data at night when everyone's asleep.

How do I create a Cron task?

The crontab -e command is used to edit the Cron task file.
In this file, each line corresponds to a task, and each line is made up of 5 fields separated by spaces.

Here's an example of a Cron task:
"0 0 * * * /usr/bin/php /var/www/html/monscript.php"

The first field corresponds to minutes, the second to hours, the third to the day of the month, the fourth to the month and the fifth to the day of the week.
After the fields defining when this task should run, you'll find the path to the script to be executed.

How do I test a Cron job?

Use the crontab -l command, which lists Cron tasks.
You can also run a task that writes to a file, and check that the file has been created or modified.

Cron pitfalls to be aware of

Each user has his own Cron tasks, and therefore his own Cron task file.
This means that if your user needs to access a file to which he does not have rights, he will not be able to modify it.

Finally, in my experience, an executing Cron task has no access to the variable
environment variable "$PATH", so if you want to run a script, you need to use the absolute path of the script, for example /bin/docker instead of docker.

I'd also like to take this opportunity to point out that even if this isn't the recommended method, because it's better to create a container specifically for this task, you can perfectly well
do something like this:
"50 3 * * * /bin/docker exec -u www-data mycontainer "php /var/www/html/monscript.php" "
It's not recommended at all, but it can help....

How can I remember all this easily?

Well, actually... you don't need to. There's a tool that lets you create Cron tasks easily, freeing up memory in your little head for something else.
It's called crontab guru.

Below each field, you'll find what it corresponds to, then, by editing the fields at the top of the page, you can see in real time when your task will run.

Conclusion

Cron is a very useful tool, allowing you to launch tasks at specific times, or at regular intervals.
It's very easy to use, but you need to know the fields that make up a Cron job, and the little pitfalls to avoid.

That's why the real aim of this article is to introduce you to crontab.guru.

Have a great week?