List & Manage Runit Services

Listing

svlr slvs svlu

User specified services are in /usr/local/etc/runsvdir-$USER.

Note that the formatting is for POSIX shell. Bash requires escaping echos, i.e. echo -> echo -e, to work properly.

#!/bin/sh

GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m'

SYS_SVDIR="/etc/runit/sv"
SYS_RUNSVDIR="/run/runit/service"
USER_RUNSVDIR="/usr/local/etc/runsvdir-$USER"
USER_SVDIR="/usr/local/etc/svdir-$USER"

BIN_NAME=${0##*/}

list_all(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_SVDIR

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_SVDIR
}

all_status(){
        echo "${CYAN}SYSTEM:$NC"
        sudo sv status $SYS_RUNSVDIR/*

        echo "\n${CYAN}USER:$NC"
        sv status $USER_RUNSVDIR/*
}

list_running(){
        echo "${CYAN}SYSTEM:$NC"
        sudo fd pid $SYS_RUNSVDIR -t f -L | sudo xargs cat | xargs ps -fp

        echo "\n${CYAN}USER:$NC"
        fd pid $USER_RUNSVDIR -t f -L | xargs cat | xargs ps -fp 2>/dev/null
}

list_supervised(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_RUNSVDIR

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_RUNSVDIR
}

list_disabled(){
        echo "${CYAN}SYSTEM:$NC"
        sudo fd down $SYS_RUNSVDIR -t f -L | awk -F/ '{print $(NF-1)}'

        echo "\n${CYAN}USER:$NC"
        fd down $USER_RUNSVDIR -t f -L | awk -F/ '{print $(NF-1)}'
}

list_not_supervised(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_RUNSVDIR $SYS_SVDIR | grep -v : | sort | uniq -u

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_SVDIR $USER_RUNSVDIR | grep -v : | sort | uniq -u
}

list_stopped(){
        echo "${CYAN}SYSTEM:$NC"
        sudo fd pid $SYS_RUNSVDIR -t f -L | sudo xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}'


        echo "\n${CYAN}USER:$NC"
        fd pid $USER_RUNSVDIR -t f -L | xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}'
}

help(){
cat <<EOF
        $BIN_NAME r     running
        $BIN_NAME s     down(stopped)

        $BIN_NAME a     all

        $BIN_NAME u     supervised
        $BIN_NAME n     not supervised
        $BIN_NAME d     disabled(not auto starting)
        $BIN_NAME z     show status of all supervised services
EOF
}

case $1 in
        r*) list_running ;;
        d*) list_disabled ;;
        u*) list_supervised ;;
        a*) list_all ;;
        n*) list_not_supervised ;;
        s*) list_stopped ;;
        z*) all_status ;;
        *) help ;;
esac

The following implementation uses find instead of fd

#!/bin/sh

GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m'

SYS_SVDIR="/etc/runit/sv"
SYS_RUNSVDIR="/run/runit/service"
USER_RUNSVDIR="/usr/local/etc/runsvdir-$USER"
USER_SVDIR="/usr/local/etc/svdir-$USER"

BIN_NAME=${0##*/}

list_all(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_SVDIR

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_SVDIR
}

all_status(){
        echo "${CYAN}SYSTEM:$NC"
        sudo sv status $SYS_RUNSVDIR/*

        echo "\n${CYAN}USER:$NC"
        sv status $USER_RUNSVDIR/*
}

list_running(){
        echo "${CYAN}SYSTEM:$NC"
        sudo find -L $SYS_RUNSVDIR -type f -name pid | sudo xargs cat | xargs ps -fp

        echo "\n${CYAN}USER:$NC"
        find -L $USER_RUNSVDIR -type f -name pid | xargs cat | xargs ps -fp
}

list_supervised(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_RUNSVDIR

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_RUNSVDIR
}

list_not_supervised(){
        echo "${CYAN}SYSTEM:$NC"
        ls -1 $SYS_RUNSVDIR $SYS_SVDIR | grep -v : | sort | uniq -u

        echo "\n${CYAN}USER:$NC"
        ls -1 $USER_SVDIR $USER_RUNSVDIR | grep -v : | sort | uniq -u
}

list_disabled(){
        echo "${CYAN}SYSTEM:$NC"
        sudo find -L $SYS_RUNSVDIR -type f -name down | awk -F/ '{print $(NF-1)}'

        echo "\n${CYAN}USER:$NC"
        find -L $USER_RUNSVDIR -type f -name down | awk -F/ '{print $(NF-1)}'
}

list_stopped(){
        echo "${CYAN}SYSTEM:$NC"
        sudo find -L $SYS_RUNSVDIR -type f -name pid| sudo xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}'


        echo "\n${CYAN}USER:$NC"
        find -L $USER_RUNSVDIR -type f -name pid | xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}'
}

help(){
cat <<EOF
        $BIN_NAME r     running
        $BIN_NAME s     down(stopped)

        $BIN_NAME a     all

        $BIN_NAME u     supervised
        $BIN_NAME n     not supervised
        $BIN_NAME d     disabled(not auto starting)
        $BIN_NAME z     show status of all supervised services
EOF
}

case $1 in
        r*) list_running ;;
        d*) list_disabled ;;
        u*) list_supervised ;;
        a*) list_all ;;
        s*) list_stopped ;;
        z*) all_status ;;
        *) help ;;
esac

Managing

svmss

#!/bin/sh

BIN_NAME=${0##*/}

USER_RUNSVDIR="/usr/local/etc/runsvdir-$USER"
USER_SVDIR="/usr/local/etc/svdir-$USER"
SYS_SVDIR="/etc/runit/sv"
SYS_RUNSVDIR="/run/runit/service"

user_start(){
        export SVDIR=$USER_RUNSVDIR
        fd pid $USER_RUNSVDIR -t f -L | xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}' | fzf --prompt="Start User Service >" | xargs -I {}  sv start {}
}

system_start(){
        sudo fd pid $SYS_RUNSVDIR -t f -L | sudo xargs -I {} sh -c "[ ! -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}' | fzf --prompt="Start System Service >" | sudo xargs -I {} sv start {}
}

user_stop(){
        export SVDIR=$USER_RUNSVDIR
        fd pid $USER_RUNSVDIR -t f -L | xargs -I {} sh -c "[ -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}' | fzf --prompt="Stop User Service >" | xargs -I {} sv stop {}
}

system_stop(){
        sudo fd pid $SYS_RUNSVDIR -t f -L | sudo xargs -I {} sh -c "[ -s {} ] && echo {}" | awk -F/ '{print $(NF-2)}' | fzf --prompt="Stop System Service >" | sudo xargs -I {} sv stop {}
}

help(){
cat <<EOF
        $BIN_NAME sr    system start
        $BIN_NAME ss    system stop
        $BIN_NAME ur    user start
        $BIN_NAME us    user stop
        $BIN_NAME *     help
EOF
}

case $1 in
        "sr") system_start ;;
        "ss") system_stop ;;
        "ur") user_start ;;
        "us") user_stop ;;
        *) help ;;
esac