From 22299f84d39c26dd661a217809ae3174ccfaf4eb Mon Sep 17 00:00:00 2001
From: Luke Smith
You can schedule anything! Some examples of what you might have done already include:
And many more, anything you can do can be turned into a cronjob.
@@ -34,25 +34,25 @@
- This the preferred method for personal tasks and scripts, it's also the easiest to get started with. Run the command
- Once you have figured out the command you want to run you need to figure out how often you want to run it and when. I am going to schedule my system updates once a week on at 3:30 AM on a Monday.
+ Once you have figured out the command you want to run you need to figure out how often you want to run it and when. I am going to schedule my system updates once a week on at 3:30 AM on Mondays.
We now have to convert this time (Every Monday at 3:30 AM) into a cron time. Cron uses a simple but effective way of scheduling when to run things.
- Crontab expressions look like this
So for our Monday at 3:30AM job we would do the following:
-
- Let's add another job, our backup job (for the purposes of this our backup command is just called
We can also randomly specify non-consecutive arguments with a comma.
Suppose you have a script you want to run at the midday of the 1st, 15th, and 20th day of every month.
- You can specify that my putting We can also easily run a command very several minutes or months, without specifying the specific times:
- We can also easily run a command every several minutes or months, without specifying the specific times:
- This cronjob will run the This cronjob will run the
-
Some tasks that you might want to schedule may include:
updatedb to update your locate database certbot to update renewing of your https certs updatedb to update your locate databasecertbot to update renewing of your https certs
-
Basic Cronjobs
crontab -e to access your users crontab
+ This the preferred method for personal tasks and scripts; it's also the easiest to get started with. Run the command crontab -e to access your user's crontab
* * * * * command-to-run
+ Crontab expressions look like this * * * * * command-to-run
The five elements before the command tell when the command is supposed to be run automatically.
.---------------- minute (0 - 59)
+ .---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
- | | | .------- month (1 - 12
+ | | | .------- month (1 - 12)
| | | | .---- day of week (0 - 6)
| | | | |
* * * * *
@@ -67,7 +67,9 @@
More examples
backup) We want to run backup Every evening at 11PM, once we work out the timings for this we can add the to the same file as the above by running crontab -e This would mean our full crontab would look like this:
+ Let's add another job, our backup job (for the purposes of this our backup command is just called backup).
+ We want to run backup every evening at 11PM.
+ Once we work out the timings for this we can add the to the same file as the above by running crontab -e This would mean our full crontab would look like this:
0 23 * * * backupConsecutive times
@@ -87,21 +89,18 @@
1,15,20 for the day of the month argument:
+ You can specify that by putting 1,15,20 for the day of the month argument:
0 12 1,15,20 * * /usr/bin/pay_bills_script"Every X minutes/days/months"
-
- */15 * * * * updatedbupdatedb command every 15 minutes.
- updatedb command every 15 minutes.Beware of this Rookie Mistake Though...
@@ -146,13 +145,14 @@
- Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below
+ Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below:
+
crontab -e crontab -e-- cgit v1.2.3