diff options
| -rw-r--r-- | certbot.html | 8 | ||||
| -rw-r--r-- | cron.html | 174 | ||||
| -rw-r--r-- | maintenance.html | 2 |
3 files changed, 181 insertions, 3 deletions
diff --git a/certbot.html b/certbot.html index dded807..cabf6cd 100644 --- a/certbot.html +++ b/certbot.html @@ -97,7 +97,7 @@ ufw allow 443</code></pre> <p> Of course, you don't want to have to remember to log in to renew them every three months, so it's easy to tell the server to automatically run this command. - We will use a cronjob for this. Run the following command: + We will use a <a href="cron.html">cronjob</a> for this. Run the following command: </p> <pre><code>crontab -e</code></pre> @@ -110,7 +110,7 @@ ufw allow 443</code></pre> </aside> <p> - This <code>crontab</code> command will open up a file for editting. + This <code>crontab</code> command will open up a file for editing. A crontab is a list of commands that your operating system will run automatically at certain times. We are going to tell it to automatically try to renew our certificates every month so we never have to. </p> @@ -125,6 +125,10 @@ ufw allow 443</code></pre> Save the file and exit to activate this cronjob. </p> + <p> + For more on cron and crontabs please <a href="cron.html">click here!</a> + </p> + <span class=next><a href="html.html">Next: Use HTML to Make Simple Webpages</a></span> </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> diff --git a/cron.html b/cron.html new file mode 100644 index 0000000..6b12269 --- /dev/null +++ b/cron.html @@ -0,0 +1,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 users 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 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> + 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><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 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 + <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><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> diff --git a/maintenance.html b/maintenance.html index 5cdbb1a..264fe68 100644 --- a/maintenance.html +++ b/maintenance.html @@ -113,7 +113,7 @@ nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man /usr/share/nginx/modules-available/mod-stream.conf</code></pre> <p> - <code>updatedb</code> is an ideal candidate for a cronjob so you don't have to worry about running each time. + <code>updatedb</code> is an ideal candidate for a <a href="cron.html">cronjob</a> so you don't have to worry about running each time. For example, adding the following to your crontab will run <code>updatedb</code> every 30 minutes: </p> <pre><code>*/30 * * * * /usr/bin/updatedb</code></pre> |
