You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/automation/cron_jobs_howto.md
+37-25Lines changed: 37 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,10 @@
2
2
title: cron - Automating commands
3
3
author: Steven Spencer
4
4
contributors: Ezequiel Bruni
5
+
update: 2021-10-22
5
6
---
6
7
7
-
# Automating Processes with cron and crontab
8
+
# Automating Processes with `cron` and `crontab`
8
9
9
10
## Prerequisites
10
11
@@ -18,32 +19,37 @@ contributors: Ezequiel Bruni
18
19
**(You can run certain scripts in your own directories as your own user. In this case, switching to root is not necessary.)**
19
20
* We assume that you're pretty cool.
20
21
21
-
# Introduction
22
+
##Introduction
22
23
23
24
Linux provides the _cron_ system, a time-based job scheduler, for automating processes. It's simple and yet quite powerful. Want a script or program to run every day at 5 PM? This is where you set that up.
24
25
25
-
The _crontab_ is essentially a list where users add their own automated tasks and jobs, and it has a number of options that can simplify things even further. This document will explore a number of these. It's a good refresher for those with some experience, new users add the cron system to their repertoire.
26
+
The _crontab_ is essentially a list where users add their own automated tasks and jobs, and it has a number of options that can simplify things even further. This document will explore some of these. It's a good refresher for those with some experience, and new users can add the `cron` system to their repertoire.
26
27
27
-
## <aname="starting-easy"></a>Starting Easy - The cron Dot Directories
28
+
`anacron` is discussed briefly here in reference to the `cron` "dot" directories. `anacron` is run by `cron`, and is advantageous for machines that are not up all the time, such as workstations and laptops. The reason for this is that while `cron` runs jobs on a schedule, if the machine is off when the job is scheduled, the job does not run. With `anacron` the job is picked up and run when the machine is on again, even if the scheduled run was in the past. `anacron` though, uses a more randomized approach to running tasks where the timing is not as exact. This can be a problem for things like backups that need to run at a specific time. That's where `cron` continues to provide the best solution for server administrators. All that being said, server administrators and desktop or laptop users can gain something from both approaches. You can easily mix and match based on your needs. For more information on `anacron` see [anacron - Automating commands](anacron.md).
28
29
29
-
Built into every Linux system including Rock Linux, for many versions now, the cron "dot" files help to automate processes quickly. These show up as directories that the cron system calls based on their naming conventions.
30
+
### <aname="starting-easy"></a>Starting Easy - The `cron` Dot Directories
30
31
31
-
In order to make something run during these auto-defined times, all you need to do is to copy your script file into the directory in question, and make sure it is executable. Here are the directories, and the times that they will run:
32
+
Built into every Linux system including Rock Linux, for many versions now, the `cron` "dot" files help to automate processes quickly. These show up as directories that the `cron` system calls based on their naming conventions. As noted in the introduction, these are now run by `cron` using `anacron` with the exception of `/etc/cron.hourly` which is still run directly by `cron`.
32
33
33
-
*`/etc/cron.hourly/` - Scripts placed here will run at 1 minute past the hour, every hour of every day.
34
-
*`/etc/cron.daily` - Scripts placed here will run at 4:02 AM every day.
35
-
*`/etc/cron.weekly` - Scripts placed here will run at 4:22 AM on Sunday every week.
36
-
*`/etc/cron.monthly` - Scripts placed here will run at 4:42 AM on the first day of the month, every month.
34
+
In order to make something run during these auto-defined times, all you need to do is to copy your script file into the directory in question, and make sure it is executable. Here are the directories:
37
35
38
-
So, provided you're alright with just letting the system auto-run your scripts at one of these pre-determined times, then it makes it very easy to automate tasks.
36
+
*`/etc/cron.hourly` - Scripts placed here will run one minute past the hour every hour.
37
+
*`/etc/cron.daily` - Scripts placed here will run every day. `anacron` adjusts the timing of these. (see note)
38
+
*`/etc/cron.weekly` - Scripts placed here will run every 7 days, based on the calendar day of the last run time. (see note)
39
+
*`/etc/cron.monthly` - Scripts placed here will run monthly based on the calendar day of the last run time. (see note)
39
40
40
-
## Create Your Own cron
41
+
!!! note
42
+
In a server environment, these are likely to be run at similar (but not exactly the same) times every day, week, and month, as the server will (hopefully) be up all the time. For more exact running times, see the @options below.
41
43
42
-
Of course, if the automated times don't work well for you for whatever reason, then you can create your own. In this example, we are assuming you are doing this as root. [see Assumptions](##-assumptions) To do this, type the following:
44
+
So, provided you're alright with just letting the system auto-run your scripts, and are alright with them simply running sometime during that period, then it makes it very easy to automate tasks.
45
+
46
+
### Create Your Own `cron`
47
+
48
+
Of course, if the automated, randomized times don't work well for you for whatever reason, then you can create your own. In this example, we are assuming you are doing this as root. [see Assumptions](##-assumptions) To do this, type the following:
43
49
44
50
`crontab -e`
45
51
46
-
This will pull up root user's crontab as it exists at this moment in your chosen editor, and may look something like this. Go ahead and read through this commented version, as it contains descriptions of each field that we will be using next:
52
+
This will pull up root user's `crontab` as it exists at this moment in your chosen editor, and may look something like this. Go ahead and read through this commented version, as it contains descriptions of each field that we will be using next:
47
53
48
54
```
49
55
# Edit this file to introduce tasks to be run by cron.
@@ -71,41 +77,47 @@ This will pull up root user's crontab as it exists at this moment in your chosen
71
77
# m h dom mon dow command
72
78
```
73
79
74
-
Notice that this particular crontab file has some of its own documentation built-in. That isn't always the case. When modifying a crontab on a container or minimalist operating system, the crontab will be an empty file, unless an entry has already been placed in it.
80
+
Notice that this particular `crontab` file has some of its own documentation built-in. That isn't always the case. When modifying a `crontab` on a container or minimalist operating system, the `crontab` will be an empty file, unless an entry has already been placed in it.
75
81
76
-
Let's assume that we have a backup script that we want to run at 10 PM at night. The crontab uses a 24 hour clock, so this would be 22:00. Let's assume that the backup script is called "backup" and that it is currently in the _/usr/local/sbin_ directory.
82
+
Let's assume that we have a backup script that we want to run at 10 PM at night. The `crontab` uses a 24 hour clock, so this would be 22:00. Let's assume that the backup script is called "backup" and that it is currently in the _/usr/local/sbin_ directory.
77
83
78
-
Note: Remember that this script needs to also be executable (`chmod +x`) in order for the cron to run it. To add the job, we would:
84
+
!!! note
85
+
Remember that this script needs to also be executable (`chmod +x`) in order for the `cron` to run it.
86
+
87
+
To add the job, we would:
79
88
80
89
`crontab -e`
81
90
82
-
"crontab" stands for "cron table" and the format of the file is, in fact, a loose table layout. Now that we are in the crontab, go to the bottom of the file and add your new entry. If you are using vi as your default system editor, then this is done with the following keys:
91
+
`crontab` stands for "cron table" and the format of the file is, in fact, a loose table layout. Now that we are in the `crontab`, go to the bottom of the file and add your new entry. If you are using vi as your default system editor, then this is done with the following keys:
83
92
84
93
`Shift : $`
85
94
86
95
Now that you are at the bottom of the file, insert a line and type a brief comment to describe what is going on with your entry. This is done by adding a "#" to the beginning of the line:
87
96
88
97
`# Backing up the system every night at 10PM`
89
98
90
-
Now hit enter. You should still be in the insert mode, so the next step is to add your entry. As shown in our empty commented crontab (above) this is **m** for minutes, **h** for hours, **dom** for day of month, **mon** for month, and **dow** for day of week.
99
+
Now hit enter. You should still be in the insert mode, so the next step is to add your entry. As shown in our empty commented `crontab` (above) this is **m** for minutes, **h** for hours, **dom** for day of month, **mon** for month, and **dow** for day of week.
91
100
92
101
To run our backup script every day at 10:00, the entry would look like this:
93
102
94
103
`00 22 * * * /usr/local/sbin/backup`
95
104
96
105
This says run the script at 10 PM, every day of the month, every month, and every day of the week. Obviously, this is a pretty simple example and things can get quite complicated when you need specifics.
97
106
98
-
### The @options for crontab
107
+
### The @options for `crontab`
99
108
100
-
As noted in the [Starting Easy](##-starting-easy) portion of this document above, scripts in the crondot directories are run at very specific times. @options offer the ability to use more natural timing. The @options consist of:
109
+
As noted in the [Starting Easy](##-starting-easy) portion of this document above, scripts in the `cron` "dot" directories are run at random times with `anacron` based on the calendar and clock, except for `cron.hourly`. @options offer the ability to use more natural timing. The @options consist of:
101
110
102
-
*`@hourly` runs the script every hour of every day at 0 minutes past the hour.
111
+
*`@hourly` runs the script every hour of every day at 0 minutes past the hour. (this is exactly the result of placing your script in `/etc/cron.hourly` too)
103
112
*`@daily` runs the script every day at midnight.
104
113
*`@weekly` runs the script every week at midnight on Sunday.
105
114
*`@monthly` runs the script every month at midnight on the first day of the month.
106
115
*`@yearly` runs the script every year at midnight on the first day of January.
107
116
*`@reboot` runs the script on system startup only.
108
117
118
+
!!! note
119
+
Using these `crontab` entries bypasses the `anacron` system and reverts to the `crond.service`. If you want daily, weekly, monthly, and yearly tasks to execute on this schedule, use a `crontab` entry as specified.
120
+
109
121
For our backup script example, if we used use the @daily option to run the backup script at midnight, the entry would look like this:
110
122
111
123
`@daily /usr/local/sbin/backup`
@@ -128,8 +140,8 @@ In the table, the commas let you specify individual entries within a field, whil
128
140
129
141
When determining when to run a script, you need to take time and plan it out, particularly if the criteria are complex.
130
142
131
-
# Conclusions
143
+
##Conclusions
132
144
133
-
The cron/crontab system is a very powerful tool for the Rocky Linux desktop user or systems administrator. It can allow you to automate tasks and scripts so that you don't have to remember to run them manually. For machines that are not on 24 hours a day, explore [anacron - Automating commands](anacron.md).
145
+
The _cron/crontab_ system is a very powerful tool for the Rocky Linux systems administrator or desktop user. It can allow you to automate tasks and scripts so that you don't have to remember to run them manually. For machines that are **not** on 24 hours a day, explore [anacron - Automating commands](anacron.md).
134
146
135
-
While the basics are pretty easy, you can get a lot more complex. For more information on crontab head up to the [crontab manual page](https://man7.org/linux/man-pages/man5/crontab.5.html). You can also simply do a web search for "crontab" which will give you a wealth of results to help you fine-tune your crontab skills.
147
+
While the basics are pretty easy, you can get a lot more complex. For more information on `crontab` head up to the [crontab manual page](https://man7.org/linux/man-pages/man5/crontab.5.html). You can also simply do a web search for "crontab" which will give you a wealth of results to help you fine-tune your `crontab` skills.
0 commit comments