How to add cron job on Asuswrt Merlin Wifi Router

See all Linux/UNIX networking related FAQ
How can I add or delete cron jobs on my wifi router powered by Asuswrt Merlin firmware? How do I add cron job on Asuswrt Merlin Wifi router using command line?

Introduction: Asuswrt-Merlin is an enhanced version of Asuswrt, the firmware used by Asus’s modern wifi routers. It is a third party alternative firmware for Asus routers, with a special emphasis on tweaks and fixes rather than radical changes or collecting as many features as possible.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Terminal/ssh
Prerequisites Asus router with Asuswrt Merlin firmware
OS compatibility *BSD Linux macOS Unix
Est. reading time 3 minutes

How to add cron job on Asuswrt Merlin Wifi Router

You need to use the cru command to list, delete, or add new cron jobs.

What is a cron on Linux?

Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tools in a Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.

cru command syntax to add, list, and delete cron jobs

The syntax is pretty simple:

Adding a new cron job

cru a {id} min hour day month week command
Where:

  • id – Unique ID for each cron job
  • min – Minute (0-59)
  • hour – Hours (0-23)
  • day – Day (0-31)
  • month – Month (0-12 [12 is December])
  • week – Day of the week(0-7 [7 or 0 is Sunday])
  • command – Script or command name to schedule.

List the cron jobs

Use the cru command as follows:
cru l

Delete the cron job

Try the cru command as follows:
cru d {id}

Examples

Let us add cron job on Asuswrt Merlin Wifi router to ping gateway every 1 hour, run:
cru a pinggw "0 * * * * /bin/ping -c 10 -q 192.168.2.254"
Above cron job will run /bin/ping command that sends only 10 pings and only display output at start.
To list current jobs run:
cru l
To delete current jobs run:
cru d pinggw
Verify it:
cru l
cru command to list delete add cron job on Asuswrt Merlin Wifi Router

How to scheduled tasks/ cron jobs after rebooting the router

Due to a small amount of storage on a router you need to store your cron job as a script using cru command.

Create a script called services-start

Type the following cd command:
cd /jffs/scripts
Edit/update a file named services-start file i.e. /jffs/scripts/services-start

#!/bin/sh
# This cript get called after all other system services 
# have been started at boot on router
# ---------------------------------------------------------
# Sample cron job to install after reboot
cru a pinggw "0 * * * * /bin/ping -c 10 -q 192.168.2.254"
 
# Run my usb backup script every day at midnight 
cru a backup "0 0 * * * /jffs/scripts/usb-backup-script.sh"

Sample /jffs/scripts/usb-backup-script.sh file displayed using the cat command:
cat /jffs/scripts/usb-backup-script.sh
Sample outputs:

#!/bin/sh
# Purpose: Backup data to connected external USB 3.x hard disk
# Author: Vivek Gite under GPL version 2.x or above
# -------------------------------------------------------------
DEST="/tmp/mnt/sda1"
nUSER="vivek"
nSERVER="192.168.1.200"
SRC="/backups"
# GRAB files from my NFS server to external USB connected to this router
rsync -az -Hh --delete --numeric-ids ${nUSER}@${nSERVER}:${SRC} ${DEST}

Make sure you set executable permission using the chmod command:
chmod a+rx /jffs/scripts/services-start
chmod a+rx /jffs/scripts/usb-backup-script.sh

Enable boot time support for /jffs/scripts/

Support for these scripts stored under /jffs/scripts/ must be enabled, under Administration -> System on the webui:
Enable user scripts

Conclusion

And there you have it, I just add cron job on Asuswrt Merlin Wifi Router. You can create scheduled tasks using the standard crond service that runs on the router. You can put your cron tasks in /var/spool/cron/crontabs/. The file must be named “admin” as this is the name of the system user. Note that this location resides in RAM, so you would have to put your cron script somewhere such as in the jffs partition such as /jffs/scripts. A boot time copy it to /var/spool/cron/crontabs/ using an init-start user script. However, a simple way to manage your cron jobs is through the included “cru” command as described here. You can then put your “cru” commands inside a user script to re-generate your cron jobs at boot time. See official project help page for more info here and here.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

5 comments… add one
  • Yaris T Jan 11, 2021 @ 16:09

    This was really clear and useful, thank you!

  • RB Apr 1, 2021 @ 13:44

    Ok. But it does not work on AiMesh system (main and 2 nodes)

  • skimpniff Apr 7, 2022 @ 7:09

    Nice write-up, thanks.

  • Bohdan Jan 19, 2023 @ 15:09

    Thanks a lot, very helpful!

  • Mikael Jan 27, 2024 @ 8:26

    Thanks, been thinking about this for a while but only managed to find overly complex and daunting methods for setting up a simple script to update the IP address of some domains that I own. I guess my Pi4B can now be put to use for other stuff :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.