From 9af5424f70e3f84cd08bdc771304d1c200c38c6c Mon Sep 17 00:00:00 2001 From: Mark McNally Date: Wed, 30 Jun 2021 18:01:49 +0100 Subject: Added a articel on how to use cron. Added links to this article on current articels that mention cron --- certbot.html | 8 ++- cron.html | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ maintenance.html | 2 +- 3 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 cron.html 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

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 cronjob for this. Run the following command:

crontab -e
@@ -110,7 +110,7 @@ ufw allow 443

- This crontab command will open up a file for editting. + This crontab 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.

@@ -125,6 +125,10 @@ ufw allow 443 Save the file and exit to activate this cronjob.

+

+ For more on cron and crontabs please click here! +

+ Next: Use HTML to Make Simple Webpages diff --git a/cron.html b/cron.html new file mode 100644 index 0000000..cc0ac0d --- /dev/null +++ b/cron.html @@ -0,0 +1,197 @@ + + + + Cronjobs + + + + + + + +

Using Cronjobs to run scheduled tasks

+ +
+

+ Cron is a service that lets you run scheduled tasks. These tasks are called cronjobs. If you have already followed the initial course you will have already used cron when you set up certbot. +

+

What tasks would I want to schedule?

+

+ You can schedule anything! Some examples of what you might have done already include: +

+ Some tasks that you might want to schedule may include: + +

+ And many more, anything you can do can be turned into a cronjob. +

+ +

How do I schedule things?

+ +

+ You can schedule tasks in the following ways: +

    +
  1. Your users crontab
  2. +
  3. System wide cron directories
  4. +
+ + + +

Your user crontab

+ +

+ This the preferred method for personal tasks and scripts, it's also the easiest to get started with. Run the command crontab -e to access your users crontab +

+ + +

+ 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. +

+

+ 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. +

+

+ Crontab expressions look like this * * * * * command-to-run +

+				 .---------------- minute (0 - 59)
+				 |  .------------- hour (0 - 23)
+				 |  |  .---------- day of month (1 - 31)
+				 |  |  |  .------- month (1 - 12
+				 |  |  |  |  .---- day of week (0 - 6) 
+				 |  |  |  |  |
+				 *  *  *  *  *
+			
+
+ + +

+ So for our Monday at 3:30AM job we would do the following: +

+             30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 
+            
+
+ +
+ +

+ Lets add another Job, our backup job (for the purposes of this our backup command is just called backup We want to run backup 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 crontab -e This would mean our full crontab would look like this: +

+             30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 
+             0 23 * * * backup 
+            
+ +

System wide cron directories

+ +

+ Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below +

+ +

+ + The directories cron.{hourly,daily,weekly,monthly} are where you can put scripts 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 /etc/cron.daily we would do the following: +

    +
  1. Create and edit the file /etc/cron.daily/backup/ : +
      +
    • vim /etc/cron.daily/backup
    • +
    +
  2. 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:
  3. +

    +

    + #!/bin/sh
    +
    + # the logger command generates system logs
    + logger "starting backup daily job"
    + 
    + # backup is hypothetical backup command
    + backup 
    +
    + logger "finished backing up"
    +                            
    +

    + +
  4. Save that file
  5. +
  6. Make that executable:
  7. +
      +
    • chmod +x /etc/cron.daily/backup
    • +
    +
+ +

+ 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. +

+ +

+ 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. +

+ +

Contribution

+ +

+ This article was written by Mark McNally +

+ + +

+ Yes I know I need to set up OpenAlias... +

+ + + + + + + + + + +
+ + + + + + + + + + + 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

- updatedb is an ideal candidate for a cronjob so you don't have to worry about running each time. + updatedb is an ideal candidate for a cronjob so you don't have to worry about running each time. For example, adding the following to your crontab will run updatedb every 30 minutes:

*/30 * * * * /usr/bin/updatedb
-- cgit v1.2.3 From acc74124b5152525078309c2b51ddd7901673239 Mon Sep 17 00:00:00 2001 From: Mark McNally Date: Wed, 30 Jun 2021 18:42:45 +0100 Subject: cron timings explained better, personal ad made less overt --- cron.html | 62 +++++++++++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) diff --git a/cron.html b/cron.html index cc0ac0d..985a5e8 100644 --- a/cron.html +++ b/cron.html @@ -61,25 +61,21 @@

Crontab expressions look like this * * * * * command-to-run +

+ So for our Monday at 3:30AM job we would do the following: +

-				 .---------------- minute (0 - 59)
-				 |  .------------- hour (0 - 23)
-				 |  |  .---------- day of month (1 - 31)
-				 |  |  |  .------- month (1 - 12
-				 |  |  |  |  .---- day of week (0 - 6) 
-				 |  |  |  |  |
-				 *  *  *  *  *
+ .---------------- minute (0 - 59)
+ | .------------- hour (0 - 23)
+ | | .---------- day of month (1 - 31)
+ | | | .------- month (1 - 12
+ | | | | .---- day of week (0 - 6) 
+ | | | | |
+ * * * * *
+30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 
 			

- -

- So for our Monday at 3:30AM job we would do the following: -

-             30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 
-