Crontab usage example

20 Super Practical Crontab Use Cases

  1. Execute the task every day at 02:00
    0 2 * * * /bin/sh backup.sh
  1. Execute tasks every day at 5:00 and 17:00
    0 5,17 * * * /scripts/script.sh
  1. Execute a task every minute
    Usually, we don't have a script that needs to be executed every minute (silently think of 12306--)
     * * * * *  /scripts/script.sh
  1. Execute the task every Sunday at 17:00
    0 17 * * sun  /scripts/script.sh
  1. Execute a task every 10 minutes
    */10 * * * * /scripts/monitor.sh
  1. perform tasks in specific months
     * * * jan,may,aug * /script/script.sh
  1. perform tasks on specific days
    0 17 * * sun,fri /script/scripy.sh

    Execute tasks at 17:00 every Friday and Sunday

  1. Run the task on the first Sunday of a month
    0 2 * * sun  [ $(date +%d) -le 07 ] && /script/script.sh
  1. Execute a task every four hours
    0 */4 * * * /scripts/script.sh
  1. Perform tasks every Monday and Sunday
    0 4,17 * * sun,mon /scripts/script.sh
  1. Execute a task every 30 seconds
    We have no way to execute it directly by appealing a similar example, because the minimum is 1min. But we can do it as follows.
    * * * * * /scripts/script.sh
    * * * * *  sleep 30; /scripts/script.sh
  1. Multiple tasks configured in one command
    * * * * * /scripts/script.sh; /scripts/scrit2.sh
  1. Perform a task once a year
   @yearly /scripts/script.sh

   @yearly is something like "0 0 1 1 *". It is executed in the first minute of every year, usually we can send New Year's greetings with this.

  1. Perform a task once a month
  @yearly /scripts/script.sh
  1. Do a task once a week
  @yearly /scripts/script.sh
  1. Execute a task once a day
  @yearly /scripts/script.sh
  1. Execute a task every minute
  @yearly /scripts/script.sh
  1. Executed when the system restarts
  @reboot /scripts/script.sh
  1. A specific account to redirect
    cron results to If you need to send it to other users, you can do it in the following ways:
   # crontab -l
   MAIL=bob
   0 2 * * * /script/backup.sh
  1. Backup all cron commands to a text file
    This is a convenient and quick way to restore when we lose cron commands.
    The following is a small example of using this method to restore cron. (Just take a look~)
    First: check the current cron
  # crontab -l
  MAIL=rahul
  0 2 * * * /script/backup.sh



Author: LeeLom
Link: https://www.jianshu.com/p/d93e2b177814
Source:
The copyright of Jianshu belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326273593&siteId=291194637