Here I demo how to add commands to system service on Linux.
Assume I have one shell script named collectd, like the following:
———————————————-
#!/bin/sh
#
# Startup script for the collectd Server
#
# chkconfig: – 50 50
# description: collectd gathers statistics about the system it is running on and stores this information
# processname: collectd
# pidfile: /opt/collectd/var/run/collectd.pid
# config: /opt/collectd/etc/collectd.conf
# command: /opt/collectd/sbin/collectd
CONFIG_FILE=/opt/collectd/etc/collectd.conf
PID_FILE=/opt/collectd/var/run/collectd.pid
CMD_FILE=/opt/collectd/sbin/collectd
case “$1” in
start)
# Starts the collectd deamon
echo “Starting collectd”
$CMD_FILE -C $CONFIG_FILE
;;
start)
# Starts the collectd deamon
echo “Starting collectd”
$CMD_FILE -C $CONFIG_FILE
;;
stop)
# stops the daemon bt cat’ing the pidfile
echo “stopping collectd”
kill -9 `cat $PID_FILE`
;;
# stops the daemon bt cat’ing the pidfile
echo “stopping collectd”
kill -9 `cat $PID_FILE`
;;
restart)
## Stop the service regardless of whether it was
## running or not, start it again.
echo “Restarting collectd”
$0 stop
$0 start
;;
## Stop the service regardless of whether it was
## running or not, start it again.
echo “Restarting collectd”
$0 stop
$0 start
;;
reload)
# reloads the config file by sending HUP
echo “Reloading config”
kill -HUP `cat $PID_FILE`
;;
# reloads the config file by sending HUP
echo “Reloading config”
kill -HUP `cat $PID_FILE`
;;
*)
echo “Usage: collectd (start|stop|restart|reload)”
exit 1
;;
echo “Usage: collectd (start|stop|restart|reload)”
exit 1
;;
esac
—————————————————–
—————————————————–
Then I copy this script to /etc/init.d/collectd and perform the below commands.
chkconfig –add collectd
chkconfig –level 2345 collectd on
service collectd start
chkconfig –level 2345 collectd on
service collectd start
沒有留言:
張貼留言