What's new

Shell snippet for automating cron script registration at boot

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

PaulC

Occasional Visitor
Here's a little snippet of shell script, intended to go into the services-start script in /jffs/scripts. It scans the USB drive for files having a .cron extension, and for each of those it finds, scans for a comment line containing 'cron:' and uses what follows to register that schedule using cru.

This isn't going to impress the accomplished, but maybe it'll help others.

Code:
#
# look for cron scripts on the USB drive, and register them with cru
#
basedir=$(nvram get apps_mounted_path)

if [ -d "${basedir}" ]
then
    for file in $(find "${basedir}" -name '*.cron')
    do
         id=$(basename "${file}" .cron)
         schedule=$(sed -n -e '/^#[ \t]*cron:/ {s/^#[ \t]*cron:[ \t]*\(.*\)$/\1/;p}' "${file}")
         cru a "${id}" "${schedule} ${file}"
    done
fi

Here's a simple example: my 'backup-settings.cron' script that backs up my router's settings onto my thumb drive daily:

Code:
#!/bin/sh
# cron: 07 4 * * * 
#
dir=$(dirname "$0")

nvram show > "$dir/settings/nvram-show.txt"
nvram save "$dir/settings/nvram-dump.bin"
cp -a /jffs "$dir/settings/"

Note for the security conscious: it's a very bad idea to run scripts automatically from removable media. If your router isn't in a physically secure location, please don't use this technique, or add some signature to .cron files that's unique to your router (that an attacker won't know), and check for it.
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top