Background daemon under Linux system

  
I believe that with the popularity of Linux, everyone is writing a lot of programs under Linux, especially some server programs are slowly transferred to Linux. Everyone must encounter the problem is how to make their own process become a daemon, run when the system starts, and always be a background process. Write a program like this below. The source code is as follows:
////////////////////////////////////init_daemon.c Start //////////////////////////////////////////////// #include
#include
#include< Br> #include
#include
/************************************* ********************************
*filename: init_daemon.c
*purpose: Generate background processes
*wrote by: zhoulifa 周立发
*date time:2006-03-10 01:00:00
*Thanks to: Beijing University of Technology Xiaohu
)
*Note: Anyone You can copy and use the code arbitrarily, including your commercial use of course
* but please follow the GPL
************************ *********************************************/
void Init_daemon(void)
{
int pid;
int i;
if(pid=fork())
exit(0);//is the parent process, ending the parent process
> else if(pid< 0)
exit(1);//fork fails, exit
//is the first child process, the background continues to execute
setsid();//the first child process becomes New session leader and process leader
//and separate from the control terminal
if(pid=fork())
exit(0);//is the first Process, end the first child process
else if(pid< 0)
exit(1);//fork fails, exit
//is the second child process, continue
//second The child process is no longer the session group length
for(i=0;i< NOFILE;++i)//Close the open file descriptor
close(i);
chdir("/tmp" ;);//Change the working directory to /tmp
umask(0);//Reset file creation mask
return;
}
///////////////////////////////init_daemon.c End ///////////////////////////////////////////////////
This is a generic function that will make your program a daemon.
Here is a test program: /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Br> /*********************************************** **********************
*filename: daemontest.c
*purpose: Test background process
*wrote by: zhoulifa周立发< Br> *date time:2006-03-10 01:09:00
*Thanks to: Beijing University of Technology Xiaohu
*Note: Anyone can copy the code and use the code, including your business. Use
* But please follow the GPL
************************************** *******************************/
void init_daemon(void);//Daemon initialization function
Void signal_reload(int signal)
{
/* The program re-imports all parameters when receiving the SIGUSR1 signal*/
FILE *fp;
time_t t;
if((fp=fopen ("test.log","a")) >=0)
{
t=time(0);
fprintf(fp,"I received signal(%d), Reload all parameters at %s\ ", signal, asctime(localtime(&t)) );
f Close(fp);
}
/*Re-import parameters*/
}
void signal_handle(int signal)
{
/* Program exits when SIGISR2 signal is received* /
FILE *fp;
time_t t;
if((fp=fopen("test.log","a")) >=0)
{
t =time(0);
fprintf(fp,"I received signal(%d), exit at %s\ ", signal, asctime(localtime(&t)) );
fclose(fp );
}
exit(0);
}
int main(int argc, char ** argv)
{
FILE *fp;
time_t t;< Br> init_daemon();//Initialize to Daemon
signal(SIGCHLD, SIG_IGN); /* Ignore the subprocess exit signal, if a subprocess is generated after that,
If this signal is not processed, it will be The child process exits to generate a zombie process*/
signal(SIGUSR1, signal_reload); /* handles the SIGUSR1 signal, which can be defined as a redirection parameter signal*/
signal(SIGUSR2, signal_handle); /* handles SIGUSR2 Signal, you can define this signal as the exit signal */
while(1)//report the running status to test.log every minute
{
/*This is the main part of each program, so Completed below, such as http services, FTP services, etc. */
sleep(60);//sleep one minute
if((fp=fopen("test.log","a")) >=0)/* Note that since the path has been switched to /tmp in init_daemon, this file is /tmp/test.log*/
{
t=time(0);
Fprintf(fp,"I'm here at %s\ ",asctime(localtime(&t)) );
fclose(fp);
}
}
}
///////////////////////////////////daemontest.c end ////////////////////////////////////////////////
Compile with command to compile:
gcc init_daemon.c daemontest.c< Br> Generated a program such as a.out
You can run this program. /a.out.
If you want this program to run automatically when the system starts, you can add a line with the su command in /etc/rc.d/rc.local, for example:
su - Jacky -c "/bin /a.out"
This command will run the /bin/a.out program as the Jacky user.
If you modify the parameters of the program during the running of the program, you will definitely want the program to re-import the parameters, then you can use Ps command to view the process number of the program:
ps -ef
Copyright © Windows knowledge All Rights Reserved