#!/bin/bash
WHOAMI=`whoami`
PID=`ps -u $WHOAMI |grep daemon |awk '{print $1}'`
if(test "$PID" !="")
then
kill $PID
fi
关闭所有守护进程
#!/bin/bash
WHOAMI=`whoami`
PID=`ps -u $WHOAMI |grep daemon |awk '{print $1}'`
if(test "$PID" ="")
then
./daemon
fi
只要创建一次
以下为daemon程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int status = 0;
void setdaemon(void)
{
pid_t pid = fork();
if(pid == -1)
{
return ;
}
if(pid>0)
{
exit(0);
}
if(pid == 0)
{
setsid();//保证控制台和child脱离关系;
chdir("/");//将当前目录改根目录
umask(0);
}
}
void catch_signal (int sign)
{
switch(sign)
{
case SIGINT:
printf("SIG");
exit(0);
case SIGTERM:
printf("KILLED");
exit(0);
case SIGUSR1:
status = 1;
}
}
int main (int arg,char *args[])
{
setdaemon();
signal(SIGINT,catch_signal);
signal(SIGKILL,catch_signal);
signal(SIGTERM,catch_signal);
signal(SIGUSR1,catch_signal);
int i=0;
while(1)
{
if(status == 1)
printf("hello %d\n",i++);
sleep(1);
}
return 0;
}