1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
<!DOCTYPE html>
<html lang=en>
<head>
<title>Cronjobs – LandChad.net</title>
<meta charset="utf-8"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel='stylesheet' type='text/css' href='style.css'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='alternate' type='application/rss+xml' title='Land Chad RSS' href='/rss.xml'>
</head>
<body>
<header><h1>Using Cronjobs to run scheduled tasks</h1></header>
<nav></nav>
<main>
<p>
Cron is a service that lets you run scheduled tasks. These tasks are called <strong> cronjobs. </strong> If you have already followed the initial course you will have already used cron when you set up certbot.
</p>
<h2> What tasks would I want to schedule? </h2>
<p>
You can schedule anything! Some examples of what you might have done already include:
<ul>
<li><code>updatedb</code> to update your <code>locate</code> database</li>
<li><code>certbot</code> to update renewing of your https certs</li>
</ul>
Some tasks that you might <em>want</em> to schedule may include:
<ul>
<li>Package updates - if you really just want to leave your server alone you can automated updating packages on your server</li>
<li>Backups - you may want to backup certain files every day and some every week, this is possible with cron</li>
</ul>
<p>
And many more, anything you can do can be turned into a cronjob.
</p>
<h2>Basic Cronjobs</h2>
<p>
This the preferred method for personal tasks and scripts; it's also the easiest to get started with. Run the command <code>crontab -e</code> to access your user's crontab
</p>
<p>
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.
</p>
<p>
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.
</p>
<p>
Crontab expressions look like this <code>* * * * * command-to-run</code>
The five elements before the command tell when the command is supposed to be run automatically.
<p>
So for our Monday at 3:30 AM job we would do the following:
<p>
<pre><code> .---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12)
| | | | .---- day of week (0 - 6)
| | | | |
* * * * *
30 3 * * 1 apt -y update && apt -y upgrade</code></pre>
<h3>Some notes</h3>
<ul>
<li>On the day of the week option, Sunday is 0 and counting up from there, Saturday will be 6.</li>
<li><code>*</code> designates "everything". Our command above has a <code>*</code> in the day of month and month columns. This means it will run regardless of the day of the month or month.</li>
<li>The hour option uses 24 hour time. 3 = 3AM, while use 15 for 3PM.</li>
</ul>
<h3>More examples</h3>
<p>
Let's add another job, our backup job (for the purposes of this our backup command is just called <code>backup</code>).
We want to run <code>backup</code> 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 <code>crontab -e</code> This would mean our full crontab would look like this:
<pre><code>0 23 * * * backup</code></pre>
<h3>Consecutive times</h3>
<p>
Suppose we want a command to run every weekday.
We know we can put <code>1</code> (Monday), but we can also use <code>1-5</code>
to signify from day 1 (Monday) to day 5 (Friday).
</p>
<pre><code>0 6 * * 1-5 echo "Wakey, wakey, wagie!" >> /home/wagie/alarm</code></pre>
<p>The above <code>echo</code> command runs every Monday through Friday at 6:00AM.</p>
<h3>Non-consecutive times</h3>
<p>
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 by putting <code>1,15,20</code> for the day of the month argument:
</p>
<pre><code>0 12 1,15,20 * * /usr/bin/pay_bills_script</code></pre>
<h3>"Every X minutes/days/months"</h3>
<p>We can also easily run a command every several minutes or months, without specifying the specific times:</p>
<pre><code>*/15 * * * * updatedb</code></pre>
<p>This cronjob will run the <code>updatedb</code> command every 15 minutes.</p>
<h3>Beware of this Rookie Mistake Though...</h3>
<p>
Suppose you want to run a script once every other month.
You might be <em>tempted</em> write this:
</p>
<pre><code>* * * */2 *</code></pre>
<p>
That might <em>feel right</em>, but this script <em>will be running once every minute during that every other month</em>.
You should specify the first two arguments, because with <code>*</code> it will be running every minute and hour!
</p>
<pre><code>0 0 1 */2 *</code></pre>
<p>This makes the command run <em>only</em> at 0:00 (12:00AM) on the first day of every two months, which is what we really want.</p>
<p>
Consult the website <a href="https://crontab.guru">crontab.guru</a> for an intuitive and interactive tester of cronjobs.
</p>
<h2>User vs. Root Cronjobs</h2>
<p>
It is important to note that user accounts all have different cronjobs.
If you have a user account <code>chad</code> and edit his crontab with <code>crontab -e</code>,
the commands you add will be run as the <code>chad</code> user, not <code>root</code> or anyone else.
</p>
<p>
Bear in mind that if you need root access to run a particular command,
you will usually want to add it as root.
</p>
<h2>System-wide cron directories</h2>
<p>
<code>crontab -e</code> is the typical interface for adding cronjobs, but it's important to at least know that system-wide jobs are often stored in the file directory.
Some programs which need cronjobs will automatically install them in the following way.
</p>
<p>
Run the command <code>ls /etc/cron*</code> you should see a list of directories and there contents. The directories should be something like the below:
</p>
<ul>
<li>/etc/cron.d <em>This is a crontab like the ones that you create with</em> <code>crontab -e</code></li>
<li>/etc/cron.hourly</li>
<li>/etc/cron.daily</li>
<li>/etc/cron.weekly</li>
<li>/etc/cron.monthly</li>
</ul>
<p>
The directories cron.{hourly,daily,weekly,monthly} are where you can put <strong> scripts </strong> to run at those times. You don't put normal cron entries here. I prefer to use these directories for system wide jobs that don't relate to an individual user.
</p>
<h2>Contribution</h2>
<ul>
<li>Mark McNally -- <a href="https://mark.mcnally.je">website</a>, <a href="https://www.youtube.com/channel/UCMiInY8BhSUtCarO6uu6i_g">Youtube</a></li>
<li>Edits and examples by Luke</li>
</ul>
</main>
<footer><a href="https://landchad.net">LandChad.net</a></br>Because Everyone should be an Internet LandChad.</br><a href="index.html"><li><img src="pix/chad.gif" alt="chad"></li></a><a href="rss.xml"><li><img src="pix/rss.svg" alt="RSS"></li></a><a href="pix/btc.png"><li><img src="pix/btc.svg" alt="BTC"></li></a><a href="pix/xmr.png"><li><img src="pix/xmr.svg" alt="XMR"></li></a><a href="https://github.com/lukesmithxyz/landchad"><li><img src="pix/git.svg" alt="Github"></li></a></footer>
</body>
</html>
|