캠핑과 개발

crontab 설명

개발/Linux2015. 5. 6. 18:37

crontab format:
{activation parameters} {command #1} ; {optional command #2} ; {optional command #3} ; {etc, commands end on the line ending}

Example:
* * * * * ./folding
This would cause folding to be launched every minute, from your home folder. Very bad, you would end up with 60 copies running by the end of an hour.

Commands are shell commands. Separate multiple commands with a semicolon ";" and end on the line ending.

Activation parameters:
@reboot = run at boot and reboot only
@yearly = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@annually = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@monthly = run at midnight on the first day of each month (equiv to 0 0 1 * *)
@weekly = run at midnight each Sunday (equiv to 0 0 * * 0)
@daily = run at midnight each day (equiv to 0 0 * * *)
@ midnight = run at midnight each day (equiv to 0 0 * * *)
@ hourly = run on the first second of every hour (equiv to 0 * * * *)
- or -
1 2 3 4 5 = specific time tags
- where -
1 = Minute (of hour) to activate [0-59]
2 = Hour (of day) to activate [0-23]
3 = Day (of month) to activate [1-31 ... 29,30,31 may not activate during all months]
4 = Month (of year) to activate [1-12 or 3-letter names "Jan,Feb,Mar"]
5 = Weekday to activate [0-7 or 3-letter names "Mon,Tue,Wed"]

If 3-letter names are used on Month/Weekday instead of numbers, they are case-insensitive. "Mon" and "mON" are equally acceptable. If numbers are used for the weekday, "0" and "7" are both Sunday and are interchangeable.

Time tags are separated by spaces. Do not use spaces within a tag, this will confuse cron. All five tags must be present. They are a logical AND of each other. There is another space between the last time tag and the first command.

A time tag can be a wildcard "*", which means "all". It can be one value, several values, a range, or a fractional range.

Examples for the Hour column:
8 = one value: execute in the 8 AM hour
5,6,9 = multiple values: execute in the 5, 6, and 9 AM hours
5-8 = range: execute in each hour between 5-8 AM (inclusive)
*/2 = fractional: execute in every other hour. 0 (midnight), 2AM, 4AM, 6AM, etc
3-12/3 = fractional range: execute in every third hour between 3AM and 12PM: 3AM, 6AM, 9AM, 12PM

Example:
5 */3 * * 1-5 cd "desktop/fold1"; ./fold &
This will launch on the 5-minute mark, every third hour, every day, every month, but only on days of the work week (Mon-Fri). It cd's to the Desktop/Folding folder #1, then launches the launch script in nohup mode so folding will keep running.