Examples of compiling the background daemon in Linux-general Linux technology-Linux programming and kernel information. For details, refer to the following section. 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. The problem is how to make your process a daemon, run it at system startup, and always be a background process. Write a program like this below. The source code is as follows:
/// // Start with init_daemon.c //////// ///////////////////////////////////
# Include
# Include
# Include
# Include
# Include
/*************************************** ******************************
* Filename: init_daemon.c
* Purpose: generates background processes.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
* Date time: 2006-03-10 01:00:00
* Thanks to: Beijing University of Technology Hu (this article from http://www.qtcn.org/bbs/simple/index.php? T116.html)
* Note: Anyone can copy and use the code at will, including your commercial use.
* But follow the GPL
**************************************** *****************************/
Void init_daemon (void)
{
Int pid;
Int I;
If (pid = fork ())
Exit (0); // It is the parent process and ends the parent process.
Else if (pid <0)
Exit (1); // fork failed, quit
// It is the first sub-process and continues execution in the background
Setsid (); // The first sub-process becomes the new session leader and process leader
// Separate it from the control terminal
If (pid = fork ())
Exit (0); // It is the first sub-process and ends the first sub-process.
Else if (pid <0)
Exit (1); // fork failed, quit
// The second sub-process. Continue
// The second sub-process is no longer the session leader
For (I = 0; I <NOFILE; ++ I) // close the opened file descriptor
Close (I );
Chdir ("/tmp"); // change the working directory to/tmp
Umask (0); // reset the file to create a mask
Return;
}
/// // Init_daemon.c //////// ///////////////////////////////////
The above is a common function that can make your program a backend daemon.
The following is a test program:
/// // Daemontest. start with c ////////////////////////////////////// /////
# Include
# Include
/*************************************** ******************************
* Filename: daemontest. c
* Purpose: test background processes.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
* Date time: 2006-03-10 01:09:00
* Thanks to: Beijing University of Technology Xiao Hu (this article referred to its article http://www.qtcn.org/bbs/simple/index.php? T116.html)
* Note: Anyone can copy and use the code at will, including your commercial use.
* But follow the GPL
**************************************** *****************************/
Void init_daemon (void); // daemon initialization Function
Void signal_reload (int signal)
{
/* When the SIGUSR1 signal is received, the program re-imports all parameters */
FILE * fp;
Time_t t;
If (fp = fopen ("test. log", "a")> = 0)
{
T = time (0 );
Fprintf (fp, "I have Ed signal (% d), reload all parameters at % s \ n", signal, asctime (localtime (& t )));
Fclose (fp );
}
/* Re-import parameters */
}
Void signal_handle (int signal)
{
/* When the SIGUSR2 signal is received, the program exits */
FILE * fp;
Time_t t;
If (fp = fopen ("test. log", "a")> = 0)
{
T = time (0 );
Fprintf (fp, "I have Ed signal (% d), exit at % s \ n", signal, asctime (localtime (& t )));
Fclose (fp );
}
Exit (0 );
}
Int main (int argc, char ** argv)
{
FILE * fp;
Time_t t;
Init_daemon (); // initialize to Daemon
Signal (SIGCHLD, SIG_IGN);/* ignore the sub-process exit signal. If a sub-process is generated after this,
If this signal is not processed, a zombie process will be generated after the sub-process exits */
Signal (SIGUSR1, signal_reload);/* processes the SIGUSR1 signal. You can define this signal as a major parameter signal */
Signal (SIGUSR2, signal_handle);/* processes the SIGUSR2 signal, which can be defined as an exit signal */
While (1) // report the running status to test. log every minute
{
/* This is the main part of the program, so the work is completed below, such as the http service and FTP service */
Sleep (60); // sleep for one minute
If (fp = fopen ("test. log "," a ")> = 0)/* Note that because the path has been switched to/tmp in init_daemon, the file is/tmp/test. log */
{
T = time (0 );
Fprintf (fp, "I'm here at % s \ n", asctime (localtime (& t )));
Fclose (fp );
}
}
}
/// // Daemontest. c end ////////////////////////////////////// /////
Run the following command to perform the compilation:
Gcc init_daemon.c daemontest. c
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 in/etc/rc. d/rc. local with the su command, for example:
Su-Jacky-c "/bin/a. out"
This command runs the/bin/a. out Program as a Jacky user.
If you modify the program parameters while running the program, you must re-import the program parameters. You can run the ps command to view the program process Number:
Ps-ef | grep a. out
Then run the kill command to send parameters to the program, for example:
Kill-s SIGUSR1 11422
Similarly, if you want to terminate the program, send the SIGUSR2 signal to the program:
Kill-s SIGUSR2 11422
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service