PDA

View Full Version : 'service' script for managing system services


dwm
12-06-2007, 02:46 PM
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.


#!/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

chatwizrd
12-06-2007, 03:25 PM
Here is one for freebsd :)



#!/bin/sh

findcmd=/usr/bin/find # Set to the find program.

################### DONT TOUCH ######################

sver="v1.0"
ver="FreeBSD Service Script $sver for DirectAdmin"
author="Andrew Keuhs (andrew at andrewk.net)"

path=/etc/rc.d:/etc/rc.d/init.d:/usr/local/etc/rc.d

if [ $(id -u) != "0" ]; then
echo "This program is for ROOT user only!"
exit 1
fi

if [ $# -lt "1" ]; then
echo "Usage: $0 < program > [ command ]"
echo "Help: $0 --help"
exit 1
fi

case "$1" in

-h|--help)
echo "$ver"
echo ""
echo "Quick Help:"
echo " ---------"
echo "To use the program do as follows:"
echo "Type: $0 < program name > [ command ]"
echo ""
echo "Example: $0 httpd restart"
echo "(This would attempt a restart on your httpd server.)"
echo ""
echo "General Help:"
echo " -----------"
echo "-h Displays this help menu."
echo "--help Displays this help menu."
echo "--author Show author information."
echo ""
;;

-a|--author)
echo "$ver was created by $author."
;;

*)
newpath=$(echo $path | tr -s ':' '\n')

for path in $newpath; do
$findcmd $path -name $1 >/dev/null 2>&1
done

if [ -e "$path/$1" ]; then
$path/$1 $2

else
echo "Sorry the program $1 does not exist."
exit 1
fi
;;

esac
exit 0

dwm
12-06-2007, 04:04 PM
Hey! I like this part of your script:


path=/etc/rc.d:/etc/rc.d/init.d:/usr/local/etc/rc.d

and:

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