Crontab

How to start with Linux

Cron, the Linux task scheduler

The Linux Cron program, is very usefull for system automation. At first you may have to get used to it a bit, but when you know how it works, it’s very usefull.

Cron is widely used by system engineers or system administrators to maintain server parks/environments. It makes live a lot easier when you do not have to backup your VDI images, or LXC Containers manually or clean up unused cached files and directories after a days work.

Usage of Cron:

– Run system backups at specific times
– Run database backups at specific times
– Clean up working directories in “clean” environments
– Run remote scripts over ssh for server maintenance
– Run ping check every 10 minutes, to see if your servers or desktops are still online
– etc


The Crontab configuration file is located at: /etc/crontab

Basic crontab commands:

# list your current crontab
crontab -l
# edit or create crontab file
crontab -e
# remove your current crontab file
crontab -r

How to configure crontab:

# minute hour day_of_month month day_of_week command

# Runs every hour
0 * * * * /home/marc/scripts/bash/backup_stwl.sh

# Runs at 14:10pm every single day
10 14 * * * /home/marc/scripts/bash/backup_stwl.sh

# Runs every 10 minutes from 11 - 11:50 on every single day of the year
*/10 10-11 * * * /home/marc/scripts/bash/backup_stwl.sh

# Runs at 14:10pm only in the weekends (Saturday 6, Sunday 0)
10 14 * * 6-0 /home/marc/scripts/bash/backup_stwl.sh

# Runs every hour only in the summer months June, July, August 6,7,8
0 * * 6-8 * /home/marc/scripts/bash/temp_check.sh

# Runs every hour only in the summer months June, July, August, December 6,7,8,12
0 * * 6-8,12 * /home/marc/scripts/bash/temp_check.sh

# Runs every other day of the month on odd days at noon
0 12 1-31/2 * * /home/marc/scripts/bash/backup_stwl.sh

# Runs every other day of the month on even days at noon
0 12 2-30/2 * * /home/marc/scripts/bash/backup_stwl.sh

You can also run commands at computer startup

@reboot /home/marc/mail_i_am_awake.sh

At home, I’m not using crontab that much, but I’ve used it a lot to automate our work environments, I have no bad things to say about Cron, just a good tool to use.