The term cron refers to a time-based job scheduler that triggers commands at set time intervals. It is configured through a simple text file called the crontab. Within this file are a list of cron jobs, each having a command and a time interval in which they are to be run.
Cron jobs can be used in any sort of automation task. Here is a short list of tasks you could use for automation.
The cron daemon is a dormant process that gets run whenever encountering a time which it is to execute some command. This daemon is responsible for executing each cron job listed in our cron tabs.
If this is the first time hearing the termin daemon (pronounced DEE-mon), it's simply a background process that answers requests to services.
Each job to be performed is stored in a file called the crontab. Here is an example of one might look like.
43 2 * * * echo "It's 2:43am!" 0 8 1,15 * * echo "Run every morning at 8:00am on the 1st and 15th of every month." */30 9-12 * * * echo "Run every 30 minutes from 9-12 am."
We'll learn how to decipher each line in the next page. For now, let's learn how to access this crontab with the
The main command used to edit your crontab is the
We can edit our crontab by passing in the
-e option. This will open the file in our default editor. If you've never opened up the crontab before, you'll get the following boilerplate, with comments guiding you on how to set you a cron.
$ crontab -e # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command
To list out all current crontab to standard out, use the
You may also check if the cron daemon is currently running. The top output shows
cron is up and running, while the second line shows the current
grep command that was just used.
$ crontab -l # Current crontab file outputted to standard out $ ps aux | grep cron root 787 0.0 0.3 23660 1788 ? Ss 18:45 0:00 cron JohnDoe 2685 0.0 0.4 15948 2236 pts/1 S+ 19:38 0:00 grep --color=auto cron
To discontinue the current crontab, use the
-r option. We can pair this with the
-i option to prompt the user 'y/Y' before actually removing the crontab.
$ crontab -r
Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.$ Check price
Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.$ Check price