171 lines
7.7 KiB
HTML
171 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang=en>
|
|
<head>
|
|
<title>Cronjobs – diyhosting.bhh.sh</title>
|
|
<!--# include file=".nav.html" -->
|
|
</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>
|
|
<!--# include file=".footer.html" -->
|
|
</body>
|
|
</html>
|