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.
- Disable DD-WRT's Cron
From the web interface, Administration->Management, find "Cron" and disable it, then click "Apply Settings"
- 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
- 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
- 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/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.
6 comments:
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.
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.
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 ?
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?
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?
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
Post a Comment