cronand crontab - microsoft azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf ·...

32
Cron and crontab Scheduling stuff 1

Upload: others

Post on 31-May-2020

8 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Cron and crontabScheduling stuff

1

Page 2: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 3: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 4: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 5: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 6: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 7: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

editing

• To create/modify the cron jobs of the user as that you are currently logged in:

crontab –e• You will get the default editor if one is set.

Otherwise will get an option.

crontab -u exampleuser -e• let's you create/modify the cron jobs

of exampleuser.

To see the default editor on your system:

$ echo $EDITOR/usr/bin/vim

To set the default editor on your system:

export VISUAL="/usr/bin/nano"export EDITOR="$VISUAL"

7

Page 8: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 9: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 10: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Cron table format

* * * * * Command_to_execute

- - - - -

| | | | |

| | | | +-- Day of week (0-7) (Sunday=0 or 7) or Sun, Mon, Tue,...

| | | +---- Month (1-12) or Jan, Feb,...

| | +------ Day of month (1-31)

| +-------- Hour (0-23)

+---------- Minute (0-59)

<minute> <hour> <day of month> <month> <day of week> <command>

case doesn't matter

There is a space between each item

10

Page 11: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 12: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 13: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Ranges

• You can use ranges to define cron jobs:• Examples:1,2,5,9means 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

Page 14: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 15: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 16: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 17: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 18: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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.

• To find where an interpreter lives use the which command:

1* * * * * /usr/bin/php [path to php script]

1which php

18

Page 19: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 20: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Crontab output

• 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

• 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

This is the “terminal” (or device) that you’re logged into

The short answer is “you can’t without some effort”

Other methods: set up email to send it to yourself; use DISPLAY if you have a GUI environment; etc. 20

Page 21: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 1

• To run /usr/bin/sample.sh at 12.59 every day and supress the output59 12 * * * /home/simon /bin/sample.sh > /dev/null 2>&1

2> means redirect stderrRedirect the stdout of cron to /dev/null

&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

Page 22: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 1 (saving output in a file)

• To run /usr/bin/sample.sh at 12.59 every day and supress the output59 12 * * * /home/simon /bin/sample.sh >> /var/log/cron.log

>> means appendRedirects 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

Page 23: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 24: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 25: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 26: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

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

Page 27: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 4

• When does this run?30 07,09,13,15 * * * sample.sh

<minute> <hour> <day of month> <month> <day of week> <command>

27

Page 28: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 4

• To run sample.sh at 07:30, 09:30 13:30 and 15:3030 07,09,13,15 * * * sample.sh

<minute> <hour> <day of month> <month> <day of week> <command>

28

Page 29: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 5

• When does this execute

* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log

<minute> <hour> <day of month> <month> <day of week> <command>

Where does the output go?

29

Page 30: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 5

• execute /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per minute.

* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log

<minute> <hour> <day of month> <month> <day of week> <command>

Output is discared, error stream is appended into the cron.log file

30

Page 31: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 6

• When does this script execute?

30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log

<minute> <hour> <day of month> <month> <day of week> <command>

31

Page 32: Cronand crontab - Microsoft Azureclasses.eastus.cloudapp.azure.com/.../lecture/cron.pdf · description •crontabis the program used to install, deinstallor list the tables used to

Example 6

• execute /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per day at 00:30h.

30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log

<minute> <hour> <day of month> <month> <day of week> <command>

32