
Cron and crontab Scheduling stuff 1 cron • Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; • crontabs found are loaded into memory. • Cron also searches for /etc/crontab and the files in the /etc/cron.d/ directory, which are in a different format. • Cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. 2 Crontab The command to create/edit, list, and remove cron jobs is crontab. crontab [ -u user ] file crontab [ -u user ] { -l | -r | -e } We’ll talk about all these options throughout these slides 3 description • crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron. • Each user can have their own crontab, • Crontab files in /var, but cannot be edited directly. • To view your cron jobs (the flag is the letter ‘l’): crontab -l 4 description • If the -u option is given, it specifies the name of the user whose crontab is to be tweaked. • If this option is not given, crontab examines "your" crontab, i.e., the crontab of the person executing the command. • The command crontab file is used to install a new crontab from some named file • or standard input if the pseudo-filename '-' is given. 5 Updating a cron job • cron checks each minute to see if its spool directory's modtime (or the modtime on /etc/crontab) has changed, • if it has, cron will then examine the modtime on all crontabs and reload those which have changed. • Thus cron need not be restarted whenever a crontab file is modfied. • Note that the crontab command updates the modtime of the spool directory whenever it changes a crontab. 6 editing • To create/modify the cron jobs of the user as that you are currently logged in: To see the default editor on your system: crontab –e $ echo $EDITOR /usr/bin/vim • You will get the default editor if one is set. Otherwise will get an option. To set the default editor on your system: export VISUAL="/usr/bin/nano" crontab -u exampleuser -e export EDITOR="$VISUAL" • let's you create/modify the cron jobs of exampleuser. 7 editing • To delete all cron jobs of the user as that you're currently logged in: crontab –r • To delete all cron jobs of exampleuser: crontab -u exampleuser –r 8 Text files • If you have written your cron jobs to a text file, you can use the text file to create the cron jobs. • For example, let's assume you have created the text file /tmp/my_cron_jobs.txt with the following contents: 30 00 * * * /path/to/script • You can create a cron job from that file as follows: crontab /tmp/my_cron_jobs.txt this will overwrite all previously created cron jobs 9 Cron table format <minute> <hour> <day of month> <month> <day of week> <command> * * * * * Command_to_execute - - - - - There is a space between each item | | | | | | | | | +-- Day of week (0-7) (Sunday=0 or 7) or Sun, Mon, Tue,... | | | +---- Month (1-12) or Jan, Feb,... case doesn't matter | | +------ Day of month (1-31) | +-------- Hour (0-23) +---------- Minute (0-59) 10 Cron table format • The asterisk (*) operator specifies all possible values for a field. e.g. every hour or every day. • The comma (,) operator specifies a list of values, for example: "1,3,4,7,8". • The dash (-) operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6". • The slash (/) operator, can be used to skip a given number of values. • For example, "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; • "*" specifies 'every hour’ • but the "/3" means that only the first, fourth, seventh...and such values given by "*" are used. • Or “add 3 to previous value” 11 dates • The day of a command's execution can be specified by two fields: day of month, and day of week. • If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. • For example, 30 4 1,15 * 5 would cause a command to be run at 4:30h on the 1st and 15th of each month, plus every Friday. <minute> <hour> <day of month> <month> <day of week> <command> 12 Ranges • You can use ranges to define cron jobs: • Examples: 1,2,5,9 means every first, second, fifth, and ninth (minute, hour, month, ...). 0-4,8-12 • means all (minutes, hours, months,...) from 0 to 4 and from 8 to 12. <minute> <hour> <day of month> <month> <day of week> <command> 13 Ranges */5 • means every fifth (minute, hour, month, ...). 1-9/2 • is the same as 1,3,5,7,9. 1,7,25,47 */2 * * * command • means: run command every second hour in the first, seventh, 25th, and 47th minute. <minute> <hour> <day of month> <month> <day of week> <command> 14 Ranges • Ranges or lists of names are not allowed if you are using names instead of numbers for months and days • e.g., Mon-Wed is not valid <minute> <hour> <day of month> <month> <day of week> <command> 15 shortcuts • Instead of the first five fields, one of eight special strings may appear: string meaning ------ ------- @reboot Run once, at startup. @yearly Run once a year, "0 0 1 1 *". @annually (same as @yearly) @monthly Run once a month, "0 0 1 * *". @weekly Run once a week, "0 0 * * 0". @daily Run once a day, "0 0 * * *". @midnight (same as @daily) @hourly Run once an hour, "0 * * * *". 16 Variables in crontabs • You can also use name=value pairs in a crontab to define variables for the cron jobs: # use /bin/sh to run commands, instead of the default /bin/bash SHELL=/bin/sh # mail any output to exampleuser, no matter whose crontab this is MAILTO=exampleuser # set the PATH variable to make sure all commands in the crontab are found PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin * * * * * my_command unless you set a PATH variable in a crontab, always use full paths in the crontab to make sure commands are found and can be executed. 17 scripts • CGI scripts are executable by default, but other scripts are not. They may need to run through a parser. • Must put the path to the parser before the path of the script. 1* * * * * /usr/bin/php [path to php script] • To find where an interpreter lives use the which command: 1which php 18 Crontab output • Cron will email to the user all output of the commands it runs, to silence this, redirect the output to a log file or to /dev/null. • If you do this you will not see the output of your script • For most regularly running scripts this is fine; there is no output, just tasks 19 Crontab output The short answer is “you can’t without some effort” • If you do want to see the output of a script must redirect it. • First find out which terminal you’re logged in on using the tty command (every ssh login is assigned a terminal): barr@Comp390-WG1:~$ tty /dev/pts/0 This is the “terminal” (or device) that you’re logged into • Now redirect your script output to this device: * * * * * /home/barr/bin/hello.sh > /dev/pts/0 • Problem: every time you log in you could get a different terminal Other methods: set up email to send it to yourself; use DISPLAY if you have a GUI environment; etc. 20 Example 1 • To run /usr/bin/sample.sh at 12.59 every day and supress the output 59 12 * * * /home/simon /bin/sample.sh > /dev/null 2>&1 Redirect the stdout of cron to /dev/null 2> means redirect stderr &1 means redirect stderr to the same place as stream 1, e.g., the same place as stdout. <minute> <hour> <day of month> <month> <day of week> <command> 21 Example 1 (saving output in a file) • To run /usr/bin/sample.sh at 12.59 every day and supress the output 59 12 * * * /home/simon /bin/sample.sh >> /var/log/cron.log >> means append Redirects the stdout of cron to the file cron.log in the directory /var/log <minute> <hour> <day of month> <month> <day of week> <command> 22 Example 2 • When does this run? 0 21 * * * sample.sh 1>/dev/null 2>&1 <minute> <hour> <day of month> <month> <day of week> <command> 23 Example 2 • To run sample.sh everyday at 9pm (21:00) 0 21 * * * sample.sh 1>/dev/null 2>&1 <minute> <hour> <day of month> <month> <day of week> <command> 24 Example 3 • When does this run? 0 1 * * 2-7 sample.sh 1>/dev/null 2>&1 <minute> <hour> <day of month> <month> <day of week> <command> 25 Example 3 • To run sample.sh every Tuesday to Saturday at 1am (01:00) 0 1 * * 2-7 sample.sh 1>/dev/null 2>&1 <minute> <hour> <day of month> <month> <day of week> <command> 26 Example 4 • When does this run? 30 07,09,13,15 * * * sample.sh <minute> <hour> <day of month> <month> <day of week> <command> 27 Example 4 • To run sample.sh at 07:30, 09:30 13:30 and 15:30 30 07,09,13,15 * * * sample.sh <minute> <hour> <day of month> <month> <day of week> <command> 28 Example 5 • When does this execute * * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log Where does the output go? <minute> <hour> <day of month> <month> <day of week> <command> 29 Example 5 • execute /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per minute.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages32 Page
-
File Size-