欢迎来到代码驿站!

Shell

当前位置:首页 > 脚本语言 > Shell

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

时间:2020-10-08 13:25:53|栏目:Shell|点击:

注册一个系统服务,开机自启动.

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脚本分析 nginx日志访问次数最多及最耗时的页面(慢查询)

栏    目:Shell

下一篇:解决linux 所有命令无法使用的问题

本文标题:Shell脚本注册到Linux系统服务实例

本文地址:http://www.codeinn.net/misctech/8302.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有