helpful tools https://crontab.guru/

Basics

man cron

man crontab

# list all regular tasks
crontab -l

# list all tasks (root)
sudo crontab -l

cron expression format:

min hour day_of_month month day_of_week

Examples

#every 1 min
*/1 * * * * 
#every 5 mins
*/5 * * * * 
#every hour at 0 min
0 * * * * 
#every hour at 30 min
30 * * * *
#every 3 hours
0 */3 * * *
#everyday at 3:00 am
0 3 * * *

Tricks

System-wide and per-user

Under /etc, there are:

  • cron.d: standalone scripts
  • cron.daily
  • cron.hourly
  • cron.weekly
  • cron.monthly
  • crontab: system crontab; nowadays empty by default

which are system-wide jobs.

For per-user job, use crontab -e; and the files are kept in /var/spool/cron. When writing per-user job, there is no need to specify the user.

Run command as root

sudo crontab -e

then you can add something like

@daily /usr/bin/geoipupdate -v

the root jobs are stored in /var/spool/cron/root

Redirect the errors

By default the errors are not captured in the file.

/dev/null is a blackhole, and 2>&1 redirect the stderr to stdout

#use a blackhole: just run the job, say nothing
>> /dev/null 2>&1

#redirect everything to a file
>> output.log 2>&1

Less than 1 minute job interval

use sleep

* * * * *  opc python3.9  /home/opc/twitter_bots/anti_harassment_bot.py  >> /home/opc/bots_output.txt
* * * * *  opc sleep 30; python3.9  /home/opc/twitter_bots/anti_harassment_bot.py  >> /home/opc/bots_output.txt

Prevent overlapping jobs

Sometimes the running time of the job might be longer than the interval.

To prevent overlapping jobs, use flock

*/4 * * * *  opc /usr/bin/flock -w 0 /home/opc/twitter_bots/report.lock python3.9  /home/opc/twitter_bots/reporting_bot.py >> /home/opc/report.log