You are not logged in.

#1 2011-03-06 17:23:58

kaaposc
Member
From: Latvia
Registered: 2008-07-16
Posts: 30

Daemon starter/stopper

Made a quick search but found no topics about a simple bash script to simplify daemon starting/stopping. Here is my try.

#!/bin/bash

if [ -z $1 ]; then
    echo "Usage:"
    echo -e "$0 [-]daemon1 [-]daemon2 ... [-]daemonN"
    echo -e "\tStarts (or restarts if running) a daemon located in /etc/rc.d/"
    echo -e "\tIf '-' in front of a daemon, it is stopped."
    exit 1
fi

if [ $UID -gt 0 ]; then
    echo "Must be root to start/stop daemons."
    exit 1
fi

while (( "$#" )); do
    daemon=$1
    if [ ${daemon:0:1} == "-" ]; then
        if [ -f /var/run/daemons/${daemon:1} ]; then
            if [ -x /etc/rc.d/${daemon:1} ]; then
                echo "Stopping ${daemon:1}..."
                /etc/rc.d/${daemon:1} stop
            else
                echo "No such daemon: ${daemon:1}!"
            fi
        else
            echo "Service ${daemon:1} not running!"
        fi
    else
        if [ -f /var/run/daemons/$daemon ]; then
            if [ -x /etc/rc.d/$daemon ]; then
                echo "Restarting $daemon..."
                /etc/rc.d/$daemon restart
            else
                echo "No such daemon: $daemon!"
            fi
        else
            if [ -x /etc/rc.d/$daemon ]; then
                echo "Starting $daemon..."
                /etc/rc.d/$daemon start
            else
                echo "No such daemon: $daemon!"
            fi
        fi
    fi
    shift
done

Offline

#2 2011-03-06 17:43:30

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Daemon starter/stopper

Offline

Board footer

Powered by FluxBB