The proper and easiest way to set cron jobs.

Will Barillon
3 min readJul 12, 2022

Your search for THE article dealing with cron stops here. You can finally relax, congratulation for digging this far !

Setting the environment up

If you already have a linux distribution you can probably skip this step. But for people like me on Windows, who don’t want to mess with their wsl, I prefer to do that on a docker container.

Trivial cron job

To set a cron job “manually”, type the following commands :

cron # starts croncrontab -e # edit a crontab file to install a new cron jobcrontab -l # read the crontab file, to make sure things are wellcrontab -r # delete the crontab file

“Wait William, that’s it ? I’ve read this a dozen of times before finding your article ! What’s next, the schedule syntax ?”

No my dear reader, I know that by now you should know the schedule syntax by heart, simply by reading every documentation on the whole internet looking for golden informations about cron. Don’t worry, the golden informations are coming ;)

Our trivial example is cute, but not suitable to be used yet. Out job task should call a script file in which our script is within. But this little change doesn’t go without problems…

Cron job calling a script

Now let’s add a new cron job which uses a script.

Debugging a cron job : identify the path

When setting a cron job up, there are two things to set :

  • the schedule of the script
  • the path of the script

In order to set the right path, the best way I found so far is to use the very trivial cron task I set : it echoes a string that is stored in a file… following a path. Which means that if you’re seeing your text file next to your script file while using ls, you know without a doubt that you path is set correctly.

When using path in a cron file, it is highly recommended to use absolute paths. The reason is your cron file will not be executed by “you” — the user, but by cron using your perspective as “the user”.

Let’s do an experiment : what will happen if I try to set my cron job with $(pwd) within the path ?

You can wait for a “I am here” entry for a long time, because it is not within the /code directory but the directory used by the cron job from the user perspective. Which means that for cron, $(pwd) is /<username> or ~.

A developper setting cron jobs always deals in absolute.

I explained the very basics of cron and crontab in this article. To go further, read my next article in which I deal with set cron job with a file and set cron jobs within a docker container.

--

--

Will Barillon
Will Barillon

Written by Will Barillon

A python developer / data scientist that wants to provide useful informations to everyone.

No responses yet