美文网首页
linux将可执行文件注册成服务

linux将可执行文件注册成服务

作者: 哆啦在这A梦在哪 | 来源:发表于2020-06-27 14:04 被阅读0次

    1.准备好你的可执行文件

    这里以 hello 为例

    执行./hello
    输出 hello

    2.在/etc/init.d/目录下生成hello.sh脚本

    hello.sh:

    #!/bin/bash 
    SERVERNAME="hello" 
    start()
    {
        echo "start $SERVERNAME"
        /home/yao/projects/$SERVERNAME
        echo "start $SERVERNAME ok!"
        exit 0;
    }
     
    stop()
    {
        echo "stop $SERVERNAME"
        killall $SERVERNAME
        echo "stop $SERVERNAME ok!"
    }
     
    case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "usage: $0 start|stop|restart"
        exit 0;
    esac
    exit
    

    3.更改脚本文件属性

    chmod +x hello.sh
    

    4.运行

    (1)启动:

    root@localhost:/home/yao/projects# service hello.sh start
    start hello
    hello world
    

    (2)停止:

    root@localhost:/home/yao/projects# service hello.sh stop
    stop hello
    stop hello ok!
    

    (3)重启:

    root@localhost:/etc/init.d# service hello.sh restart
    stop hello
    stop hello ok!
    start hello
    hello world
    hello world
    

    5.完事了!

    相关文章

      网友评论

          本文标题:linux将可执行文件注册成服务

          本文链接:https://www.haomeiwen.com/subject/ognmfktx.html