'service' script for managing system services

dwm

Verified User
Joined
Dec 4, 2007
Messages
65
Location
Netherlands
I am willing to share my scripts. Here is the script i made for managing system services quick and easy. Maybe there is a default package for it which i can't find, but i sure know some users will be happy with this script which implement the same ease as managing services on CentOS.

Code:
#!/bin/sh
# Author: Tim Boormans, Direct Web Solutions
# Email: info [at] directwebsolutions.nl
# Description: This script is a shortcut to start, stop, reload
# and/or restart services on your system with ease.
#
# Examples:
# service name_of_the_service start
# service name_of_the_service stop

# For using this on your own system add a file called 'service' with
# this content in the /sbin/ folder on your harddrive and
# run once this command: chmod +x /sbin/service


# Variables
SERVICE=$1
OPTION=$2

# Functions
status() {
        if [[ `ls -a /etc/init.d/ | grep -e "^$SERVICE$"` ]]
        then
                echo -n "Status service: $SERVICE"
                echo
                /etc/init.d/$SERVICE status
        else
                echo -n "Service $SERVICE not found."
                echo
        fi
}

start() {
        if [[ `ls -a /etc/init.d/ | grep -e "^$SERVICE$"` ]]
        then
                echo -n "Starting service: $SERVICE"
                echo
                /etc/init.d/$SERVICE start
        else
                echo -n "Service $SERVICE not found."
                echo
        fi
}

stop() {
        if [[ `ls -a /etc/init.d/ | grep -e "^$SERVICE$"` ]]
        then
                echo -n "Stopping service: $SERVICE"
                echo
                /etc/init.d/$SERVICE stop
        else
                echo -n "Service $SERVICE not found."
                echo
        fi

}

reload() {
        if [[ `ls -a /etc/init.d/ | grep -e "^$SERVICE$"` ]]
        then
                echo -n "Reloading service: $SERVICE"
                echo
                /etc/init.d/$SERVICE reload
        else
                echo -n "Service $SERVICE not found."
                echo
        fi
}

restart() {
        if [[ `ls -a /etc/init.d/ | grep -e "^$SERVICE$"` ]]
        then
                echo -n "Restarting service: $SERVICE"
                echo
                /etc/init.d/$SERVICE restart
        else
                echo -n "Service $SERVICE not found."
                echo
        fi
}

# Exactly two parameters required
if [[ $# != 2 ]]
then
        echo -n "Usage: $0 name_of_the_service {status|start|stop|reload|restart}"
        echo
        exit 1
fi

# Check the command line for actions
case "$2" in
        "status")
                status
                ;;
        "start")
                start
                ;;
        "stop")
                stop
                ;;
        "reload")
                reload
                ;;
        "restart")
                restart
                ;;

        *)
                echo -n "Usage: $0 name_of_the_service {status|start|stop|reload|restart}"
                echo
                exit 1
esac

exit 0
 
Last edited:
Hey! I like this part of your script:

Code:
path=/etc/rc.d:/etc/rc.d/init.d:/usr/local/etc/rc.d
and:
Code:
if [ $(id -u) != "0" ]; then
        echo "This program is for ROOT user only!"
        exit 1
fi

I will keep it in mind for next scripts! :D
 
Back
Top