Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:base-system:cron [2021/02/09 18:36] – Table of shortcuts gandalfdocs: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:
-====== Scheduling tasks ======+====== Scheduling tasks with cron ======
 {{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}} {{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
  
 ===== Introduction ===== ===== Introduction =====
-  * OpenWrt allows to run jobs (programs, scripts) at specified times using [[wp>Cron|cron]] service. +  * OpenWrt can run scheduled tasks using [[wp>Cron|cron]] service. 
-  * This how-to describes the methods for setting up cron jobs.+  * This how-to describes the method for setting up cron jobs
 +  * See also [[docs:guide-user:advanced:watchcat|Watchcat]] to reboot based on schedule or connectivity.
  
-===== Web interface =====+===== Goals ===== 
 +  * Run programs or scripts at a specific time. 
 +  * Automate scheduled task management. 
 + 
 +===== Web interface instructions =====
 Set up cron jobs using web interface. Set up cron jobs using web interface.
-  - Navigate to **[[http://openwrt.lan/|LuCI]] -> System -> Scheduled Tasks**.+  - Navigate to **LuCI -> System -> Scheduled Tasks**.
   - Edit the configuration and click the **Save** button.   - Edit the configuration and click the **Save** button.
  
-===== Command-line interface =====+===== Command-line instructions =====
 Set up cron jobs using command-line interface. Set up cron jobs using command-line interface.
  
Line 27: Line 32:
 This will edit the configuraion ''/etc/crontabs/root'' file in [[docs:guide-user:base-system:user.beginner.cli#editing_files|vi editor]]. This will edit the configuraion ''/etc/crontabs/root'' file in [[docs:guide-user:base-system:user.beginner.cli#editing_files|vi editor]].
  
-:!: There should be a end-of-line character on the last line of the crontab file.+:!: There should be a EOL character on the last line of the crontab file.
 Just leave an empty line at the end to be sure. Just leave an empty line at the end to be sure.
  
 ===== Task specification ===== ===== Task specification =====
 Each line is a separate task written in the specification: Each line is a separate task written in the specification:
 +
 <code> <code>
 * * * * * command to execute * * * * * command to execute
Line 44: 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 ^ +^  min\\ 0-59  ^  hour\\ 0-23  ^  day/month\\ 1-31  ^  month\\ 1-12  ^  day/week\\ 0-6  ^ Description ^ 
-  */5                         | Every 5 minutes | + */5          | Every 5 minutes | 
-  12      */3  |                  | Every 3 hours at 12 minutes | + 12   */3  |       | Every 3 hours at 12 minutes | 
-  57      11       15    | 1,6,12 |     | At 11:57 Hrs  on 15th of Jan, June & Dec | + 57   11   15  | 1,6,12 |   | At 11:57 Hrs on 15th of Jan, June & Dec | 
-  25                      |  1-5  | At 6:25 AM every weekday (Mon-Fri) | + 25        |  1-5  | At 6:25 AM every weekday (Mon-Fri) | 
-           |  4,12,26          | At midnight on 4th, 12th and 26th of every month | +    |  4,12,26     | At midnight on 4th, 12th and 26th of every month | 
-|  5,10   |  9,14  |    10          |  0,4  | At 9:05AM, 9:10AM, 2:05PM and 2:10PM every Sunday and Thursday |+|  5,10  |  9,14  |  10    |  0,4  | At 9:05AM, 9:10AM, 2:05PM and 2:10PM every Sunday and Thursday |
  
 :!: 0 (zero) is treated as Sunday. :!: 0 (zero) is treated as Sunday.
-If you set the day of the week to 7, busybox will go bonkers and run your command every day.+If you set the day of the week to 7, BusyBox will go bonkers and run your command every day.
  
-Table of shortcuts : +**Table of shortcuts:** 
-Shortcut  Description Equivalent + 
-@reboot At system startup None +Shortcut Equivalent ^ Description ^ 
-@yearly  Every years 0 0 1 1 * +| ''@yearly'' | ''0 0 1 1 *'' | Every year | 
-@annually  Every years 0 0 1 1 * +| ''@annually'' | ''0 0 1 1 *'' | Every year | 
-@monthly  Every months 0 0 1 * * +| ''@monthly'' | ''0 0 1 * *'' | Every month | 
-@weekly  Every weeks 0 0 * * 0 +| ''@weekly'' | ''0 0 * * 0'' | Every week | 
-@daily  Every days 0 0 * * * +| ''@daily'' | ''0 0 * * *'' | Every day | 
-@midnight  Every days 0 0 * * * +| ''@midnight'' | ''0 0 * * *'' | Every day | 
-@hourly  Every hours 0 * * * *+| ''@hourly'' | ''0 * * * *'' | Every hour | 
 + 
 +:!:  Time shortcuts are not enabled by default. Shortcuts require compiling busybox with FEATURE_CROND_SPECIAL_TIMES enabled in the busybox compile options.
  
 ===== Troubleshooting ===== ===== Troubleshooting =====
Line 80: Line 88:
   * [[man>crontab(1)]], [[man>crontab(5)]]   * [[man>crontab(1)]], [[man>crontab(5)]]
   * [[https://busybox.net/downloads/BusyBox.html#crontab|BusyBox crontab]]   * [[https://busybox.net/downloads/BusyBox.html#crontab|BusyBox crontab]]
-  * [[http://adminschoice.com/crontab-quick-reference|Crontab – Quick Reference]]+  * [[http://adminschoice.com/crontab-quick-reference|Crontab quick reference]]
  
 ==== Periodic reboot ==== ==== Periodic reboot ====
Line 107: Line 115:
 There is usually no programmable circuitry to actually power off the unit. There is usually no programmable circuitry to actually power off the unit.
 ''reboot'' does work, in case you should want to reboot the router periodically. ''reboot'' does work, in case you should want to reboot the router periodically.
- 
-However, a more flexible approach is to use use the [[packages:pkgdata:watchcat|watchcat]] package. 
- 
-<code bash> 
-opkg update 
-opkg install watchcat luci-app-watchcat 
-</code> 
  
 ==== Periodic network restart ==== ==== Periodic network restart ====
Line 133: Line 134:
 59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx:xx:xx:xx:xx:xx 59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx:xx:xx:xx:xx:xx
 # crontab must end with the last line as space or a comment # crontab must end with the last line as space or a comment
 +</code>
 +
 +==== Keep number of configs / snapshots of config ====
 +To keep a number N of configurations/settings at the directory "/root", you can also use cron. This way you can access and restore configurations of the past 100 days. The cronjob can easily be extended to copy the config to another device, to be able to replicate a broken OpenWRT device from scratch.
 +
 +<code bash>
 +#Make a new backup/configurations snapshot at nighttime 00:01 am, keep last N=100 snapshots
 +#01 00 * * * sysupgrade -b "/root/backup-${HOSTNAME}.tar.gz" # <-- keep just one config
 +01 00 * * * find "/root" -type f -name "backup-*.tar.gz" | sort -r | awk 'NR > 100' | xargs rm -f; sysupgrade -b "/root/backup-${HOSTNAME}-$(date +\%Y-\%m-\%d-\%H-\%M-\%S).tar.gz"
 </code> </code>
  
  • Last modified: 2023/10/14 06:55
  • by vgaetera