# cat shouhu.sh
#!/bin/sh
while true; do
server=`ps aux | grep face-recognition-services-0.0.3 | grep -v grep`
if [ ! "$server" ]; then
sh start.sh
sleep 10
fi
sleep 5
done
# nohup sh shouhu.sh >/dev/null 2>&1 &
C++基础之守护进程
C++基础之守护进程_c++守护进程_qccz123456的博客-优快云博客
#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
#include <cstdlib>
void init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
setsid();
if(pid=fork())
exit(0);
else if(pid< 0)
exit(1);
for(i=0;i< NOFILE;++i)
close(i);
chdir("/tmp");
umask(0);
return;
}
main()
{
FILE *fp;
time_t t;
init_daemon();
while(1)
{
if((fp=fopen("test.log", "a")) >=0)
{
t=time(0);
fprintf(fp,"I'm here at %sn",asctime(localtime(&t)) );
fclose(fp);
}
sleep(1);
}
}