How to implement a script every few days in crontab

Source: Internet
Author: User

At work, you often run into a script, or a command, every few days/hours/minutes. If it is every few hours, how many minutes to run the program, in the crontab may be a better implementation, the following are some examples and crontab format description:


Specific examples:

# 6 o'clock in the afternoon to the morning 6, every 15 minutes to execute a script
0,15,30,45 18-06 * * * */bin/bash $HOME/script.sh >/dev/null 2>&1

# Restart service every two hours
* */2 * * * */etc/init.d/apache2 restart

The following is a summary of how many minutes, every hour, daily/weekly/monthly/yearly crontab

0 0 1 1 *

If a script is executed every 10 days of the month, it can also be written as:

# 1th, 11th, 21st, 31st for each month, perform HTTP service restart

0 0 */10 * */etc/init.d/apache2 restart


But how do you do this if you're going to do it in natural days, say, every 27 days, a script? If this is the case, obviously can not be directly implemented through crontab, must be circuitous implementation. Here are two ways to think of it.

method One implements the control of time by scripting

Ideas:

1 set the number of times to execute in the script running.sh, if 10 times, each interval 27 days, script.sh for the script to be executed

2 then execute nohup bash running.sh & place it in the background for automatic execution


" method Two " is implemented indirectly through crontab

Ideas:

1 Execute the running.sh script regularly every day, and then judge in the script running.sh, when the time interval satisfies the condition

2 Execute the script.sh script while modifying the last execution time for the next execution


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.