使用 supervisor 服务,将程序监控起来,若是程序挂掉了,能够实现自启动c++
编写 c++ 程序 test.capp
#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; }
启动服务ssh
supervisord -c /etc/supervisord.conf测试
# 使用了默认的配置文件 在 /etc/ 下ui
要给须要自拉起的程序添加配置文件 默认放在 /etc/supervisor.d/ 目录下,以 .conf 文件结尾spa
测试程序为 test.confrest
[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 命令来进入控制台code
[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>
进入以后,看到的是在监控的程序的名称blog
使用 help 能够看服务支持哪些自命令rem
通常经常使用的包括
reload
从新加载,这样某个新添/删除的服务就能够看到了
shutdown 关闭某个程序
start 启动某个程序