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"

  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)

    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/

    /sbin/ifconfig ath0 down

    another one: /opt/usr/sbin/

    /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
    /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/ /opt/usr/sbin/

  4. Edit Cron Scheduler
    DD-WRT's wireless scheduling is available only for Broadcom routers(see picture below) but missing from Atheros builds.
    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/
    0 23,0-16 * * *   /opt/usr/sbin/

    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/
    0 23,0-16 * * 1-4   /opt/usr/sbin/
    0 0-16 * * 5   /opt/usr/sbin/

    Refer here for the full instruction about cron.


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...


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!

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!

RouterLogin said...

Thank you for sharing valuable information
Router Login not working
routerlogin admin page
Routerlogin Net

Robert Green said...

It is one of the best site that I have visited. Hope you will share more quality blog posts thank you.
Quickbooks contact helps you with everything regarding QuickBooks software. If you are stuck in some general issues related to QuickBooks like installation error or payroll issues then we are here to help you anytime you want.

allhelplinesupport3046 said...

Nice blog post on this topic.Gmail Customer Service Number
Gmail support number

AllHelplineNumber said...

We are one of the third party which provide best in class support for all kind of Roku tv setup , installation, updation and much more related issues over phone call and chat across the USA 24*7. We have a dedicated team of professionals who are well certified and experience to handle all kind of Roku tv box issues. We ensure each and every customer to get full satisfaction for his concern. You just need to dial up our roku customer support service number 1-800-382-3046.
Roku Customer Service Number
Roku Support Phone Number
Roku Toll Free Number
Roku Customer Care Number
Roku Helpline Number

Unknown said...
This comment has been removed by the author.
Candice Cromwell said...
This comment has been removed by the author.
Candice Cromwell said...
This comment has been removed by the author.
shalley30 said...

if you are facing problem related to virus in your PC and computer.Sign in to enter your product key, access your account, manage your subscription, and extend your Norton protection to PC, Mac, Android, and iOS devices then contact us.

Alex williams said...

Do you require HP printer setup for your mac operating system? Is your printer driver not suitable for macOS? Then visit the to get the software and driver for better functioning of your printer. You can also call our expert HP support team for services.

Post a Comment