1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice

Welcome To SNBForums

SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.

If you'd like to post a question, simply register and have at it!

While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!

Extend "service" to Entware services

Discussion in 'Asuswrt-Merlin' started by cmkelley, Feb 11, 2019.

  1. cmkelley

    cmkelley Senior Member

    Joined:
    Aug 11, 2015
    Messages:
    360
    Location:
    Greater Los Angeles Area, California, USizicstania
    I got tired of typing '/opt/etc/init/S01syslog-ng restart', particularly when there's a perfectly good "service action_target" mechanism for the firmware services. So, not knowing any better, I set out to remedy that situation (it's late, I'm punchy, bear with me). In the spirit of the "add one line" mantra, it requires only a single line to be added to /jffs/scripts/service-event:
    Code:
    /jffs/scripts/entware-service "$1" "$2" & # service ACTION_TARGET for Entware
    If you don't already have a /jffs/scripts/service-event script, create one:
    Code:
    #!/bin/sh
    
    /jffs/scripts/entware-service "$1" "$2" & # service ACTION_TARGET for Entware
    
    and make it executable
    Code:
    chmod +x /jffs/scripts/service-event
    Create a /jffs/scripts/entware-service file (sorry, no fancy installer):
    Code:
    #!/bin/sh
    #tof
    
    # allow f/w "service" command to work on Entware services
    # - services must be defined as /opt/etc/init.d/S##service_name
    # - S##service_name must be an executable shell script
    
    ACTION=$1
    TARGET=$2
    
    if [ $# -lt 2 ] || [ ! -x /opt/bin/find ]
    then
        exit 1
    fi
    
    SRVNAME="$(/opt/bin/find /opt/etc/init.d/ -perm '-u+x' -name "S[0-9][0-9]$TARGET")"
    
    if [ "X$SRVNAME" != "X" ]
    then
        $SRVNAME $ACTION
    fi
    
    #eof
    
    and make it executable
    Code:
    chmod +x /jffs/scripts/entware-service
    Now you can restart Entware services with eg. "service restart_syslog-ng", "service restart_stubby", etc.

    Annoyingly enough, just like when restarting a firmware service, you have to look in /tmp/syslog.log to see if it worked or not. Seriously, you can type "service tango_uniform" and it will reply with "Done."

    EDIT: added -x test for /opt/bin/find as discussed in comments
     
    Last edited: Feb 11, 2019
  2. Dabombber

    Dabombber Occasional Visitor

    Joined:
    Apr 29, 2016
    Messages:
    17
    If you want one line, why not just have in /jffs/scripts/service-event
    Code:
    #!/bin/sh
    
    # service ACTION_TARGET for Entware
    [ -x /opt/bin/find ] && /opt/bin/find /opt/etc/init.d/ -perm '-u+x' -name "S[0-9][0-9]$2" -execdir {} "$1" \; &
    
    EDIT: Added [ -x /opt/bin/find ] check
     
    Last edited: Feb 11, 2019
    ColinTaylor likes this.
  3. ColinTaylor

    ColinTaylor Part of the Furniture

    Joined:
    Mar 31, 2014
    Messages:
    7,549
    Location:
    UK
    You might want to put a check in for the existence of /opt/bin/find and exit the script if not found.

    During boot there are system services which start before the Entware partition has mounted (dnsmasq, diskmon, firewall, upnp, vpnserver1, etc.). This means that the find command will not be found. TBH it's not really a problem because the script will just fall through to the bottom, but it's not very aesthetic.

    Code:
    + ACTION=restart
    + TARGET=dnsmasq
    + [ 2 -lt 2 ]
    + /opt/bin/find /opt/etc/init.d/ -perm -u+x -name S[0-9][0-9]dnsmasq
    /jffs/scripts/entware-service: line 19: /opt/bin/find: not found
    + SRVNAME=
    + [ X != X ]
     
    Last edited: Feb 11, 2019
  4. cmkelley

    cmkelley Senior Member

    Joined:
    Aug 11, 2015
    Messages:
    360
    Location:
    Greater Los Angeles Area, California, USizicstania
    2 reasons: 1st, I'm not anywhere near that good with scripting, the line I had was copied off rc.unslung with minimal changes; and 2nd, I thought it was going to be more complicated than it was when I started, I thought I'd have to use some sort of loop, so I started off with it in another script and never thought about if I could make it one line (not that I would have known how even if I had thought of it).