How to Set Up Automated Tasks in Linux Using Cron | by 👩🏻‍💻 Kessie Zhang | Towards Data Science

Have you ever found yourself doing repetitive tasks on a regular basis? For example, deleting temporary files every week to conserve your disk space, scraping data from a site every week to gather new information or sending recurring emails to the same set of people for “reminder” campaigns, and so on. If so, you might want to set up a cron job scheduler, which will automatically perform the tasks for you at any scheduled time.
Cron comes from “chron,” the Greek prefix for “time.” It’s a daemon to execute scheduled commands on Linux or Unix-like systems, which allows you to schedule any tasks at specified intervals.
Cron: Cron is a daemon that runs at the times of system boot.
Crontab: Crontab (cron table) is a file that contains the schedule of cron entries to be run and at specified times. File location varies by operating systems.
Cron job or cron schedule: Cron job or cron schedule is a specific set of execution instructions specifying day, time, and command to execute. crontab can have multiple execution statements.
Before we jump into setting up a cron job, we need to know how to specify how often and when the cron job will run.
It consists of five parts: minute, hour, day of the month, month, and day of the week.
* * * * * command to be executed- - - - -| | | | || | | | ----- Day of week (0 - 7) (Sunday=0 or 7)| | | ------- Month (1 - 12)| | --------- Day of month (1 - 31)| ----------- Hour (0 - 23)------------- Minute (0 - 59)
For example, the given code will execute a job on 10th June at 08:30 AM. Notice that * means all the possible units. For more information, you can check out more examples from Geeksforgeeks.
30 08 10 06 * /PATH TO FILE/-  -  -  -  -|  |  |  |  ||  |  |  |  ----- every Day of week (0 - 7) (Sunday=0 or 7)|  |  |  ------- 06th Month (1 - 12)|  |  --------- 10th Day of month (1 - 31)|  ----------- 08 Hour (0 - 23)------------- 30 Minute (0 - 59)
crontab -e Edit crontab file, or create one if it doesn’t already exist.crontab -l crontab list of cronjobs, display crontab file contents.crontab -r Remove your crontab file.crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
a Append text following the current cursor position
A Append text to the end of the current line
i Insert text before the current cursor position
I Insert text at the beginning of the cursor line
o Open up a new line following the current line and add text there
O Open up a new line in front of the current line and add text there
x Delete character
dw Delete word from the cursor on
dd Delete line
:wq Write the file to disk and quit the editor
:q! Quit (no warning)
:q Quit (a warning is printed if a modified file has not been saved)
To start a new crontab file, you will need to type crontab -e in your terminal (Note: it’s not necessary to type this command in the same directory as the file you want to execute.) After that, you will open an empty VIM editor. We can then write the time syntax and the location of the file you want to execute.
Add this line inside the vim editor: * * * * * /PATH TO FILE/script.py
If this works for you, you are done! You just created a cron job.
If not, don’t worry, keep reading. ❤
If you receive this error message like this “/bin/sh: /Users/XXX/Documents/Python/shellScript.sh: Permission denied”, you can try adding a python executable path.
which python
This command-line returns the python executable path. By adding the python executable, cron can now execute the python script.
* * * * * /PATH TO Python Executable/ /PATH TO FILE/script.py
If you receive this error message like this “[Errno 1] Operation not permitted”, simply drag /usr/sbin/cron folder into Full Disk Access in System Preference can help you solve the problem.
You might want to check whether the cron job is running fine. To do so, you can create a log file that collects the cron execution log. By adding this >> /tmp/test.txt after the file name, you will save the cronjob’s output in the test.txt file.
* * * * * /PATH TO FILE/script.py >> /tmp/test.txt
There you have it! It is pretty easy and straightforward to set up a cron job, and it will make our lives so much easier. If you have any suggestions, please feel free to share them in the comment section.
If you find this helpful, please follow me and check out my other blogs. ❤️
Until next time, happy learning! 👩🏻‍💻