How to add cron job on Asuswrt Merlin Wifi Router

last updated in Categories , , , ,

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.

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:

Add 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 cron jobs

cru l

Delete cron job

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.