User Tools

Site Tools


Cron and crontab

Cron allows to run jobs (programs, scripts) at specified times.

OpenWrt comes with a cron system by default, provided by busybox.

Adding and editing cron jobs

You can edit the current config with:

crontab -e 

This will open /etc/crontabs/root file in vi editor. See editing guide for details.

:!: 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.

To see the current crontab:

crontab -l

Task Specification

Each line is a separate task written in the specification:

* * * * * command to execute
- - - - -
| | | | |
| | | | ----- Day of week (0 - 6) (Sunday =0)
| | | ------- Month (1 - 12)
| | --------- Day (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Examples of time specification:

*/5 * * * * every 5 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.
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

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.

Activating cron

Cron is not enabled by default, so your jobs won't be run. To activate cron:

/etc/init.d/cron start
/etc/init.d/cron enable

:!: If there is no crontab defined (i.e. /etc/crontabs/ is empty), then cron won't start! Make sure you already defined cron jobs before trying to start cron.


You can read log messages with logread | grep cron. Not all messages are logged, to increase logging change cronloglevel property in /etc/config/system


Tips and tricks

Check that the jobs are run

Each time a job is run by cron, a line is printed in OpenWrt logs. Run logread to check whether your jobs are correctly run.

Periodic reboot of a router

A simple solution 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. This could lead to a never-ending loop of reboots.

In the boot process the clock is initially set by sysfixtime to the most recent timestamp of any file found in /etc. The most recent file is possibly a status file or config file, modified maybe 30 seconds before the reboot initiated by cron. 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… (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:

# Reboot at 4:30am every day
# Note: To avoid infinite reboot loop, wait 70 seconds
# and touch a file in /etc so clock will be set
# properly to 4:31 on reboot before cron starts.
30 4 * * * sleep 70 && touch /etc/banner && reboot

:!: On many platforms shutdown does not work; it will just halt the CPU but it won't power off the device. 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.

Periodically check of internet and reboot if needed

Sometimes router can works during days (even months), and then suddenly connection to the internet disappears. But router is working and we need just reboot it using web-interface.

In this case we can periodically check of the internet and reboot if needed.


Create bash script to ping some site and reboot the router if needed.

cat << "EOF" > /root/
# Prepare vars
DATE=$(date +%Y-%m-%d" "%H:%M:%S)
# Ping and reboot if need
ping -c3
if [ $? -eq 0 ]; then
    echo "ok" 
    echo "${DATE} - ok" >> $logFile
    echo "REBOOT" 
    echo "${DATE} - REBOOT" >> $logFile
# If the log file size is greater then 100KB (102400 bytes), then renew it.
logFileSize=$(wc -c "$logFile" | awk '{print $1}')
if [ $logFileSize -gt 102400 ]; then
    echo "Size of $logFile = $logFileSize bytes." 
    echo "Renew the log file" 
    mv -f $logFile "${logFile}.bk" 
chmod +x /root/

Add cron task to run this script every 15 minutes:

# Check every 15 minutes ping to
# If it is failed, then reboot the router
# Note: To avoid infinite reboot loop, wait 70 seconds and touch a file in /etc 
# so clock will be set properly on reboot before cron starts.
0,15,30,45 * * * * sleep 70 && touch /etc/banner && /root/

The logs will be in /root/pingRouterCheckReboot.log.

Alarm clock

If you have Daylight saving time 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. 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.

#min hour day month dayofweek command
59 05 * * 1 /usr/bin/wol -h xx:xx:xx:xx:xx:xx # Mo
#crontab must (as fstab) end with the last line as space or a comment
docs/guide-user/base-system/cron.txt · Last modified: 2019/05/04 20:53 by stokito