Turn on router LED from cronjob

Hello people,

tl;dr version:

How can I turn LED lights on/off from a cronjob? Or, is there a better way of turning a LED based on a command output?

Long version:

I recently installed OpenWRT on a tp-link Archer C7 router and it's working great!
I have configured it to use PIA vpn provider with OpenVPN. Anyone interested I did a quick script for setting up and enable specific configs

I have also configured the router's WPS button to turn on/off the OpenVPN service as a quick way to turn the VPN on/off.

One thing that I haven't been able to do yet, is to make the LEDs work the way I want.

I would like to have the router's WPS LED to be ON when OpenVPN is running and OFF when it's not.

But I couldn't yet make it work with a cronjob. Maybe I'm missing something regarding how cronjobs work? Not sure.

Here's the cronjob I'm trying:

* * * * * echo "$([ $(pgrep -f openvpn) ] && echo default-on || echo none)" > /sys/class/leds/tp-link\:green\:wps/trigger

Manually running the command has the desired effect, but nothing happens with the cronjob.
Nothing very useful from the logs (from my understanding at least) either.

Ultimately, I want the light to be on/off according to the output of a command. I'm going for a cronjob as this is what I can think of. Happy to try another way if there's any.

Another thing that came to my mind is some sort of hook for OpenVPN, like: OpenVPN starts, turn light on. OpenVPN stops, turn ligh off. But I have not found such a thing yet.

Any ideas where to go from here?

master

commit 8fe9940db66517679f09fa1d2f6f79229b6a8361
openvpn: add generic hotplug mechanism

otherwise try and hook into something else in hotplug / procd trigger / inotifywatch /tmp/vpn.status or lookup 'pservice' ...

or even simpler... use the up / down scripts...

1 Like

There are differences between cron and interactive shells; one of them is that PATH is not set in a cron shell. One way to debug cron commands is by using ( command ) 2>&1 >>/root/cron.log and then looking at the logs in the file.

Presumably this should work.

You mean something like this?

That's still an open issue unfortunately.

Or you mean something else?

Thanks for the tip, but no luck with that. The file /root/cron.log is created but it's empty.

I went through it before but didn't realize I could use tun0 dev in this option.

Thanks a lot, it works now!

For reference, this is the config I'm using:

# /etc/config/system

config led 'led_wps'     
        option name 'WPS'               
        option dev 'tun0'       
        option mode 'link'     
        option sysfs 'tp-link:green:wps'
        option trigger 'netdev'

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.