[SOLVED] Shell Script Daemon


I need to find a way to control some paramenters of my routers while using VPNC (an IPSec VPN CLient). When it either connects to the VPN or disconnects I need some actions to be taken (turn on WPS led, routes, fix dns, etc), a simple bash script apparently does the trick but I need to have it running at startup. So I basically have to monitor the state of the tunnel interface and take action when this state changes:

while :
        operstate=$(cat /sys/devices/virtual/net/vpn-tun0/operstate)
        operstatewan=$(cat /sys/devices/platform/ag71xx.0/net/eth0/operstate)

        if [[ -e /sys/class/net/vpn-tun0 ]] && [[ "$operstate" == "unknown" ]] && [[ "$operstatewan" == "up" ]];

                if [ $state -eq 0 ]
                        echo 1 > /sys/devices/platform/leds-gpio/leds/tp-link:blue:qss/brightness
                        logger vpn state up
                        route add -net netmask dev vpn-tun0
                        logger vpn route configured

                if [ $state -eq 1 ]

                        echo 0 > /sys/devices/platform/leds-gpio/leds/tp-link:blue:qss/brightness
                        logger vpn state is down dnsmasq will be restarted
                        /etc/init.d/dnsmasq restart
        sleep 5

What is the best way to turn it into a daemon, I guess using init.d is the way, but I ended up bricking one of my routers trying to achieve that so before I brick another device I need some help. I tryed the following without good results.

#!/bin/sh /etc/rc.common


start_service() {
stop () {
        killall script_vpn.sh

Any help is appreciated, I know https://wiki.openwrt.org/inbox/procd-init-scripts but this page tutor assumes you already have some knowledge. How can I improve this?

Why do you talk specifically about "bash" in the topic title, while your script is written for "sh" and is actually run by "ash" in busybox. Or have you really installed the bash shell and defined that it is used?

You might achieve the daemonising also by launching the script in a detached shell from /etc/rc.local (that is run at the end of the boot process)

e.g. add this to /etc/rc.local:

( script_vpn.sh )&

Ps. make sure to specify the correct directory or place the script on a directory that is in path.

1 Like

Inacurate title now corrected. I like your suggestion. I'll try that. I would also like to mention that this script_vpn.sh is older version and possibly not working quite well. I'll correct that when topic is solved.

May want to check hotplug as well.


Using hotplug or a ubus listener is a good suggestion for this kind of thing.

Here's a decent description of procd, reverse-engineered as I read it

Edit: The original link does not appear to work at this time. Here's an archive of it:

1 Like

I recall that VPNC calls a script called "vpnc-script" on connect and disconnect, so you could also try to add your commands there. In LEDE it seems to be located in "/lib/netif" if you've installed the "vpnc-scripts" package.

You can also use /etc/init.d/ to kick off these scripts. You can, in theory, put the script itself directly into init.d or simply create a new script that simply launches the existing script. init.d lets you define the start order and gives you the ability to define start, stop, and restart behaviors. And you can then enable/disable the script by using:
/etc/init.d/<script_name> enable (or disable)
or use the LuCI startup page.

This link is unavailable.

Thanks, I'll take a look at it and it looks like the proper way to go. If hotplug uses operstate files as references we might find some problems though. First wan operstate is up in some devices even if there is no cable attached to the interface (as far as I know it has to do with hardware architecture), vpnc creates a tunnel where the operstate is "unknown" when it's up. Sure it can be overcome.

rc.local solution works fine for now, thanks. I will take a look at hotplug and procd for future use though.

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