当前位置: 移动技术网 > 网络运营>服务器>Linux > Shell脚本注册到Linux系统服务实例

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

2017年12月12日  | 移动技术网网络运营  | 我要评论

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网