You are not logged in.

#1 2009-10-03 23:31:18

bimbo
Member
Registered: 2005-08-12
Posts: 22

Service Configuration Tool (svc) for Archlinux

Hello, I'm providing you with a very simple Service Configuration Tool for the management of system services (scripts located inside /etc/rc.d).
DOWNLOAD LINK: http://aur.archlinux.org/packages.php?ID=30660

Usage:
svc DAEMON subcommand

DAEMON = name of the service to manage
subcommand = subcommand to pass to the service (e.g. start)

Example (DAEMON=samba): 
svc samba enable
svc samba disable
svc samba configure

It acts as a wrapper for services and provides a mechanism for enabling (starting and adding the daemon to /etc/rc.conf) and disabling (stopping and removing the daemon from /etc/rc.conf) them. The tool does not constrain your subcommands to only enable and disable, any other service subcommand should act as expected (the value of this subcommand is passed to the rc.d script directly).

By its own, the script doesn't do much. It is with the help of a shell with a completion system (like zsh or recent versions of bash) that you can get the full benefits of it. For example, I only have to type "svc <TAB>" and all available system services will be available for completion, once a service is typed, it's subcommands (start, stop, restart, etc) will be available for completion too, along with enable and disable.

Other feature I integrate into some rc.d scripts is the subcommand "configure", which simply calls an editor with the service configuration file (e.g. vim /etc/samba/smb.conf). By combining this and the "svc" tool I can easily configure some services (the ones that make sense, apache is not a good candidate for this since it's got many configuration files): "svc samba configure".

This was done for two reasons: 1) I used opensolaris for over a year and a half, and when I switched back to linux there was a utility I really missed: "svcadm" (actually, the whole SMF is what I miss, but such a complete/beautiful init framework/service management replacement won't make it into gnu/linux any time soon); 2) As a system administrator, it's daunting to have to type "/e<TAB>/r<TAB>.<TAB>/$SERVICE_NAME $SUBCOMMAND" when I need to manage a service, it's much more pleasant to simply write "svc <TAB> $SERVICE_NAME $SUBCOMMAND" ($SERVICE_NAME and $SUBCOMMAND show options for completion when pressing <TAB>).

Along with the "svc" script, a script for the zsh completion system is provided. Please contact me if you have an implementation for bash.


Enjoy,
Edgar Merino

Last edited by bimbo (2009-10-08 01:53:58)

Offline

#2 2009-10-04 00:09:00

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: Service Configuration Tool (svc) for Archlinux

Excellent! I have a question, though. How would I integrate this with my premade functions so that the functions get tab complete? The functions are below.

start() {
sudo /etc/rc.d/$1 start
}

stop() {
sudo /etc/rc.d/$1 stop
}

restart() {
sudo /etc/rc.d/$1 restart
}

Offline

#3 2009-10-04 00:40:09

bimbo
Member
Registered: 2005-08-12
Posts: 22

Re: Service Configuration Tool (svc) for Archlinux

You could easily get rid of those premade functions and integrate the functionality inside the _svc script:

In the "svc()" function, for every option of the switch (case) statement, prefix the calls to the rc.d scripts with "sudo", e.g. "sudo ${SERVICES_DIR}/$1 start". If you want to constrain completion options for services to only "( start stop restart)", remove the "enable" and "disable" bindings defined in the "svc()" function and modify the "_svc()"  function (notice the underscore) like this:

_svc() {
    case $CURRENT in
        2) # Show services under /etc/rc.d for completion
            _services
            ;;
        3) # Show current service subcommands for completion

            # This is here so that _init_d works on the service name rather
            # than on the actual function name "svc"
            (( CURRENT-- )) 
            shift words

            # CONSTRAIN COMPLETION OPTIONS TO ONLY start, stop and restart
            compadd start stop restart
            ;;
    esac

}

Last edited by bimbo (2009-10-04 02:10:16)

Offline

#4 2009-10-04 02:27:12

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: Service Configuration Tool (svc) for Archlinux

bimbo wrote:

You could easily get rid of those premade functions and integrate the functionality inside the _svc script:

In the "svc()" function, for every option of the switch (case) statement, prefix the calls to the rc.d scripts with "sudo", e.g. "sudo ${SERVICES_DIR}/$1 start". If you want to constrain completion options for services to only "( start stop restart)", remove the "enable" and "disable" bindings defined in the "svc()" function and modify the "_svc()"  function (notice the underscore) like this:

_svc() {
    case $CURRENT in
        2) # Show services under /etc/rc.d for completion
            _services
            ;;
        3) # Show current service subcommands for completion

            # This is here so that _init_d works on the service name rather
            # than on the actual function name "svc"
            (( CURRENT-- )) 
            shift words

            # CONSTRAIN COMPLETION OPTIONS TO ONLY start, stop and restart
            compadd start stop restart
            ;;
    esac

}

So, would I have to call it via _svc $DAEMON start or something similar, then?

Offline

#5 2009-10-04 04:31:09

bimbo
Member
Registered: 2005-08-12
Posts: 22

Re: Service Configuration Tool (svc) for Archlinux

No, if you are using ZSH and the script _svc is in the FPATH (e.g. under /usr/share/zsh/site-functions), then every time you type "svc" in your command line it will look for a script named "_svc" for completion. Try reading the ZSH manual regarding the new completion system (man zshcompsys) or the user manual, but let me warn you that this is not easy stuff.

If you follow what I posted earlier, then you should be able to call your services like this:

svc $DAEMON start
svc $DAEMON stop
svc $DAEMON restart

alternatively, you can use the enable, disable bindings for starting and stopping services:

svc $DAEMON enable
svc $DAEMON disable

Last edited by bimbo (2009-10-04 04:32:59)

Offline

#6 2009-10-04 04:54:55

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: Service Configuration Tool (svc) for Archlinux

bimbo wrote:

No, if you are using ZSH and the script _svc is in the FPATH (e.g. under /usr/share/zsh/site-functions), then every time you type "svc" in your command line it will look for a script named "_svc" for completion. Try reading the ZSH manual regarding the new completion system (man zshcompsys) or the user manual, but let me warn you that this is not easy stuff.

If you follow what I posted earlier, then you should be able to call your services like this:

svc $DAEMON start
svc $DAEMON stop
svc $DAEMON restart

alternatively, you can use the enable, disable bindings for starting and stopping services:

svc $DAEMON enable
svc $DAEMON disable

Thanks for explaining. I'm going to try it now. big_smile

Offline

#7 2009-10-15 17:12:09

darknebula
Member
Registered: 2008-06-25
Posts: 17

Re: Service Configuration Tool (svc) for Archlinux

I'm  not sure if this is what you're looking for, but this is what I have...

start() {
    sudo /etc/rc.d/$1 start
}

stop() {
    sudo /etc/rc.d/$1 stop
}

restart() {
    sudo /etc/rc.d/$1 restart
}

compdef '_files -W /etc/rc.d' start stop restart

Last edited by darknebula (2009-10-15 17:17:48)

Offline

Board footer

Powered by FluxBB