+ 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: +
-
+
-
updatedbto update yourlocatedatabase
+ -
certbotto update renewing of your https certs
+
-
+
- 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
+
+ .---------------- 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
+
-
+
- /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: +
- Save that file +
- Make that executable: +
-
chmod +x /etc/cron.daily/backup
+
+
+ #!/bin/sh + + # the logger command generates system logs + logger "starting backup daily job" + + # backup is hypothetical backup command + backup + + logger "finished backing up" ++ + +
-
+
+ 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 +
- If you feel like donating: +
- XMR:
42ueuWXJGP48xtahfnujWDMFHJVdMGeFj85jxuwPBygkVw1GEBbHfTsDFBByTSLXpDV4bKcNr7EPCfvSJpyYLY5HPvqUmRB
+ - BTC:
36mXUtU6Kh9pmYvGYPLY7peHU4TR7FBATT
+
-
+
+ Yes I know I need to set up OpenAlias... +
+ + + + + + + + + + +