歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux注冊系統服務

Linux注冊系統服務

日期:2017/3/1 10:04:35   编辑:Linux編程

Linux下注冊一個系統服務,開機自啟動.

1、腳本編寫

#vim test.sh

#!/bin/bash

#description: hello.sh
#chkconfig: 2345 20 81

EXEC_PATH=/usr/local/
EXEC=hello.sh
DAEMON=/usr/local/hello.sh
PID_FILE=/var/run/hello.sh.pid

. /etc/rc.d/init.d/functions

if [ ! -x $EXEC_PATH/$EXEC ] ; then
echo "ERROR: $DAEMON not found"
exit 1
fi

stop()
{
echo "Stoping $EXEC ..."
ps aux | grep "$DAEMON" | kill -9 `awk '{print $2}'` >/dev/null 2>&1
rm -f $PID_FILE
usleep 100
echo "Shutting down $EXEC: [ OK ]"
}

start()
{
echo "Starting $EXEC ..."
$DAEMON > /dev/null &
pidof $EXEC > $PID_FILE
usleep 100
echo "Starting $EXEC: [ OK ]"
}

restart()
{
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status -p $PID_FILE $DAEMON
;;
*)
echo "Usage: service $EXEC {start|stop|restart|status}"
exit 1
esac

exit $?

2、注冊服務

# chmod 700 test.sh

# cp test.sh /etc/init.d/

# chkconfig --add test.sh

# chkconfig --list

3、刪除服務

# chkconfig --del test.sh

Copyright © Linux教程網 All Rights Reserved