Shell脚本注册到Linux系统服务实例

2019/7/10 21:46:50

本文主要是介绍Shell脚本注册到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


这篇关于Shell脚本注册到Linux系统服务实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


暂无数据...
扫一扫关注最新编程教程