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
---
cron.html | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 197 insertions(+)
create mode 100644 cron.html
(limited to 'cron.html')
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:
+
+
updatedb to update your locate database
+
certbot to update renewing of your https certs
+
+ Some tasks that you might want to schedule may include:
+
+
Package updates - if you really just want to leave your server alone you can automated updating packages on your server
+
Backups - you may want to backup certain files every day and some every week, this is possible with cron
+
+
+ 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:
+
+
Your users crontab
+
System wide cron directories
+
+
+
+
+
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
+
+ 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:
+
+ Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below
+
+
/etc/cron.d This is a crontab like the ones that you create with crontab -e
+
/etc/cron.hourly
+
/etc/cron.daily
+
/etc/cron.weekly
+
/etc/cron.monthly
+
+
+
+
+ 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:
+
+
Create and edit the file /etc/cron.daily/backup/ :
+
+
vim /etc/cron.daily/backup
+
+
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:
+ 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
+
+
+
+
+ I am a (internet) LandChad, my website is here
+
+
+ Contact me about this article or just because here
+
+
+ I have a youtube channel where I may post some LandChad related content as well as other content that you will hopefully find interesting
+
- 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:
+ 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:
-
-
-
-
-
-
-
-
--
cgit v1.2.3
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:
-
-
Your users crontab
-
System wide cron directories
-
-
-
-
-
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:
- 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:
-
On the day of the week option, Sunday is 0 and counting up from there, Saturday will be 6.
+
* designates "everything". Our command above has a * in the day of month and month columns. This means it will run regardless of the day of the month or month.
+
The hour option uses 24 hour time. 3 = 3AM, while use 15 for 3PM.
+
+
+
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).
+
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:
-
-
Create and edit the file /etc/cron.daily/backup/ :
-
-
vim /etc/cron.daily/backup
-
-
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:
- 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.
-