MyTetra Share
Делитесь знаниями!
Linux crontab examples (every X minutes or hours)
Время создания: 27.12.2017 10:14
Текстовые метки: linux crontab cron
Раздел: Linux
Запись: Velonski/mytetra-database/master/base/1514351668zmfpb4aqcc/text.html на raw.githubusercontent.com

Linux crontab FAQ: How do I schedule Unix or Linux crontab jobs to run at intervals, like “Every five minutes,” “Every ten minutes,” “Every half hour,” and so on?


Solution: I’ve posted other Unix and Linux crontab tutorials here before (How to edit your Linux crontab file, Example Linux crontab file format), but I’ve never included a tutorial that covers the “every” options, so here are some examples to demonstrate this crontab syntax.

Linux crontab: How to run a command every minute


To run a Unix/Linux crontab command every minute, use this syntax:



# run this command every minute of every day to check apache

* * * * * /var/www/devdaily.com/bin/check-apache.sh


I created that crontab entry when I was having a problem with the Apache web server, and needed to run a shell script test every minute of every day to see that it was running properly.

Descriptions of the crontab date/time fields


All those * symbols are what make this command run every minute. Specifically, those first five fields have the following meanings:


# field # meaning allowed values

# ------- ------------ --------------

# 1 minute 0-59

# 2 hour 0-23

# 3 day of month 1-31

# 4 month 1-12 (or names, see below)

# 5 day of week 0-7 (0 or 7 is Sun, or use names)


I’m assuming that you have some previous knowledge of the crontab syntax in this tutorial, so I won’t discuss those fields too much, but what this shows is that in the first field you specify the “Minute” value, in the second field you specify the “Hour,” followed by Day Of Month, then Month, and finally Day Of Week. You’ll see more examples as we go along.

Run a crontab command every hour


To run a Linux/Unix crontab every hour of every day, you use a very similar syntax. Here’s a crontab entry I use to access a Drupal cron.php page five minutes after every hour using wget:


# hit this url to run the drupal cron process every hour of every day

# this command will run at 12:05, 1:05, etc.

5 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php


Run a crontab entry every day


Here’s a crontab example that shows how to run a command from the cron daemon once every day. In this command I run my backup scripts at 4:30 a.m. every day:


# run the backup scripts at 4:30am

30 4 * * * /var/www/devdaily.com/bin/create-all-backups.sh


Run a crontab entry every 5 minutes


There are a couple of ways to run a crontab entry every five minutes. First, here’s the brute force way:


0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/www/devdaily.com/bin/do-update.sh


That command works just fine, and there’s nothing technically wrong with it. But, the crontab syntax offers a shortcut for this situation. The crontab step syntax lets you use a crontab entry in the following format to run a Unix or Linux command every five minutes. I show this syntax in bold in this example:


# run this crontab entry every 5 minutes

*/5 * * * * /var/www/devdaily.com/bin/do-update.sh


That’s a nice convenience feature for situations like this. Here’s a nice blurb about the step command syntax from the crontab man page:


Step values can be used in conjunction with ranges. Following a range

with "<number>" specifies skips of the number’s value through the

range.


For example, "0-23/2" can be used in the hours field to specify

command execution every other hour (the alternative in the V7 standard

is "0,2,4,6,8,10,12,14,16,18,20,22").


Steps are also permitted after an asterisk, so if you want to say

"every two hours", just use "*/2".


Unix and Linux “crontab every” summary


I hope that’s enough crontab examples to help you run your own commands every minute, every 5 minutes, every hour, or every day, etc.


As usual, if you have any questions, comments, or your own crontab examples to share, just use the comment form below.

Так же в этом разделе:
 
MyTetra Share v.0.65
Яндекс индекс цитирования