如何在Centos里面,把.net core程序设为开机自启动

确定你的.net core程序可以在centos手动启动后,下一步,就是把这个程序做成一个服务,让它开机自自动了

1.创建脚本文件

到目录/etc/rc.d/init.d下面,创建一个myserver.sh文件

要用vi生成,从windows生成,拷贝到linux,可能最终会无法启动这个脚本

vi myserver.sh

内容如下:

#!/bin/bash
# chkconfig: 2345 10 30
# description: testServer

dotnet /home/yourapp.dll

#!/bin/bash
符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/bash来执行程序。

# chkconfig: 2345 10 30

其中2345是默认启动级别,级别有0-6共7个级别。

  等级0表示:表示关机   

  等级1表示:单用户模式   

  等级2表示:无网络连接的多用户命令行模式   

  等级3表示:有网络连接的多用户命令行模式   

  等级4表示:不可用   

  等级5表示:带图形界面的多用户模式   

  等级6表示:重新启动

10是启动优先级,90是停止优先级

如果你把启动优先级设为80,开机的时候,小于80的服务会先启动,直到没有低于80的服务,你的服务才会启动

2.设置脚本文件为可执行

sudo chmod +x myserver.sh

3.激活你的服务脚步

chkconfig --add myserver.sh

chkconfig myserver.sh on

 4.测试

用命令启动这个脚本试试看,没有提示错误就是脚本没有问题了

service myserver start

最后重启,发现你的程序已经自动运行了

支持start stop restart

#!/bin/bash
# chkconfig: 2345 10 30
# description: Webapi server

function start(){
        nohup dotnet /data/gateway/JMS.Gateway.dll >/dev/null 2>&1 &
}

function stop(){
     local pid=$(ps aux|grep "dotnet /data/gateway/JMS.Gateway.dll"|grep -v "grep"|awk '{print $2}')
     if [ "$pid" != "" ];then
        echo "found $pid --"
        kill -9 $pid
        echo "killed service"
     else
        echo "no service running"
     fi
}

function restart(){
   stop
   start
}

case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'restart')
        restart
        ;;
    *)
        ;;
esac
exit 0

 方法二、使用Supervisor守护进程

安装supervisor

# epel源

yum install epel-release
# 安装supervisor
yum install -y supervisor
# 开机自启动
systemctl enable supervisord
 
# 主配置文件
/etc/supervisord.conf
# 运行程序配置文件夹
/etc/supervisord.d/
 

在程序配置文件夹/etc/supervisord.d中添加test.ini:

[program:test]
directory=/home/yourfolder
command=dotnet ***.dll
autostart=true
autorestart=true
stderr_logfile=/home/log/error.log
stdout_logfile=/home/log/out.log

systemctl stop supervisord

systemctl start supervisord
systemctl status supervisord
 
 
查看进程状态
supervisorctl status
停止某个进程
supervisorctl stop test
启动某个进程
supervisorctl start test  //test是program名称
supervisorctl restart test
添加新的服务
supervisorctl update
# 重新加载配置文件,不影响正在运行的程序 (不要用reload,所有程序都会重启的)
supervisorctl reread     //发现新的配置文件
supervisorctl add your配置  //拉起指定的配置
 
program配置项说明
;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

 

posted @ 2017-04-18 17:10  IWing  阅读(1906)  评论(0编辑  收藏  举报