An Overview of crontab
What is crontab?
Suppose you want to take a backup of database daily, you have to log in to phpMyAdmin and perform a full backup of your database daily/weekly/monthly as per your requirement. What if you just have to set the whole process up once and it happened automatically? without your help?
Crontab helps us just for these kind of tasks. Repetitive-Monotonous-Autonomous, i.e, which are done repeatedly, contains same steps and can be automated too.
Crontab is a utility available in Linux to schedule time-based jobs.
How to set it up?
Crontab has an easy command for that
crontab -e
-e , well you guessed it, stands for ‘edit’. Crontab uses the same user to execute the script from which it is edited. For eg, if you want to run a command using superuser, you have to first login to superuser and then type the above command.
It’s asking which editor to use by default? It’s simple, whichever you like(I use nano). A window opened up. Now what?
Timestamps Overview
Now you have to decide when you want to run the script, from where to run the script, and if you want the script output to be saved to a file.
* * * * * php /home/ubuntu/take_db_backup.php >> /home/ubuntu/output.log
Let’s understand the above command?
In * * * * *
each *
has a particular meaning.
From left to right:
Minute — can be 0(at the starting of every hour) to 30(every hour at 30 minutes) to 59(every hour at 59th minute)
Hour — can be 0(at midnight) to 12(at noon) to 23(at 11pm in the night)
Day(month) — can be 1(1st of every month) to 31(31st day of every month)
Month — can be 1(January) to 12(December)
Day(week) — can be 0(sunday) to 6(saturday)
You can use
crontab.guru
for a bit of ease.
Then, It’s just how you will run the script. For different execution styles, you can write as follows:
* * * * * python /path/to/script/send_me_stats.py >> /path/to/error/log.log0 4 * * * php /mail/me/daily/at/4/am.php# this is a comment
0 0 * * * /run/this/bash/file/at/midnight/upload_images.sh
Above are some of the examples which can be written in crontab.
Testing a script
Now that you know when you want to run it, test it. See the current time, set the current time in the cron. For eg. If right now is 12:20am, I will use following to test
21 12 * * * python /path/to/test/file.py
When i save and exit, it should run at the very next minute. If the result is as expected. Put in the correct time and save & exit.
Voila! your crontab is up and running.
More Options.. Do I need them?
There are bunch of more options too:
- -r Use at your own risk! It resets the crontab of current user. You can check out another blog on what to do if I f****d up my crontab?
- -u user To specify the user which will run the cron. This can be run only as root user
- -l To check the list of crontab entry of the current user.
- -d dir To set the location of crontab directory
Bonus: Useful crons for everyday use
The below cron will remove any files under /tmp that haven’t been modified in more than seven days:
0 4 * * * find /tmp -mtime +7 -exec rm -f {} \;
This cron will display the current time on the system console every 20 minutes:
*/20 * * * * (echo -n " "; date; echo) > /dev/con1
Sources:
- http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_utilities%2Fc%2Fcrontab.html
- https://crontab.guru/
Thanks for your time!