summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cron.html203
1 files changed, 104 insertions, 99 deletions
diff --git a/cron.html b/cron.html
index 985a5e8..6b12269 100644
--- a/cron.html
+++ b/cron.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang=en>
<head>
- <title>Cronjobs</title>
+ <title>Cronjobs &ndash; 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'>
@@ -13,7 +13,7 @@
<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.
+ 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>
@@ -31,73 +31,119 @@
And many more, anything you can do can be turned into a cronjob.
</p>
- <h2> How do I schedule things? </h2>
+ <h2>Basic Cronjobs</h2>
<p>
- You can schedule tasks in the following ways:
- <ol>
- <li> Your users crontab </li>
- <li> System wide cron directories </li>
- </ol>
-
-
-
- <h3> Your user crontab </h3>
-
- <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 users crontab
</p>
- <aside>
- <p>
- These crontabs are located at <code> /var/spool/cron/crontabs/ </code> If you wanted to you can edit these files instead of running <code> crontab -e </code>
- </p>
- </aside>
-
- <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 a Monday.
+
+ <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 a Monday.
</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>
+ 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:30AM job we would do the following:
<p>
- <pre>
- .---------------- minute (0 - 59)
+ <pre><code> .---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12
- | | | | .---- day of week (0 - 6)
+ | | | | .---- day of week (0 - 6)
| | | | |
* * * * *
-30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade
- </pre>
- <br>
-
- <br>
- <aside class=callout>
- <p>
- For day of the week field (the last field), the week starts on a Sunday, so Monday is <code> 1 </code>
- </p>
- </aside>
- <br>
- <aside>
- <p>
- The website <a href="crontab.guru">https://crontab.guru</a> is insanely helpful for figuring out specific cron timings
- <p> Run the command <code> cat /etc/crontab </code> to see a basic expalnation on how to write crontab timings </p>
- </p>
- </aside>
- <p>
- Lets 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>
- 30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade
-
- 0 23 * * * backup
- </pre>
-
- <h3> System wide cron directories </h3>
+30 3 * * 1 apt -y update && apt -y dist-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 my 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 very 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
@@ -110,60 +156,19 @@
</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. For example If we wanted to put our backup job in <code> /etc/cron.daily </code> we would do the following:
- <ol>
- <li> Create and edit the file <code> /etc/cron.daily/backup/ </code>:
- <ul>
- <li> <code> vim /etc/cron.daily/backup </code> </li>
- </ul>
- <li> Add the backup command in there, we can also add some logging because it's a full script. The full thing will look like this: </li>
- <p>
- <pre>
- #!/bin/sh
-
- # the logger command generates system logs
- logger "starting backup daily job"
-
- # backup is hypothetical backup command
- backup
-
- logger "finished backing up"
- </pre>
- </p>
- </li>
- <li> Save that file </li>
- <li> Make that executable: </li>
- <ul>
- <li> <code> chmod +x /etc/cron.daily/backup </code> </li>
- </ul>
- </ol>
-
- <p>
- It's that easy, no messing around with cron timers! A downside of this is that you can't choose when these run, most of the time they will run at ~6:30am every hour,day,week,month.
+ 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>
- <p>
- That's it! There is not much more to know about cron. Hopefully you now know the appropriate cron method and have the ability to correctly schedule your tasks.
- </p>
+ <h2>Contribution</h2>
- <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>
- <p>
- - Mark McNally -- <a href="https://mark.mcnally.je">website</a>, <a href="https://www.youtube.com/channel/UCMiInY8BhSUtCarO6uu6i_g">Youtube</a>
- </p>
-
</main>
<footer><a href="https://landchad.net">LandChad.net</a></br>Because Everyone should be an Internet LandChad.</br><li><a href="index.html"><img src="pix/chad.gif" alt="chad"></a></li><li><a href="rss.xml"><img src="pix/rss.svg" alt="RSS"></a></li><li><a href="pix/btc.png"><img src="pix/btc.svg" alt="BTC"></a></li><li><a href="pix/xmr.png"><img src="pix/xmr.svg" alt="XMR"></a></li><li><a href="https://github.com/lukesmithxyz/landchad"><img src="pix/git.svg" alt="Github"></a></footer>
</body>
</html>
-
-
-
-
-
-
-
-