How to automate tasks with CRON on Ubuntu
A cron job is a Linux command used for scheduling tasks to be executed sometime in the future with a pre-decided time and date pattern. This is normally used to schedule a job that is executed periodically — for example, to send out an email notification every morning.
Pre-requisites
- Access to Ubuntu 18.04 or higher
- Script to be executed with cron
- CRON daemon setup (We’ll cover it anyways)
As a quick guide, here is our sample Python script in place
mock1.py
from datetime import datetimeprint("CRON executed at - \n")
print(datetime.now().strftime("%D %H:%M:%S"))
Virtual environments & CRON
Your .py script would be mostly built around the virtual environment bubble containing necessary python packages.
And to ensure that your cron job accesses the environment-specific modules associated with its own virtual environment, just quickly run below command in your activated virtual environment:
>> which python3
/home/user1/mock_project/my_venv/bin/python3
..which returns above python executable path. Keep it handy for our next step
Next we’ll install and setup cron daemon in our Ubuntu environment:
- Update your computer’s local package dependencies
sudo apt update # remove sudo if you do not require root privileges
- Install cron
sudo apt install cron
- Make sure its set to run in the background
sudo systemctl enable cron
- Check cron active/inactive status
sudo systemctl status cron
- Add your cron job(s) by enabling crontab
crontab -e
If its your first time setting up cron, you’ll be prompted to select a default text editor when editing jobs within crontab.
You can choice either of the options comfortable with. For now let’s go with nano editor —
Now, you’ll notice there is no Time zone indicated — by default UTC. We can change that with our local time zone — example IST (Indian Standard Time)
TZ = "IST" # first LOC below documentation comments
Now, there are essentially 3 parts of the cron job command we need to focus on —
cron pattern | python path | python script
Writing the correct syntax patterns for cron jobs seems a tedious task but I personally find crontab.guru very easy to experiment with.
Our python script would be executed every Friday at 11.03 am.
3 11 * * FRI /home/user1/mock_project/my_venv/bin/python3 /home/user1/mock_project/mock1.py
Optionally, you can track error logs if the cron job fails at any given point by appending a simple parameter to our cron command:
3 11 * * FRI /home/user1/mock_project/my_venv/bin/python3 /home/user1/mock_project/mock1.py > /custom_path/abcd.log 2>&1
Save and close the crontab; view all listed cron jobs(read mode only):
crontab -l
To view currently running cron jobs execute:
sudo grep CRON /var/log/syslog
If you are interested in a quick test-run of cron daemon on your Ubuntu system just enable below cron job that will print current date every minute:
* * * * * /bin/bash -l -c 'date > ~/cron-test.txt'
Useful tips
As part of your Web applications that are IaaS based and require periodic database backups, you can setup daily/weekly CRON jobs.
For example — Django provides a single LOC to dump database at specific location. You may go a step ahead and dump your DB files encrypted in private Cloud storage!
And that’s all needed, hope it serves as a quick guide. Always appreciate feedback and thoughts.
Other articles that helped me along the way
https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804