supervisor 配置程序挂起自启动

使用 supervisor 服务,将程序监控起来,如果程序挂掉了,可以实现自启动

编写 c++ 程序 test.c

#include <stdio.h>
#include <string.h>

int main(){
        FILE *fp = fopen("./1.txt","a+");
        if(fp==0){
                printf("can't open file\n");
                return 0;
        }

        int ix = 0;
        for(;;ix++){
                fseek(fp,0,SEEK_END);
                char s_add_arr[10];
                memset(s_add_arr,'\0',10);
                sprintf(s_add_arr,"%i\n",ix);
                fwrite(s_add_arr,strlen(s_add_arr),1,fp);

                sleep(1);
        }
        fclose(fp);
        return 0;
}

启动服务

supervisord  -c /etc/supervisord.conf

# 使用了默认的配置文件 在 /etc/ 下

要给需要自拉起的程序添加配置文件 默认放在 /etc/supervisor.d/ 目录下,以 .conf 文件结尾

测试程序为 test.conf

[program:test]
command=/home/jingchanglin/code/test/test
autostart=true
autorestart=true
startsecs=10
priority=1
redirect_stderr=true
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10
stdout_logfile=/home/jingchanglin/code/test/app.log

服务启动后,可以使用 supervisorctl 命令来进入控制台

[root@localhost]# supervisorctl
sshd                             RUNNING   pid 28606, uptime 0:02:42
test                             RUNNING   pid 28605, uptime 0:02:42
supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

supervisor> 

进入之后,看到的是在监控的程序的名称

使用 help 可以看服务支持哪些自命令

一般常用的包括

reload

重新加载,这样某个新添/删除的服务就可以看到了

shutdown 关闭某个程序

start 启动某个程序

猜你喜欢

转载自www.cnblogs.com/oftenlin/p/9479553.html