introduction-to-crontab
LOGS
01:00:06 <ardchoille> #startmeeting Introduction to crontab
01:00:06 <zodbot> Meeting started Mon May 24 01:00:06 2010 UTC.  The chair is ardchoille. Information about MeetBot at http://wiki.debian.org/MeetBot.
01:00:06 <zodbot> Useful Commands: #action #agreed #halp #info #idea #link #topic.
01:00:13 <ardchoille> #meetingname introduction-to-crontab
01:00:13 <zodbot> The meeting name has been set to 'introduction-to-crontab'
01:00:30 <ardchoille> #topic Introduction
01:00:45 <ardchoille> Welcome everyone. I'm going to be talking about crontab and we'll do a tutorial a little later.
01:01:07 <ardchoille> Questions are welcome at any time
01:01:26 <ardchoille> #topic What is crontab?
01:01:40 <Spencer_tt> yeah what's behind the name crontab
01:01:50 <ardchoille> Crontab (CRON TABle) is a program that manipulates the CRON daemon, making it easy for users to schedule tasks and run programs at pre-determined intervals.
01:02:23 <ardchoille> A crontab can also be an ASCII text file which contains the schedule of cron entries to be run and at what times they are to be run.
01:02:57 <ardchoille> This can be quite useful. For example, you may have a personal temporary directory that you want cleaned out daily to keep from exceeding your quota.
01:03:30 <ardchoille> This is where cron scheduling comes into play.
01:03:55 <ardchoille> Note: If the computer system running the crontab is down, the crontab will not run.
01:04:17 <ardchoille> When the system comes back up, the crontab will resume its normal activity but will not go back and run the jobs that were missed due to the system being down.
01:05:39 <ardchoille> #topic What is cron?
01:06:00 <ardchoille> Cron runs as a crond daemon and searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd and loads them into memory.
01:06:33 <nirik> (leading question) what about /etc/crontab? how does that fit in?
01:08:12 <ardchoille> nirik: Have you some information on that? I didn't run into that as part of my research
01:08:44 <nirik> yeah, basically it's a system crontab file... it's for root jobs...
01:08:49 <nirik> it's better to use the user ones. ;)
01:08:57 <ardchoille> ah, ok
01:09:14 <ardchoille> Cron also searches for /etc/anacrontab and the files in the /etc/cron.d directory.
01:09:59 <ardchoille> Cron examines all stored crontabs, checking each command to see if it should be run in the current minute.
01:10:19 <N3LRX> Does'nt anacron take charge of cron jobs missed during downtime?
01:10:49 <ardchoille> It does
01:11:05 <ardchoille> The crond daemon must be running in order to execute defined tasks.
01:11:13 <evilfix> what are the user crontab files?
01:11:57 <ardchoille> evilfix: I'll get to that a little laterif that's ok
01:12:03 <evilfix> sure
01:12:18 <ardchoille> #topic Who can use crontab?
01:12:39 <ardchoille> You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow
01:13:05 <ardchoille> If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny
01:13:31 <ardchoille> If only cron.deny exists and is empty, all users can use crontab.
01:13:50 <ardchoille> If neither file exists, only the root user can use crontab.
01:14:19 <ardchoille> The allow/deny files consist of one username per line.
01:14:48 <ardchoille> #topic Crontab and its options
01:15:07 <ardchoille> The crontab command is used to install, remove or list the tables used to drive the cron daemon.
01:15:33 <ardchoille> Each user can have their own crontab, and though these files are in /var/spool/ , they are not intended to be edited directly.
01:16:21 <ardchoille> To edit a user crontab, use the 'crontab' command along with the desired option.
01:16:41 <ardchoille> Crontab has the following options:
01:17:00 <ardchoille> The -u option appends the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines "your" crontab
01:17:29 <ardchoille> The -l option causes the current crontab to be displayed on standard output.
01:18:02 <ardchoille> The -r option causes the current crontab to be removed.
01:18:32 <ardchoille> The -e option is used to edit the current crontab.
01:18:59 <ardchoille> The -i option modifies the -r option to prompt the user for a ’y/Y’ confirmation response before actually removing the crontab.
01:19:50 <ardchoille> The -s option will append the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing/replacement occurs.
01:20:37 <ardchoille> evilfix: I believe this next topic explains your question
01:20:49 <ardchoille> #topic How it works
01:21:12 <ardchoille> The system maintains a crontab for each user account.
01:21:37 <ardchoille> In order to edit or create a crontab, the user must use a text editor that is available on the system.
01:22:03 <ardchoille> The crontab must be opened with the command 'crontab' using the '-e' option.
01:22:43 <ardchoille> Each line in a crontab represents a separate crontab entry (hereafter referred to as a "cron job").
01:23:11 <ardchoille> #topic The crontab file
01:23:26 <ardchoille> Each of the sections in a cron job are separated by a space, with the final section containing one or more spaces.
01:24:31 <ardchoille> For a graphical representation of a cronjob, see this page:
01:24:37 <ardchoille> #link http://ardchoille42.fedorapeople.org/Classroom/crontab-layout.png
01:25:06 <ardchoille> No spaces are allowed within sections 1-5, only between them.
01:25:24 <ardchoille> Sections 1-5 are used to indicate when and how often the task is to be executed.
01:25:40 <ardchoille> Note: It is best to enter the full path to any commands, system or not, in the 6th field.
01:26:05 <ardchoille> This is how a cron job is laid out:
01:26:16 <ardchoille> minute (0 - 59), hour (0 - 23, 0 = midnight), day (1 - 31), month (1 - 12), weekday (0 - 6, 0 = Sunday), command
01:26:30 <ardchoille> Note: If used, the the asterisk (*) character in the first five fields represents all valid values for that field.
01:26:41 <ardchoille> Comments can be used in a crontab using the hash (#) character.
01:26:58 <ardchoille> #topic Examples
01:27:10 <ardchoille> 01 04 1 1 1 /usr/bin/some_command
01:27:18 <ardchoille> The above example will run /usr/bin/some_command at 4:01am (04:01) on any Monday which falls on January 1st.
01:27:46 <ardchoille> 45 10 1 * * /usr/bin/some_command
01:27:57 <ardchoille> Note the asterisks (*) in the month and weekday fields of this example.
01:28:14 <ardchoille> This example will run /usr/bin/some_command at 10:45am (10:45) on the 1st day of every month.
01:28:45 <ardchoille> Comma-separated values can be used to run more than one instance of a particular command within a time period:
01:28:56 <ardchoille> 01,31 04,17 1 1,6 * /usr/bin/some_command
01:29:12 <ardchoille> The above example will run /usr/bin/some_command at 01 and 31 past the hours of 4:00am (04:00) and 5:00pm (17:00) on the 1st day of every January and June.
01:29:45 <ardchoille> Dash-separated values can be used to run a command continuously:
01:29:51 <kc8hfi> is it necessary to put 2 digits together for each field?  i noticed you had 01 instead of just a 1
01:30:27 <ardchoille> I'm not really sure about that, I've always used two digits.
01:30:43 <ardchoille> Can someone chime in about that?
01:31:38 <ardchoille> I assume a single digit will work as in the next example
01:31:46 <ardchoille> 01 17 1-15 6 * /usr/bin/some_command
01:32:11 <ardchoille> The above example will run /usr/bin/some_command at 01 past the hour of 5:00pm (17:00) on the 1st through the 15th days of every June.
01:32:59 <ardchoille> Using a mix of the above values, we can, for example, use the following line as a valid cron job:
01:33:08 <ardchoille> 01,31 04,17 15-20 1,6 * /usr/bin/some_command
01:33:23 <ardchoille> The above example will run /usr/bin/some_command at 01 and 31 past the hours of 4:00am (04:00) and 5:00pm (17:00) on the 15th through the 20th days of every January and June.
01:33:59 <ardchoille> The DISPLAY environment variable can be used to run graphical apps from a cron job:
01:34:07 <ardchoille> 45 13 * * * env DISPLAY=:0.0 /usr/bin/some_graphical_app
01:34:22 <ardchoille> The above example will run /usr/bin/some_graphical_app daily at 1:45pm (13:45).
01:35:26 <ardchoille> #topic Setting up a cronjob
01:36:00 <ardchoille> We will now use crontab to setup a cron job to run the gnome calculator. Open your favorite terminal emulator and run crontab -e
01:36:19 <seemant> ardchoille: you can use a single digit
01:36:31 <ardchoille> seemant: Thank you
01:36:49 <ardchoille> kc8hfi: Single digits are acceptable
01:37:01 <ardchoille> Those of you using the default editor (vi/vim), press the "i" key to enter insert mode.
01:37:11 <ardchoille> Note: If you make a mistake in the vi editor, you can simply press the ESC key, type ":q!" (without quotes) to discard changes and exit the editor.
01:37:35 <ardchoille> On an empty line, enter the following cron job line but amend the first two fields to run the command about two minutes from now:
01:38:10 <ardchoille> 00 00 * * * env DISPLAY=:0.0 /usr/bin/gcalctool
01:38:43 <ardchoille> Substitute the 00 00 for 2 minutes from now and the current  hour
01:39:01 <ardchoille> Press the ESC key to exit insert mode, then type ":wq" (without the quotes, and don't forget the colon) to save and exit the editor.
01:39:18 <ardchoille> After you exit the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically and begin running.
01:40:01 <ardchoille> In a couple of minutes,the gnome calculator app should launch assuming there were no problems with your cronjob syntax
01:41:20 <evilfix> worked for me :)
01:42:51 <ardchoille> Did everyone get gnome calculator running?
01:43:10 <ardchoille> ok, moving on
01:43:19 <ardchoille> #topic Crontab error handling
01:43:36 <ardchoille> We will now use crontab to edit the gnome calculator cronjob we just created, but this time we will set an intentional error so you can see how the system handles crontab errors.
01:43:58 <ardchoille> Run crontab -e again to edit the current crontab
01:44:06 <ardchoille> Those of you using the default editor (or vim), press the "i" key to enter insert mode.
01:44:26 <ardchoille> On the line you entered earlier, replace the content in the hour field (second field) with the number 24 - the minute field doesn't matter:
01:44:36 <ardchoille> 00 24 * * * env DISPLAY=:0.0 /usr/bin/gcalctool
01:44:51 <ardchoille> Press the ESC key to exit insert mode, then type ":wq" (without the quotes, and don't forget the colon) to save and exit the editor.
01:45:05 <ardchoille> After you exit the editor, the modified crontab will be checked for accuracy.
01:45:13 <ardchoille> However, this time you should get an error explaining what is wrong as well as a prompt to allow you to fix it.
01:45:31 <ardchoille> The cause of the error here is "bad hour", there are only 24 hours in a day and you entered 24 in the hour field.
01:45:45 <ardchoille> Note: The minute, hour and weekday fields begin counting at 0, so "24" in the hour field would essentially be the 25th hour, outside that of a 24 hour day.
01:46:03 <ardchoille> Since we are now finished with the example gnome calculator cron job, you can use use the error prompt to remove the gnome calculator line from your crontab.
01:46:19 <ardchoille> from the error prompt, type a "y" (without quotes) to re-enter the editor.
01:46:33 <ardchoille> We won't be using insert mode this time so use the arrow keys to place the cursor on the /usr/bin/gcalctool line and type "dd" (without quotes) to delete the entire line.
01:46:57 <ardchoille> You shouldn't need to press the ESC key as we didn't enter insert mode this time. Once you have removed the gcalctool line, type ":wq" (without the quotes, and don't forget the colon) to save and exit the editor.
01:47:53 <ardchoille> Your crontab should now be in the same state it was before class started, assuming you didn't make any additions
01:47:54 <p3nguin> Hint: you can also type ZZ to save and exit vi/vim.
01:48:04 <nb> or :x
01:48:15 <ardchoille> Nice tips, thank you
01:48:23 <bapa> ZQ's also a shortcut for quitting without saving.
01:48:46 <ardchoille> Note: If you'd like an easy reminder of the cronjob syntax, you can add a comment line such as this to the top of your crontab:
01:48:53 <nb> bapa, so it's like :q! ?
01:49:15 <ardchoille> # min(0-59), hour(0-23), dom(1-31), mon(1-12), dow(0-6, Sunday=0), command
01:49:29 <ardchoille> The hash character "#" tells the system that this line is a comment and it can be left in the crontab permanently.
01:49:42 <ardchoille> #topic Related reading
01:49:51 <ardchoille> Please read the following if you'd like to learn more about cron and crontab:
01:49:56 <ardchoille> man 8 cron
01:50:02 <ardchoille> man 5 crontab
01:50:09 * nb suggests the use of #info
01:50:38 <ardchoille> notedthanks
01:50:49 <ardchoille> #topic Q and A
01:50:54 <ardchoille> That is all I have for this class. Any general questions?
01:51:26 <ardchoille> p3nguin , nb , bapa Thank you for those tips
01:51:52 <ardchoille> I read where someone used a command like this:
01:52:04 <ardchoille> /01 04 * * * command
01:52:21 <ardchoille> I tried using that forward slash and it didn't work on my f13 system
01:52:51 <ardchoille> It's supposed to mean "run every n minutes"
01:53:44 <ardchoille> I'm not sure if that's a feature of Paul Vixie's cron though
01:54:30 <p3nguin> */4 in the minute field will run it every 4 minutes.
01:54:46 <ardchoille> Ah, the article I read might have been a typo
01:55:02 <p3nguin> */1 would be the same as * anyway.
01:55:22 <ardchoille> yeah
01:57:07 <ardchoille> Anyone have anything else?
01:58:06 <ardchoille> Thank you all for attending
01:58:29 <ardchoille> #endmeeting