Tuesday, August 4, 2015

How to schedule cron jobs on an ASUS wireless router

Want to run a task on a regular schedule on your ASUS wireless router? Well, you're out of luck.

Or are you?

Cron is the well-known method of scheduling tasks for Unix, the equivalent of "at" on Windows. My purpose is not to document the use of cron - it is well documented elsewhere. Alas, ASUS does not include the crontab utility for creating and editing jobs in its firmware, but the cron daemon (crond) is installed and running. If a jobs file can be loaded into the daemon, crond will happily run the jobs.

A lesser documented standard feature in crond is to watch the directory /var/spool/cron/crontabs/ for a file listing jobs to run. Ordinarily you should not modify files in this directory manually - that's what crontab is for - but absent crontab one can still manually edit a file and place it in this folder.

For instance, if I create a file with the following contents, and copy it to /var/spool/cron/crontabs/<admin user name>, it will run the job.sh script every 5 minutes:
# cron jobs to import into root's crontab
*/5 * * * * /jffs/job.sh
Simple and sweet.

A minor "gotcha" is that /var is volatile storage; it is wiped out and recreated every time the router boots, so any cron file placed in that directory has to be replaced upon reboot.

One undocumented feature in ASUSWRT is the ability to specify a custom script to execute anytime a USB drive is mounted. Since a USB drive that is plugged into the router has to be mounted before use, this feature also has the effect of substituting for a "run at boot" script, as long as there is a USB drive attached.


ASUSWRT also provides a small portion of persistent storage useful for placing files that you don't want to be deleted upon rebooting. This folder is located at /jffs, and is a convenient place to store custom scripts. Just be aware that while /jffs is not disturbed by rebooting, it might be deleted upon installing an updated firmware. I keep a backup copy of my custom scripts elsewhere, but for simplicity sake I run them from /jffs.

The syntax to cause a script to run upon mounting a USB drive is:

nvram set script_usbmount="/jffs/scriptname"
nvram commit
For our purposes, /jffs/scriptname contains the following line to copy a cron file from persistent storage into the folder watched by crond:
cp /jffs/<admin user name> /var/spool/cron/crontabs
Make sure the file permissions allow executing (running) the script; since this is Unix, the simplest way to do this is chmod 755 scriptname. Incidentally, there is also a script_usbumount option that runs upon unmounting or removing a USB drive. If you are so inclined, run nvram show to see a very long list of parameters ASUSWRT stores.

Why might one want to schedule tasks on a router? Here are a few examples:

Do you have something to add? A question you'd like answered? Think I'm out of my mind? Join the conversation below, reach out by email at david (at) securityforrealpeople.com, or hit me up on Twitter at @dnlongen

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Whois David?

My photo

I have spent the better part of two decades in information technology and security, with roots in application developer support, system administration, and network security. My specialty is cyber threat intelligence - software vulnerabilities and patching, malware, social networking risks, etc. In particular, I strive to write about complex cyber topics in a way that can be understood by those outside the infosec industry.

Why do I do this? A common comment I get from friends and family is that complex security topics give them headaches. They want to know in simple terms how to stay safe in a connected world. Folks like me and my peers have chosen to make a profession out of hacking and defending. I've been doing this for the better part of two decades, and so have a high degree of knowledge in the field. Others have chosen different paths - paths where I would be lost. This is my effort to share my knowledge with those that are experts in something else.

When not in front of a digital screen, I spend my time raising five rambunctious teens and pre-teens - including two sets of twins. Our family enjoys archery, raising show and meat rabbits, and simply enjoying life in the Texas hill country.

For a decade I served as either Commander or a division leader for the Awana Club in Dripping Springs, Texas; while I have retired from that role I continue to have a passion for children's ministry. At the moment I teach 1st through 3rd grade Sunday School. Follow FBC Dripping Springs Kids to see what is going on in our children's ministries.