Sunday, June 27, 2010

Wireless Scheduler with Cron Job

Cron is a time-based job scheduler in Linux systems which enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. In this example, we'll use cron to control the wireless signal, set it to turn on only at given time.

DD-WRT is shipped with cron support but again we'll use our own cron daemon as it's easier for future update and will save some flash wearing.

  1. Disable DD-WRT's Cron
    From the web interface, Administration->Management, find "Cron" and disable it, then click "Apply Settings"
    cron-disable

  2. Install cron(busybox) service
    The cron is provided by busybox and should've been installed. If not, run
    opkg install busybox

    and create the scheduling file folder:
    mkdir /etc/crontabs

    Set it to run on startup by creating /opt/etc/init.d/crond with following:

    source /mnt/root/.profile
    kill -9 $(pidof crond)
    /opt/usr/sbin/crond

    Then:
    chmod a+x /opt/etc/init.d/crond
    ln -s /opt/etc/init.d/crond /opt/etc/init.d/S80crond

  3. The script to switch wireless
    Now create a file /opt/usr/sbin/wireless-off.sh

    #!/bin/sh
    /sbin/ifconfig ath0 down

    another one: /opt/usr/sbin/wireless-on.sh

    #!/bin/sh
    /sbin/ifconfig ath0 up

    On VAP enabled routers, additional interfaces like ath0.1, ath0.2...etc. also need to be taken care of:
    /sbin/ifconfig ath0.1 down
    and
    /sbin/ifconfig ath0.1 up
    The full list of wireless interfaces can be found by running /sbin/ifconfig

    Don't forget the permission:
    chmod a+x /opt/usr/sbin/wireless-on.sh /opt/usr/sbin/wireless-off.sh

  4. Edit Cron Scheduler
    DD-WRT's wireless scheduling is available only for Broadcom routers(see picture below) but missing from Atheros builds.
    radio-sheduling-broadcom 
    However, its not hard to implement this on Atheros routers with the help of cron and the scripts above.
    For example, to turn on wireless only from 5pm to 11pm, run
    crontab -e
    which will bring you the cron job editor. Add two lines like that

    0 17 * * *   /opt/usr/sbin/wireless-on.sh
    0 23,0-16 * * *   /opt/usr/sbin/wireless-off.sh


    Save. The wireless-on script will runs only once at 5PM(17:00) but the wireless-off script runs every hour(23:00, 0:00 ... 16:00) to ensure the wireless can still be turned off in case the router is rebooted halfway.

    Now how about turning off wireless only on weekdays?

    0 17 * * *   /opt/usr/sbin/wireless-on.sh
    0 23,0-16 * * 1-4   /opt/usr/sbin/wireless-off.sh
    0 0-16 * * 5   /opt/usr/sbin/wireless-off.sh


    Refer here for the full instruction about cron.

10 comments:

Anonymous said...

Thanks for this posting. I'd been looking for a way to turn the radio on/off based on time of day. Looking at the crontabs I understand the precaution of assuming things don't always go as planned and that perhaps the router might get cycled during its presumed off period. Running the off-script "just in case" is acceptable I suppose but do you know of a way (a script of some sort) to determine within the program if the radio is off or on? Just seems like a cleaner approach than not knowing and continuing to turn the radio off when it is in fact likely already off.

leo.d said...

The router is running openwrt atm so I'm not sure if it's the same on dd-wrt. However, on OpenWrt, when turn off the wireless(ifconfig wlan0 down) the wlan0 interface is no longer displayed in "ifconfig" output, also the output of "ifconfig wlan0" is slightly different from when wifi is on.

Anonymous said...

Hi,

if i try to create the scheduling file folder i get this error message:

mkdir: cannot create directory '/etc/crontabs': Read-only file system

how can i fix this ?

Anonymous said...

I am unable to create the folders you are refering to in the guide. nothing happends. when I do df -h I can see that 100% (4,1 MB) of my filesystem is in use. Is this guide based on routers with external harddrives attaced?

Anonymous said...

To all who receive errors: Seems that commands only work with "real official" DD-wrt, not Buffalo branded.

Another thing, I think there is a typo, opkg should be ipkg?

Gianluigi Di Vaio said...

Hi, from my point of view is much better to go in Administration -> Management and enable the cron daemon, than in the "cron window" write something like this example:
30 23 * * * root /sbin/ifconfig ath0 down
30 06 * * * root /sbin/ifconfig ath0 up

The example script will shutdown the WiFi at 23.30 everyday and will start-up it again at 06.30

Remember to Apply and Reboot your DD-WRT router!
Ciao,
Gianluigi

Koen De Jaeger said...

Does this also turn off the radio waves that are emitted by the router and cause long term health problems?

Anonymous said...

Thank you to Gianluigi Di Vaio for his tips ;)

Thomas Venere said...

Ciao Gianluigi Di Vaio.. visto che sei italiano potresti aiutarmi?
Io possiedo questo router TP-Link WR1043ND e avrei la necessitĂ  di spegnere ed accendere in automatico la WiFi.
Vorrei farlo con il cron, ma non riesco a capire il giusto script da scrivere.
Io vorrei accendere la wirelessa ogni giorno alle 6:00 del mattino e spegnerla ogni giorno alle 21:00
Qualcuno può dirmi cosa devo scrivere dentro il CRON JOB (giusti spazi...root, etc)?

Unknown said...

The built in cron cant be disabled. It gets enabled when I reboot and then prevents the cron from busybox from starting!

Post a Comment