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 [2019/09/30 12:30] – "periodic reboot" optimized, "activating cron" obsoleted vgaeteradocs: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:
-====== Cron and crontab ====== +====== Scheduling tasks with cron ====== 
-[[wp>Cron]] allows to run jobs (programs, scripts) at specified times.+{{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
  
-OpenWrt comes with a cron system by default, provided by busybox.+===== Introduction ===== 
 +  * OpenWrt can run scheduled tasks using [[wp>Cron|cron]] service. 
 +  * 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.
  
-===== Adding and editing cron jobs ===== +===== Goals ===== 
-You can edit the current config with:+  * Run programs or scripts at a specific time. 
 +  * Automate scheduled task management. 
 + 
 +===== 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.
  
 <code bash>  <code bash> 
 +# Edit configuration
 crontab -e  crontab -e 
-</code> 
  
-This will open ''/etc/crontabs/root'' file in ''vi'' editor. +# Show configuration 
-See [[docs:guide-user:base-system:user.beginner.cli|editing guide]] for details.+crontab -l
  
-:!: There should be a end-of-line character on the last line of the crontab file. +# Apply changes 
-If in doubt, just leave an empty line at the end.+/etc/init.d/cron restart 
 +</code>
  
-To see the current crontab:+This will edit the configuraion ''/etc/crontabs/root'' file in [[docs:guide-user:base-system:user.beginner.cli#editing_files|vi editor]].
  
-<code bash> +:!: There should be a EOL character on the last line of the crontab file. 
-crontab -l +Just leave an empty line at the end to be sure.
-</code>+
  
-==== 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 37: 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 ^ 
-|   */     *        *    |      |     | Every 5 minutes | 
-|   12    |   */ |      *    |      |     | Every 3 hours at 12 minutes | 
-|   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    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 | 
  
-<WRAP tip> +^  min\\ 0-59  ^  hour\\ 0-23  ^  day/month\\ 1-31  ^  month\\ 1-12  ^  day/week\\ 0-6  ^ Description ^ 
-0 (zerois treated as Sunday. +|  */5  |  *  |  *  |  *  |  *  | Every 5 minutes | 
-If you set the day of the week to 7busybox will go bonkers and run your command every day. +|  12  |  */3  |  *  |  *  |  *  | Every 3 hours at 12 minutes | 
-</WRAP>+|  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,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 |
  
-===== Logging ===== +:!0 (zero) is treated as Sunday. 
-You can read log messages with ''logread | grep cron''. Not all messages are logged, to increase logging change ''cronloglevel'' property in [[docs:guide-user:base-system:system_configuration|/etc/config/system]]+If you set the day of the week to 7, BusyBox will go bonkers and run your command every day.
  
-===== References ===== +**Table of shortcuts:** 
-  * [[http://linux.die.net/man/5/crontab|crontab manpage]] + 
-  [[http://adminschoice.com/crontab-quick-reference]] +^ Shortcut ^ Equivalent ^ Description ^ 
-  * see [[http://www.busybox.net/downloads/BusyBox.html]] for options of the Busybox implementation+| ''@yearly'' | ''0 0 1 1 *'' | Every year | 
 +| ''@annually'' | ''0 0 1 1 *'' | Every year | 
 +| ''@monthly'' | ''0 0 1 * *'' | Every month | 
 +| ''@weekly'' | ''0 0 * * 0'' | Every week | 
 +| ''@daily'' | ''0 0 * * *'' | Every day | 
 +| ''@midnight'' | ''0 0 * * *'' | Every day | 
 +| ''@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 ===== 
 +You can read log messages with: 
 + 
 +<code bash> 
 +logread -e cron 
 +</code> 
 + 
 +Not all messages are logged, to increase logging change ''[[docs:guide-user:base-system:system_configuration|cronloglevel]]'' option.
  
 ===== Extras ===== ===== Extras =====
-==== Logging ==== +==== References ==== 
-Each time a job is run by crona line is printed in OpenWrt logs+  * [[man>crontab(1)]][[man>crontab(5)]] 
-Run ''logread'' to check whether your jobs are correctly run.+  * [[https://busybox.net/downloads/BusyBox.html#crontab|BusyBox crontab]] 
 +  * [[http://adminschoice.com/crontab-quick-reference|Crontab quick reference]]
  
 ==== Periodic reboot ==== ==== Periodic reboot ====
-A simple solution for some hard-to-solve problems (memory leak, performance degradation, ...) is to reboot the router periodically, for instance every night.+A simple workaround for some hard-to-solve problems (memory leak, performance degradation, ...) is to reboot the router periodically, for instance every night.
  
 However, this is not as simple as it seems, because the router usually does not have a real-time clock. However, this is not as simple as it seems, because the router usually does not have a real-time clock.
Line 74: Line 100:
 So, in the boot process the clock gets set backwards a few seconds to that file's timestamp. So, in the boot process the clock gets set backwards a few seconds to that file's timestamp.
 Then cron starts and notices a few seconds later that the required boot moment has again arrived and reboots again... Then cron starts and notices a few seconds later that the required boot moment has again arrived and reboots again...
-(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.)+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.
  
 One solution for cron is to use a delay and touch a file in ''/etc'' before reboot. One solution for cron is to use a delay and touch a file in ''/etc'' before reboot.
Line 90: Line 116:
 ''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.
  
-Howevera more flexible approach is to use use the [[packages:pkgdata:watchcat|watchcat]] package.+==== Periodic network restart ==== 
 +A simple solution for restart all your network (lanwan and wifi) every 10 minutes is this:
  
 <code bash> <code bash>
-opkg update +*/10 * * * * /etc/init.d/network restart
-opkg install watchcat+
 </code> </code>
  
 ==== Alarm clock ==== ==== Alarm clock ====
-If you have [[wp>Daylight saving time]] you could write yourself a nice alarm clock ;-)+If you have [[wp>Daylight saving time|DST]] you could write yourself a nice alarm clock.
 When DST starts in central Europe, clocks advance from 02:00 CET to 03:00 CEST on last Sunday in March. 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. +Six day before that, you could make your WoL wake you 10 minutes earlier. 
-Later won't work, you'll be late ;-)+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. When DST ends in central Europe, clocks retreat from 03:00 CEST to 02:00 CET on last Sunday in October.
  
 <code bash> <code bash>
-#min hour day month dayofweek command +# min hour day month dayofweek command 
-59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx:xx:xx:xx:xx:xx # Mo +59 05 * * 1 /usr/bin/wol -h 192.168.1.255 xx:xx:xx:xx:xx:xx 
-#crontab must (as fstab) 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