100 Useful Command-Line Utilities

by Oliver; 2014

82. crontab

crontab is a tool to automatically run a command or script at periodic intervals. The first hit on Google has a nice how-to about crontab: To quote directly from this source, you need to make a file of the form:

FieldRange of Values
minute0-59
hour0-23
day1-31
month1-12
day-of-week0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
command-line-to-executethe command to run along with the parameters to that command if any

Once you make a file of this form, you can run it. For example, if:
$ cat mycron.txt
45 10 * * * rm -r /full/path/trash/* > /full/path/out.o 2> /full/path/out.e
Then you run this as:
$ crontab mycron.txt
And it will empty the trash folder every day at 10:45 a.m.

Display your cron jobs:
$ crontab -l
One peculiar thing about cron is that, if you're on a shared computer system, your system administrator might be the one running cron in his name and, when you invoke cron, you won't start a process running under your name. For this reason, you have to use absolute paths in the commands you put in your cron job: cron doesn't know about the local folder from which you run the command.

<PREV   NEXT>