Let Test2 directly become the Guardian process
[[email protected] 02]# cat test2.c //test2#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<sys/stat.h>#include<stdlib.h>int main(){ if(daemon(1,1)==-1){ perror("daemon error"); exit(EXIT_FAILURE); } int i=0; while(1){ FILE *fd=fopen("test2.log","a"); fprintf(fd,"%d 1552227\n",i++); sleep(1); fclose(fd); } return 0;}[[email protected] 02]#
The execution of Test2 has become a test2.log file, and the constantly updated test2.log indicates that it is constantly running
32 155222733 155222734 155222735 155222736 155222737 155222738 155222739 155222740 1552227[[email protected] 02]#
Exit the current terminal, ( no longer prompted for a task in progress ) to enable the new terminal, and then observe Test2.log
The discovery program is still performing normally
Use
View All Daemons
2761 ? Sl 0:00 \_ /usr/libexec/evolution-addressbook-factory-subpr 2766 ? Sl 0:00 /usr/libexec/ibus-x11 --kill-daemon 2837 ? Sl 0:00 /usr/libexec/gvfsd-network --spawner :1.4 /org/gtk/g 2852 ? Sl 0:00 /usr/libexec/gvfsd-dnssd --spawner :1.4 /org/gtk/gvf 9158 ? Ss 0:00 ./test2 9187 ? Sl 0:00 /usr/sbin/abrt-dbus -t133[[email protected] 02]#
./test2 is a top-level process with no parent process ( unlike the previous-bash as the parent process )
To print information in the process, you can use the method that is output to log, but note the points
- Write the file close every time, otherwise it is not written in
- To control the program write log, otherwise it will produce a GB of text log, multiple write log programs can run at the same time may be down the system
Linux Guard Service-the role, purpose, and characteristics of the parent process identity of the daemon