Linux Demo: Scheduling Tasks with cron

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this demonstration we're going to talk about scheduling tasks using the cron Dame and now the 80 daemon can also be used to schedule tasks in the future but it has one glaring weakness and that is the fact that it can only run the jobs that you specify once in the future which really doesn't work very well for jobs that have to be run on a regular basis such as running a backup of your system every night the cron daemon works much better in this type of scenario using cron you can create a custom schedule for when the job should be run and then the cron daemon will automatically run that job for you according to the schedule that you specify now the reason this works is because the cron daemon which as you can see here is installed and running by default checks once every minute to see if there are any jobs that it should run and if there are it runs them so using the cron daemon you can configure either system cron jobs or you can define user specific cron jobs we're going to look at creating system cron jobs first I'm going to use the LS command here to view all the files and directories in the slash etsy directory that begin with cron now in order to create a system job you need to create a script containing all the necessary commands and put them in the appropriate cron directory notice that there are four directories here that you can use we have cron daily cron hourly bran monthly and cron weekly any scripts in the cron daily directory are run once a day any scripts in the hourly directory will be run once an hour any scripts in the monthly directory will be run once a month and any scripts in the weekly directory are run once a week for example notice in the daily directory there is a script called log rotate let's take a look at it there you can see that once a day because it's in the daily directory this command is going to be run and rotate our logs now you're not stuck with just the default scripts that are listed here you can create your own scripts and then add them to the appropriate directory for example if you had a script that you wanted to run every hour you would drop it into the cron hourly directory let's run the LS command again now in addition to these four directories that have a predefined schedule there's also another directory that you need to be aware of called cron D now cron D is used for system jobs whose schedule doesn't fit in one of these default directories for example you may need to run a particular job on a schedule that's not hourly daily or monthly but it's something else well you can define that in cron dot d now there's one critical thing you need to remember about cron D and that is the fact that you do not put scripts in cron D the way you do these other directories instead you add crontab files now crontab file is just a text file that contains a list of commands to be run by cron and each command along with its associated schedule is listed on a single line in the file so let's go ahead and look up one of these that's already been defined in cron d let's look at zero hourly you'll notice right away that zero hourly is not listed in green and the output of the LS command so we know automatically that that is not a script it's not an executable file the way these files are let's take a look at the zero hourly crontab file notice down here that we have a cron job defined in the zero hourly crontab file now there's a very specific syntax that needs to be followed when you define a cron tab file each line within the cron tab file is made up of specific fields this first field is the minutes field that specifies how many minutes past the hour that a particular command should be run in this case we're going to run the command specified at one minute after the hour the second field is the hour field right here the hour field specifies the hour of the day when the command should be run be aware that the cron daemon does prefer the 24-hour clock so you should use a value of 0 to 23 in this field in this case we actually have a star character in that field which means all anytime you see a star character in one of these fields it represents all possible values the next field is the day field which specifies the day of the month that the command should be run the next field specifies the month of the year when the command should be run the next field specifies the day of the week when the command should be run the next field specifies the name of the user that the command should be run as in this case we're going to run the command as root and then the last field specifies the exact command to be run in this example the run parts command will be used and what's it going to run it's going to run all of the scripts in /ft slash cron hourly right there so in this example at one minute after the hour of every hour of the day of every day of the month of every month of the year of every day of the week this command is going to be run and it's going to be run as the root user now you can define your own system jobs in a custom crontab file all you have to do is create the crontab file and drop it in the cron dot d directory and then the command that you specify will be run according to its associated schedule just remember that you have to use the same syntax as is used in this example file right here now in addition to creating system jobs each user on the system can create their own non system cron jobs each user can create their own individual crontab file that is unique to their account and it's stored in slash var slash slash cron instead of here in the / XE / cron D directory so in order to show you how to do this I'm going to X out of my root user account so I'm back to my our Tracy user account and I'm going to create a cron tab file for the our Tracy user I do this by typing on tab - e and I'm going to press the insert key so I can edit the file and for this scenario let's suppose that I want to create a backup of my user's home directory every day at 10:01 p.m. so in order to do this I enter a one for my minutes field then a tab and then I specify the hour when this job should run again cron likes the 24-hour clock instead of the 12-hour clock so to run at 10:00 p.m. I need to specify 20 - 22 - 12 would equal 10 or 10:00 p.m. but the job is going to run at 10:01 p.m. then we have to specify what day of the month we want it to run we're gonna run every day of the month then we have to specify which months of the year we want the command to run all of them and then we have to specify which days of the week we want it to run to want it to run Monday through Friday or every day let's just run it every day and then I have to specify the name of the command to run now we're going to create our backup using the tar utility so I have to specify slash bin slash guitar it is important to note that if you're using the tar command from the shell prompt you just typed our well that doesn't work with cron you have to specify the full path to the command you want to run in your crontab file if you don't know which directory that is use the which command to find out that information then I specify that I want to create a new tar file and I want it can be compressed using gzip compression and then we'll use the F command specify the name of the backup file we want to create I want to put it in /mnt slash private and I want the name of the file to be our Tracy home underscore back and I'll give it a dot tar.gz extension to indicate that it's at our archive it's been compressed using gzip and then have to specify what I want backed up I just want to backup my home directory tilde forward slash so I'll press escape and now enter exit to save my changes and exit the editor and the job is now submitted so at 10:01 tonight the tar command will run and create that backup file we can verify this by entering Bron tab - L it'll then read the crontab file for my user account and I'll put it here on the screen so we can see what jobs I have configured and when they're gonna run now if for some reason we decide we don't want to have this cron job running anymore we need to remove it one option would be to load crontab again and just erase it or you can simply enter on tab - r and when i do my entire cron tab file is removed so be aware before you run crontab - are that if there are other commands within that file that you want to keep you don't want to use cron tab - are because it deletes the entire file you should instead go in and use crontab e to edit out the specific lines but if you don't want any cron jobs at all defined for your user account just use the - R option to delete the cron tab file entirely that's it for this demonstration in this demo we talked about using the cron daemon to schedule jobs we talked about how to use cron to schedule system jobs and then we talked about how to use cron to define user specific jobs
Info
Channel: The Linux Man
Views: 3,264
Rating: 5 out of 5
Keywords: Linux
Id: K7noRc8qEfA
Channel Id: undefined
Length: 9min 11sec (551 seconds)
Published: Thu Nov 24 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.