Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
| docs:guide-user:base-system:cron [2019/05/04 14:03] – [Adding and editing cron jobs] stokito | docs:guide-user:base-system:cron [2023/08/08 11:30] – Added my cronjob to keep last N snapshots of config torxgewinde | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| + | {{section> | ||
| - | [[wp> | + | ===== Introduction ===== |
| + | * OpenWrt can run scheduled tasks using [[wp> | ||
| + | * This how-to describes the method for setting up cron jobs. | ||
| + | * See also [[docs: | ||
| - | OpenWrt comes with a cron system by default, provided by busybox. | + | ===== Goals ===== |
| + | * Run programs or scripts at a specific time. | ||
| + | * Automate scheduled task management. | ||
| - | ===== Adding and editing | + | ===== Web interface instructions ===== |
| + | Set up cron jobs using web interface. | ||
| + | - Navigate to **LuCI -> System -> Scheduled Tasks**. | ||
| + | - Edit the configuration and click the **Save** button. | ||
| + | |||
| + | ===== Command-line instructions ===== | ||
| + | Set up cron jobs using command-line interface. | ||
| - | You can edit the current config with: | ||
| <code bash> | <code bash> | ||
| + | # Edit configuration | ||
| crontab -e | crontab -e | ||
| - | </ | ||
| - | This will open ''/ | + | # Show configuration |
| + | crontab | ||
| - | :!: There should be a end-of-line character on the last line of the crontab file. If in doubt, just leave an empty line at the end. | + | # Apply changes |
| + | /etc/init.d/cron restart | ||
| + | </ | ||
| + | This will edit the configuraion ''/ | ||
| - | To see the current | + | :!: There should be a EOL character on the last line of the crontab |
| - | <code bash> | + | Just leave an empty line at the end to be sure. |
| - | crontab -l | + | |
| - | </ | + | |
| + | ===== Task specification ===== | ||
| + | Each line is a separate task written in the specification: | ||
| - | each line is a separate task written in the specification: | ||
| < | < | ||
| * * * * * command to execute | * * * * * command to execute | ||
| - - - - - | - - - - - | ||
| | | | | | | | | | | | | ||
| - | | | | | ----- Day of week (0 - 6) (Sunday =0 or =7) | + | | | | | ----- Day of week (0 - 6) (Sunday =0) |
| | | | ------- Month (1 - 12) | | | | ------- Month (1 - 12) | ||
| | | --------- Day (1 - 31) | | | --------- Day (1 - 31) | ||
| Line 36: | Line 50: | ||
| Examples of time specification: | Examples of time specification: | ||
| - | |||
| - | ^ min\\ 0-59 ^ hour\\ 0-23 ^ day/month\\ 1-31 ^ month\\ 1-12 ^ day/week\\ 0-6 ^ Description | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | | ||
| - | | 5,10 | ||
| - | <WRAP left round tip 70%> | + | ^ min\\ 0-59 ^ hour\\ 0-23 ^ |
| - | 0 (zero) is treated as Sunday. If you set the day of the week to 7, busybox will go bonkers | + | | */5 | * | * | * | * | Every 5 minutes | |
| - | </ | + | | 12 | */3 | * | * | * | Every 3 hours at 12 minutes | |
| - | ===== Activating cron ===== | + | | 57 | 11 | 15 | 1,6,12 | * | At 11:57 Hrs on 15th of Jan, June & Dec | |
| + | | 25 | 6 | * | * | 1-5 | At 6:25 AM every weekday (Mon-Fri) | | ||
| + | | 0 | 0 | 4, | ||
| + | | 5,10 | 9,14 | 10 | * | 0,4 | At 9:05AM, 9:10AM, 2:05PM and 2:10PM every Sunday and Thursday | | ||
| - | Cron is not enabled by default, so your jobs won't be run. To activate cron: | + | :!: 0 (zero) |
| + | If you set the day of the week to 7, BusyBox will go bonkers and run your command every day. | ||
| - | <code bash> | + | **Table of shortcuts: |
| - | / | + | |
| - | / | + | |
| - | </ | + | |
| - | :!: If there is no crontab defined (i.e. '' | + | ^ Shortcut ^ Equivalent ^ Description ^ |
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| - | ===== References ===== | + | :!: Time shortcuts are not enabled by default. Shortcuts require compiling busybox with FEATURE_CROND_SPECIAL_TIMES enabled in the busybox compile options. |
| - | * [[http:// | + | ===== Troubleshooting ===== |
| - | * [[http:// | + | You can read log messages with: |
| - | * see [[http:// | + | |
| - | ===== Tips and tricks ===== | + | <code bash> |
| + | logread -e cron | ||
| + | </ | ||
| - | ==== Check that the jobs are run ==== | + | Not all messages |
| - | Each time a job is run by cron, a line is printed in OpenWrt logs. | + | ===== Extras ===== |
| + | ==== References ==== | ||
| + | * [[man> | ||
| + | * [[https:// | ||
| + | * [[http:// | ||
| - | ==== Periodic reboot | + | ==== Periodic reboot ==== |
| + | A simple workaround for some hard-to-solve problems (memory leak, performance degradation, | ||
| - | A simple | + | However, this is not as simple |
| + | This could lead to a never-ending loop of reboots. | ||
| - | However, this is not as simple as it seems, because | + | In the boot process the clock is initially set by '' |
| + | The most recent file is possibly | ||
| + | So, in the boot process the clock gets set backwards a few seconds | ||
| + | Then cron starts and notices | ||
| + | At the end of the boot process ntpd starts, and it may also take a while before ntpd gets and sets the correct time, so cron may start the reboot in between. | ||
| - | In the boot process the clock is initially set by '' | + | One solution for cron is to use a delay and touch a file in '' |
| - | + | ||
| - | One solution for cron is to use a delay and touch a file in /etc before reboot: | + | |
| <code bash> | <code bash> | ||
| Line 89: | Line 112: | ||
| </ | </ | ||
| - | :!: On many platforms '' | + | :!: On many platforms '' |
| + | There is usually no programmable circuitry to actually power off the unit. | ||
| + | '' | ||
| - | ==== Periodically check of internet and reboot if needed | + | ==== Periodic network restart |
| - | + | A simple solution for restart all your network | |
| - | Sometimes router can works during days (even months), and then suddenly connection to the internet disappears. But router | + | |
| - | + | ||
| - | In this case we can periodically check of the internet and reboot if needed. | + | |
| - | + | ||
| - | **Solution:** | + | |
| - | + | ||
| - | Create bash script to ping some site and reboot the router if needed. | + | |
| <code bash> | <code bash> | ||
| - | cat << " | + | */10 * * * * /etc/init.d/network restart |
| - | #!/bin/sh | + | </code> |
| - | # Prepare vars | + | ==== Alarm clock ==== |
| - | DATE=$(date +%Y-%m-%d" | + | If you have [[wp> |
| - | logFile="/ | + | When DST starts in central Europe, clocks advance from 02:00 CET to 03:00 CEST on last Sunday in March. |
| + | Six day before that, you could make your WoL wake you 10 minutes earlier. | ||
| + | Later won't work, you'll be late. | ||
| + | When DST ends in central Europe, clocks retreat from 03:00 CEST to 02:00 CET on last Sunday in October. | ||
| - | # Ping and reboot if need | ||
| - | ping -c3 openwrt.org | ||
| - | |||
| - | if [ $? -eq 0 ]; then | ||
| - | echo " | ||
| - | echo " | ||
| - | else | ||
| - | echo " | ||
| - | echo " | ||
| - | reboot | ||
| - | fi | ||
| - | |||
| - | # If the log file size is greater then 100KB (102400 bytes), then renew it. | ||
| - | logFileSize=$(wc -c " | ||
| - | if [ $logFileSize -gt 102400 ]; then | ||
| - | echo "Size of $logFile = $logFileSize bytes." | ||
| - | echo "Renew the log file" | ||
| - | |||
| - | mv -f $logFile " | ||
| - | fi | ||
| - | EOF | ||
| - | |||
| - | chmod +x / | ||
| - | </ | ||
| - | |||
| - | Add cron task to run this script every 15 minutes: | ||
| <code bash> | <code bash> | ||
| - | # Check every 15 minutes ping to google.com | + | # min hour day month dayofweek command |
| - | # If it is failed, then reboot the router | + | 59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx: |
| - | # Note: To avoid infinite reboot loop, wait 70 seconds and touch a file in /etc | + | # crontab must end with the last line as space or a comment |
| - | # so clock will be set properly on reboot before cron starts. | + | |
| - | 0, | + | |
| </ | </ | ||
| - | The logs will be in ''/ | + | ==== Keep number of configs / snapshots of config |
| - | ==== Alarm clock ==== | + | To keep a number N of configurations/ |
| - | + | ||
| - | If you have [[wp> | + | |
| - | When DST starts in central Europe, clocks advance from 02:00 CET to 03:00 CEST on last Sunday in March. | + | |
| - | Six day before that, you could make your WOL wake you 10 minutes earlier. Later won't work, you' | + | |
| - | When DST ends in central Europe, clocks retreat | + | |
| <code bash> | <code bash> | ||
| - | #min hour day month dayofweek command | + | #Make a new backup/ |
| - | 59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx: | + | #01 00 * * * sysupgrade -b "/root/backup-${HOSTNAME}.tar.gz" |
| - | #crontab must (as fstab) end with the last line as space or a comment | + | 01 00 * * * find "/ |
| </ | </ | ||