«

创建Linux系统服务的方法

时间:2024-3-2 15:35     作者:韩俊     分类: Linux


linux环境:ubuntu


#! /bin/sh
# Provides: server.sh
# Default-Start: 2 3 4 5
# Short-Description: server testing...

EXEC_PATH=/home/huangmin/cdev/project
EXEC=server
PID_FILE=/var/run/server.sh.pid
DAEMON=/home/huangmin/cdev/project/server

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

stop() {
echo "Stopping $EXEC ..."
killall $DAEMON >/dev/null
rm -f $PID_FILE
sleep 1
echo "Shutting down $EXEC: [OK]"
}

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

restart() {
stop
start
}

case "$1" in
start)
start

stop)
stop

restart)
restart

status)
echo "Status Here"

*)
echo "Usage: service $EXEC {start|stop|restart|status}"
exit 1
esac

exit $?

/home/huangmin/cdev/project/server是我们需要的作为系统服务的程序。
写好脚本之后,将其放置到目录/etc/init.d中,或者做成软链接形式如:sudo ln -s /home/huangmin/cdev/project/bash/server.sh /etc/init.d/servertest.sh
最后利用chkconfig命令将其添加进系统服务: chkconfig ?add servertest.sh
可以使用命令service servertest.sh start马上启动该服务程序

标签: linux

热门推荐