diff mbox series

[OpenWrt-Devel] base-files: add list-enabled/disabled to service function in /etc/profile

Message ID CALu2O0Rzyg+Kv7h=tX7Gg9s9shPAUtb46oLsYpD61OYZ=VYsLg@mail.gmail.com
State New
Headers show
Series [OpenWrt-Devel] base-files: add list-enabled/disabled to service function in /etc/profile | expand

Commit Message

Stan Grishin June 3, 2020, 11:42 a.m. UTC
Implement service list-enabled and service list-disabled to provide an easy way
for users to list enabled/disabled services from CLI.

Signed-off-by: Stan Grishin <stangri@melmac.net>
---
 package/base-files/files/etc/profile | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

+        fi
+}

Comments

Michael Jones June 3, 2020, 6:33 p.m. UTC | #1
On Wed, Jun 3, 2020 at 6:42 AM Stan Grishin <stangri@melmac.net> wrote:

> Implement service list-enabled and service list-disabled to provide an
> easy way
> for users to list enabled/disabled services from CLI.
>
> Signed-off-by: Stan Grishin <stangri@melmac.net>
> ---
>  package/base-files/files/etc/profile | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/package/base-files/files/etc/profile
> b/package/base-files/files/etc/profile
> index 0beff1608f..e8350cfd6a 100644
> --- a/package/base-files/files/etc/profile
> +++ b/package/base-files/files/etc/profile
> @@ -38,3 +38,24 @@ in order to prevent unauthorized SSH logins.
>  --------------------------------------------------
>  EOF
>  fi
> +
> +service() {
> +        if [ "$1" = "list-enabled" ]; then
> +                for F in /etc/init.d/* ; do
> +                        $F enabled && echo "$F enabled"
> +                done;
> +        elif [ "$1" = "list-disabled" ]; then
> +                for F in /etc/init.d/* ; do
> +                        $F enabled || echo "$F disabled"
> +                done;
> +        elif [ -f "/etc/init.d/$1" ]; then
> +                /etc/init.d/$@
> +        else
> +                echo "Usage: service
> list-disabled|list-enabled|<service> [command]"
> +                if [ -n "$1" ]; then
> +                        echo "service "'"'"$1"'"'" not found, the
> following services are available:"
> +                        ls "/etc/init.d"
> +                fi
> +                return 1
> +        fi
> +}
> --
> 2.25.1
>
>
Could you provide examples of how this would be used?

What about "list-start" and "list-stop" as well?
Stan Grishin June 3, 2020, 11:21 p.m. UTC | #2
To obtain the list of enabled (for autostart) services, you'd type
service list-enabled. For disabled services service list-disabled. It
is useful when you need to quickly check which services are
enabled/disabled or when helping other users troubleshoot.

An alternative to list-enabled/list-disabled that I have considered
was to output the enabled status of available services below the usage
output, ie replace:
                if [ -n "$1" ]; then
                        echo "service "'"'"$1"'"'" not found, the
following services are available:"
                    ls "/etc/init.d"
                fi

with

                if [ -n "$1" ]; then
                        echo "service "'"'"$1"'"'" not found, the
following services are available:"
                    for F in /etc/init.d/* ; do
                        $F enabled && echo "$F (autostart enabled)" ||
echo "$F (autostart **disabled**)"
                    done;
                fi


Please elaborate on the list-start and list-stop question, I'm not
sure I understand the purpose of those.


On Wed, Jun 3, 2020 at 11:34 AM Michael Jones <mike@meshplusplus.com> wrote:
>
>
>
> On Wed, Jun 3, 2020 at 6:42 AM Stan Grishin <stangri@melmac.net> wrote:
>>
>> Implement service list-enabled and service list-disabled to provide an easy way
>> for users to list enabled/disabled services from CLI.
>>
>> Signed-off-by: Stan Grishin <stangri@melmac.net>
>> ---
>>  package/base-files/files/etc/profile | 21 +++++++++++++++++++++
>>  1 file changed, 21 insertions(+)
>>
>> diff --git a/package/base-files/files/etc/profile
>> b/package/base-files/files/etc/profile
>> index 0beff1608f..e8350cfd6a 100644
>> --- a/package/base-files/files/etc/profile
>> +++ b/package/base-files/files/etc/profile
>> @@ -38,3 +38,24 @@ in order to prevent unauthorized SSH logins.
>>  --------------------------------------------------
>>  EOF
>>  fi
>> +
>> +service() {
>> +        if [ "$1" = "list-enabled" ]; then
>> +                for F in /etc/init.d/* ; do
>> +                        $F enabled && echo "$F enabled"
>> +                done;
>> +        elif [ "$1" = "list-disabled" ]; then
>> +                for F in /etc/init.d/* ; do
>> +                        $F enabled || echo "$F disabled"
>> +                done;
>> +        elif [ -f "/etc/init.d/$1" ]; then
>> +                /etc/init.d/$@
>> +        else
>> +                echo "Usage: service
>> list-disabled|list-enabled|<service> [command]"
>> +                if [ -n "$1" ]; then
>> +                        echo "service "'"'"$1"'"'" not found, the
>> following services are available:"
>> +                        ls "/etc/init.d"
>> +                fi
>> +                return 1
>> +        fi
>> +}
>> --
>> 2.25.1
>>
>
> Could you provide examples of how this would be used?
>
> What about "list-start" and "list-stop" as well?
Michael Jones June 3, 2020, 11:53 p.m. UTC | #3
On Wed, Jun 3, 2020 at 6:21 PM Stan Grishin <stangri@melmac.net> wrote:

> To obtain the list of enabled (for autostart) services, you'd type
> service list-enabled. For disabled services service list-disabled. It
> is useful when you need to quickly check which services are
> enabled/disabled or when helping other users troubleshoot.
>
> An alternative to list-enabled/list-disabled that I have considered
> was to output the enabled status of available services below the usage
> output, ie replace:
>                 if [ -n "$1" ]; then
>                         echo "service "'"'"$1"'"'" not found, the
> following services are available:"
>                     ls "/etc/init.d"
>                 fi
>
> with
>
>                 if [ -n "$1" ]; then
>                         echo "service "'"'"$1"'"'" not found, the
> following services are available:"
>                     for F in /etc/init.d/* ; do
>                         $F enabled && echo "$F (autostart enabled)" ||
> echo "$F (autostart **disabled**)"
>                     done;
>                 fi
>
>
> Please elaborate on the list-start and list-stop question, I'm not
> sure I understand the purpose of those.
>

Originally I asked that question because I misunderstood what the goal of
this change was.

I thought that you were proposing to add the ability to enable / disable
multiple services at the same time, so I was asking about the ability to
start / stop multiple services at the same time.

It's clear not that's not what you were trying to propose.

So instead, what about listing the services that are running, and also
listing the services that are configured, but not running?

I don't know that that provides a lot of value, so it may not be worth
doing.
Stan Grishin June 4, 2020, 9:56 a.m. UTC | #4
On Wed, Jun 3, 2020 at 4:53 PM Michael Jones <mike@meshplusplus.com> wrote:
>
>
>
> On Wed, Jun 3, 2020 at 6:21 PM Stan Grishin <stangri@melmac.net> wrote:
>>
>> To obtain the list of enabled (for autostart) services, you'd type
>> service list-enabled. For disabled services service list-disabled. It
>> is useful when you need to quickly check which services are
>> enabled/disabled or when helping other users troubleshoot.
>>
>> An alternative to list-enabled/list-disabled that I have considered
>> was to output the enabled status of available services below the usage
>> output, ie replace:
>>                 if [ -n "$1" ]; then
>>                         echo "service "'"'"$1"'"'" not found, the
>> following services are available:"
>>                     ls "/etc/init.d"
>>                 fi
>>
>> with
>>
>>                 if [ -n "$1" ]; then
>>                         echo "service "'"'"$1"'"'" not found, the
>> following services are available:"
>>                     for F in /etc/init.d/* ; do
>>                         $F enabled && echo "$F (autostart enabled)" ||
>> echo "$F (autostart **disabled**)"
>>                     done;
>>                 fi
>>
>>
>> Please elaborate on the list-start and list-stop question, I'm not
>> sure I understand the purpose of those.
>
>
> Originally I asked that question because I misunderstood what the goal of this change was.
>
> I thought that you were proposing to add the ability to enable / disable multiple services at the same time, so I was asking about the ability to start / stop multiple services at the same time.
>
> It's clear not that's not what you were trying to propose.
>
> So instead, what about listing the services that are running, and also listing the services that are configured, but not running?

Is there an easy universal way to determine if the service is running?

> I don't know that that provides a lot of value, so it may not be worth doing.

If there's a one/two liner to figure out if a service is running or
not, it may be better to implement `service list` which would print
the table of available init scripts, wherever they are enabled or not
and wherever they are running or not.

If that is deemed to be a better approach I can resubmit the patch.
Paul Fertser June 4, 2020, 1:02 p.m. UTC | #5
On Thu, Jun 04, 2020 at 02:56:31AM -0700, Stan Grishin wrote:
> If there's a one/two liner to figure out if a service is running or
> not,

ubus call service list '{ "verbose": true, "name": "openvpn" }' | \
     jsonfilter -e '@.openvpn.instances["foo"].running
Stan Grishin June 4, 2020, 1:31 p.m. UTC | #6
Thanks. Should I replace list-enabled and list-disabled with list, doing this:

        if [ "$1" = "list" ]; then
            for F in /etc/init.d/* ; do
                printf "%-30s\t%10s\t%10s\n"  "$F" \
                $( $($F enabled) && echo "enabled" || echo "disabled" ) \
                $( [ "$(ubus call service list "{ 'verbose': true,
'name': '$(basename $F)' }" \
                    | jsonfilter -q -e "@.$(basename
$F).instances[*].running")" = "true" ] \
                    && echo "running" || echo "stopped" )
            done;
        fi

?

On Thu, Jun 4, 2020 at 6:02 AM Paul Fertser <fercerpav@gmail.com> wrote:
>
> On Thu, Jun 04, 2020 at 02:56:31AM -0700, Stan Grishin wrote:
> > If there's a one/two liner to figure out if a service is running or
> > not,
>
> ubus call service list '{ "verbose": true, "name": "openvpn" }' | \
>      jsonfilter -e '@.openvpn.instances["foo"].running
>
> --
> Be free, use free (http://www.gnu.org/philosophy/free-sw.html) software!
> mailto:fercerpav@gmail.com
Paul Fertser June 4, 2020, 1:46 p.m. UTC | #7
On Thu, Jun 04, 2020 at 06:31:36AM -0700, Stan Grishin wrote:
> Thanks. Should I replace list-enabled and list-disabled with list, doing this:

I'm not sure it's anywhere close to the optimal solution with ubus and
jsonfilter, but I'm not well-versed in the area enough to suggest a
proper one, sorry.
Daniel Bailey June 4, 2020, 11:13 p.m. UTC | #8
Somebody who is better with jsonfilter than me might be able to clean this
up, but it does the trick:

#!/bin/sh

. /usr/share/libubox/jshn.sh

json_load "$(ubus call service list)"
json_get_keys SERVICES

for SERVICE in $SERVICES; do
    echo "$SERVICE: `ubus call service list | jsonfilter -e
"@.$SERVICE.instances[\"*\"].running"`"
done

On Thu, Jun 4, 2020 at 2:56 AM Stan Grishin <stangri@melmac.net> wrote:

> On Wed, Jun 3, 2020 at 4:53 PM Michael Jones <mike@meshplusplus.com>
> wrote:
> >
> >
> >
> > On Wed, Jun 3, 2020 at 6:21 PM Stan Grishin <stangri@melmac.net> wrote:
> >>
> >> To obtain the list of enabled (for autostart) services, you'd type
> >> service list-enabled. For disabled services service list-disabled. It
> >> is useful when you need to quickly check which services are
> >> enabled/disabled or when helping other users troubleshoot.
> >>
> >> An alternative to list-enabled/list-disabled that I have considered
> >> was to output the enabled status of available services below the usage
> >> output, ie replace:
> >>                 if [ -n "$1" ]; then
> >>                         echo "service "'"'"$1"'"'" not found, the
> >> following services are available:"
> >>                     ls "/etc/init.d"
> >>                 fi
> >>
> >> with
> >>
> >>                 if [ -n "$1" ]; then
> >>                         echo "service "'"'"$1"'"'" not found, the
> >> following services are available:"
> >>                     for F in /etc/init.d/* ; do
> >>                         $F enabled && echo "$F (autostart enabled)" ||
> >> echo "$F (autostart **disabled**)"
> >>                     done;
> >>                 fi
> >>
> >>
> >> Please elaborate on the list-start and list-stop question, I'm not
> >> sure I understand the purpose of those.
> >
> >
> > Originally I asked that question because I misunderstood what the goal
> of this change was.
> >
> > I thought that you were proposing to add the ability to enable / disable
> multiple services at the same time, so I was asking about the ability to
> start / stop multiple services at the same time.
> >
> > It's clear not that's not what you were trying to propose.
> >
> > So instead, what about listing the services that are running, and also
> listing the services that are configured, but not running?
>
> Is there an easy universal way to determine if the service is running?
>
> > I don't know that that provides a lot of value, so it may not be worth
> doing.
>
> If there's a one/two liner to figure out if a service is running or
> not, it may be better to implement `service list` which would print
> the table of available init scripts, wherever they are enabled or not
> and wherever they are running or not.
>
> If that is deemed to be a better approach I can resubmit the patch.
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
>
Stan Grishin June 4, 2020, 11:49 p.m. UTC | #9
Daniel, thanks, that's what I ended up doing in the code for the `service list`.

Should I send the patch for `service list` and remove `service
list-enabled` and `service list-disabled`? Do people care about this
minor improvement?

On Thu, Jun 4, 2020 at 4:13 PM Daniel Bailey <danielb@meshplusplus.com> wrote:
>
> Somebody who is better with jsonfilter than me might be able to clean this up, but it does the trick:
>
> #!/bin/sh
>
> . /usr/share/libubox/jshn.sh
>
> json_load "$(ubus call service list)"
> json_get_keys SERVICES
>
> for SERVICE in $SERVICES; do
>     echo "$SERVICE: `ubus call service list | jsonfilter -e "@.$SERVICE.instances[\"*\"].running"`"
> done
>
> On Thu, Jun 4, 2020 at 2:56 AM Stan Grishin <stangri@melmac.net> wrote:
>>
>> On Wed, Jun 3, 2020 at 4:53 PM Michael Jones <mike@meshplusplus.com> wrote:
>> >
>> >
>> >
>> > On Wed, Jun 3, 2020 at 6:21 PM Stan Grishin <stangri@melmac.net> wrote:
>> >>
>> >> To obtain the list of enabled (for autostart) services, you'd type
>> >> service list-enabled. For disabled services service list-disabled. It
>> >> is useful when you need to quickly check which services are
>> >> enabled/disabled or when helping other users troubleshoot.
>> >>
>> >> An alternative to list-enabled/list-disabled that I have considered
>> >> was to output the enabled status of available services below the usage
>> >> output, ie replace:
>> >>                 if [ -n "$1" ]; then
>> >>                         echo "service "'"'"$1"'"'" not found, the
>> >> following services are available:"
>> >>                     ls "/etc/init.d"
>> >>                 fi
>> >>
>> >> with
>> >>
>> >>                 if [ -n "$1" ]; then
>> >>                         echo "service "'"'"$1"'"'" not found, the
>> >> following services are available:"
>> >>                     for F in /etc/init.d/* ; do
>> >>                         $F enabled && echo "$F (autostart enabled)" ||
>> >> echo "$F (autostart **disabled**)"
>> >>                     done;
>> >>                 fi
>> >>
>> >>
>> >> Please elaborate on the list-start and list-stop question, I'm not
>> >> sure I understand the purpose of those.
>> >
>> >
>> > Originally I asked that question because I misunderstood what the goal of this change was.
>> >
>> > I thought that you were proposing to add the ability to enable / disable multiple services at the same time, so I was asking about the ability to start / stop multiple services at the same time.
>> >
>> > It's clear not that's not what you were trying to propose.
>> >
>> > So instead, what about listing the services that are running, and also listing the services that are configured, but not running?
>>
>> Is there an easy universal way to determine if the service is running?
>>
>> > I don't know that that provides a lot of value, so it may not be worth doing.
>>
>> If there's a one/two liner to figure out if a service is running or
>> not, it may be better to implement `service list` which would print
>> the table of available init scripts, wherever they are enabled or not
>> and wherever they are running or not.
>>
>> If that is deemed to be a better approach I can resubmit the patch.
>>
>> _______________________________________________
>> openwrt-devel mailing list
>> openwrt-devel@lists.openwrt.org
>> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
diff mbox series

Patch

diff --git a/package/base-files/files/etc/profile
b/package/base-files/files/etc/profile
index 0beff1608f..e8350cfd6a 100644
--- a/package/base-files/files/etc/profile
+++ b/package/base-files/files/etc/profile
@@ -38,3 +38,24 @@  in order to prevent unauthorized SSH logins.
 --------------------------------------------------
 EOF
 fi
+
+service() {
+        if [ "$1" = "list-enabled" ]; then
+                for F in /etc/init.d/* ; do
+                        $F enabled && echo "$F enabled"
+                done;
+        elif [ "$1" = "list-disabled" ]; then
+                for F in /etc/init.d/* ; do
+                        $F enabled || echo "$F disabled"
+                done;
+        elif [ -f "/etc/init.d/$1" ]; then
+                /etc/init.d/$@
+        else
+                echo "Usage: service
list-disabled|list-enabled|<service> [command]"
+                if [ -n "$1" ]; then
+                        echo "service "'"'"$1"'"'" not found, the
following services are available:"
+                        ls "/etc/init.d"
+                fi
+                return 1