From: http://www.cppblog.com/sunicdavy/archive/2012/02/28/166680.html1.1 add daemon for Process
Reference http://yubosun.akcms.com/tech/linux-daemon-program.htm
1: #include <unistd.h>
2: #include <signal.h>
3: #include <sys/types.h>
4: #include <sys/stat.h>
5: #include <stdio.h>
6: #include <stdlib.h>
7:
8: #ifndef NOFILE
9: #define NOFILE 3
10: #endif
11:
12: void init_daemon()
13: {
14: int pid;
15: int i;
16: If (pid = fork () Exit (0); // parent process, exit
17:
18: else if (PID <0) Exit (1); // fork failed
19:
20:/* continue executing the sub-process */
21: setsid (); // create a new session group. The sub-process becomes the group leader and is separated from the control terminal.
22:
23:/* prevent sub-processes (Team Leaders) from obtaining control terminals */
24: If (pid = fork () Exit (0); // parent process, exit
25:
26: else if (PID <0) Exit (1); // fork error, exit
27:
28:/* the second sub-process continues to be executed. The second sub-process is no longer a session group leader */
29:
30: // for (I = 0; I <nofile; I ++)/* close open file descriptor */
31: //{
32: //close(i);
33: //}
34: chdir ("/tmp");/* switch the working directory */
35: umask (0);/* reset the file creation mask */
36: return;
37: }
38:
39: int main(int argc, char* argv[])
40: {
41: FILE *fp;
42:
43: Signal (sigchld, sig_ign);/* ignore the subprocess end signal to prevent zombie processes */
44:
45: init_daemon();
46:
47: while(1)
48: {
49: sleep(1);
50:
51: // note: the log is written to this directory.
52: if((fp = fopen("/var/tmp/test.log", "a")) != NULL)
53: {
54: fprintf(fp, "%s\n", "test message");
55: fclose(fp);
56: }
57: }
58:
59: return 0;
60: }
1.2 write service scripts
Reference http://blog.sina.com.cn/s/blog_57421ff80100c7nn.html
The red text is the part to be filled in, and the file header can be optional
1: #!/bin/bash
2:
3: # chkconfig: 3 3 1
4:
5: # description: web kill center
6:
7: EXEC_PATH=/usr/local/bin
8:
9: EXEC=CenterServiced
10:
11: PID_FILE=/var/run/CenterServiced.pid
12:
13: DAEMON=/usr/local/bin/CenterServiced
14:
15: if ! [ -x $EXEC_PATH/$EXEC ] ; then
16:
17: echo "ERROR: $EXEC_PATH/$EXEC not found"
18:
19: exit 1
20:
21: fi
22:
23: stop()
24:
25: {
26:
27: echo "Stoping $EXEC ..."
28:
29: killall $DAEMON >/dev/null
30:
31: echo "Shutting down $EXEC: [ OK ]"
32:
33: }
34:
35: start()
36:
37: {
38:
39: echo "Starting $EXEC ..."
40:
41: $DAEMON > /dev/null &
42:
43: echo "Starting $EXEC: [ OK ]"
44:
45: }
46:
47: restart()
48:
49: {
50:
51: stop
52:
53: start
54:
55: }
56:
57: case "$1" in
58:
59: start)
60:
61: start
62:
63: ;;
64:
65: stop)
66:
67: stop
68:
69: ;;
70:
71: restart)
72:
73: restart
74:
75: ;;
76:
77: status)
78:
79: status -p $PID_FILE $DAEMON
80:
81: ;;
82:
83: *)
84:
85: echo "Usage: service $EXEC {start|stop|restart|status}"
86:
87: exit 1
88:
89: esac
90:
91: exit $?
92:
1.3 create a service
Reference http://hi.baidu.com/guanxiansun/blog/item/b4c7dcf55f6011e47709d724.html
Copy the service file to/etc/init. d and remove the extension. The file name is the service name.
Chmod + X./wkcenter
If startup is not set, the service cannot be found and operated.
1.4 set the startup sequence
Create startup Link
Ln/etc/init. d/wkcenter/etc/rc3.d/s03wkcenter
Create close link
Ln/etc/init. d/wkcenter/etc/rc0.d/k03wkcenter
1.5 Add a service
Chkconfig -- add wkcenter
Check whether the service exists
Chkconfig -- list | grep wkcenter
View service status
Chkconfig wkcenter on
Note: Make sure that wkcenter is enabled in any or part of 2, 3, 4, 5. It must be green. The gray text indicates that the service cannot be started or has other problems.
1.6 Test
Temporarily enable command Test
Service wkcenter start
1.7 QA
Reference link: http://blog.526net.com /? P = 1706
1. Do not place the service in the user's home directory. It is best to place the service in the/usr/local/bin directory and write the log to var. Otherwise, the service test is normal but cannot be started automatically.
2. in Linux, the life cycle of the program started by the parent process follows the parent process. The parent process can be a terminal. Once the parent process is terminated, all child processes must end. therefore, the daemon process must be detached from the parent process to avoid being controlled by the lifecycle of the parent process.