From fa19378ef79eeeb322889a093aab00e805627c17 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Wed, 30 Jun 2021 15:17:56 -0400 Subject: cron examples added, tweaks, removal of some --- cron.html | 203 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 104 insertions(+), 99 deletions(-) (limited to 'cron.html') diff --git a/cron.html b/cron.html index 985a5e8..6b12269 100644 --- a/cron.html +++ b/cron.html @@ -1,7 +1,7 @@ - Cronjobs + Cronjobs – LandChad.net @@ -13,7 +13,7 @@

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

@@ -31,73 +31,119 @@ And many more, anything you can do can be turned into a cronjob.

-

How do I schedule things?

+

Basic Cronjobs

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

+ 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 + Crontab expressions look like this * * * * * command-to-run + The five elements before the command tell when the command is supposed to be run automatically.

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

-

- .---------------- minute (0 - 59)
+            
 .---------------- 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 
-			
-
- -
- -
- -

- 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

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

Some notes

+ + +

More examples

+

+ Let's 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: +

0 23 * * * backup
+ +

Consecutive times

+ +

+ Suppose we want a command to run every weekday. + We know we can put 1 (Monday), but we can also use 1-5 + to signify from day 1 (Monday) to day 5 (Friday). +

+ +
0 6 * * 1-5 echo "Wakey, wakey, wagie!" >> /home/wagie/alarm
+ +

The above echo command runs every Monday through Friday at 6:00AM.

+ +

Non-consecutive times

+ +

+ 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 1,15,20 for the day of the month argument: +

+ +
0 12 1,15,20 * * /usr/bin/pay_bills_script
+ +

"Every X minutes/days/months"

+ +

We can also easily run a command very several minutes or months, without specifying the specific times: +

+ +
*/15 * * * * updatedb
+ +

+ This cronjob will run the updatedb command every 15 minutes. +

+ +

Beware of this Rookie Mistake Though...

+ +

+ Suppose you want to run a script once every other month. + You might be tempted write this: +

+ +
* * * */2 *
+ +

+ That might feel right, but this script will be running once every minute during that every other month. + You should specify the first two arguments, because with * it will be running every minute and hour! +

+ +
0 0 1 */2 *
+ +

This makes the command run only at 0:00 (12:00AM) on the first day of every two months, which is what we really want.

+ +

+ Consult the website crontab.guru for an intuitive and interactive tester of cronjobs. +

+ +

User vs. Root Cronjobs

+ +

+ It is important to note that user accounts all have different cronjobs. + If you have a user account chad and edit his crontab with crontab -e, + the commands you add will be run as the chad user, not root or anyone else. +

+ +

+ Bear in mind that if you need root access to run a particular command, + you will usually want to add it as root. +

+ +

System-wide cron directories

+ +

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

Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below @@ -110,60 +156,19 @@

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

-

- 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

-

Contribution

+ -

- - Mark McNally -- website, Youtube -

-
- - - - - - - - -- cgit v1.2.3