Friday, April 26, 2013

Wireless Scheduler with Cron Job

Original post from http://g300nh.blogspot.com/2010/06/wireless-schedule-with-cron-job.html

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) serviceThe 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 downand
    /sbin/ifconfig ath0.1 upThe 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 -ewhich 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.

No comments:

Post a Comment