Linux gets console data for the daemon process

Source: Internet
Author: User

Linux provides a daemon function that allows the process to run out of the console and achieve the effect of running in the background. However, after the process runs in the background, the data that was originally output from the terminal console will not be visible. So, how do you get the data back?
Here, the topic of the article revolves around how to get the console data of the background process, the principle of which should start from daemon.
daemon mainly do two things:
1. Create a child process, exit the current process, and create a new session with a child process. This way, the child process will not be closed even if the parent process exits

2, the standard input, standard output, standard error are redirected/dev/null


The daemon implementation is roughly as follows:

int daemonize (int nochdir, int noclose) {int fd;switch (fork ()) {Case-1:return ( -1); case 0:break;default:_exit (EXIT_SUCC ESS);} if (setsid () = = 1) return ( -1), if (Nochdir = = 0) {if (ChDir ("/")! = 0) {perror ("chdir"); return (-1);}} if (Noclose = = 0 && (fd = open ("/dev/null", O_rdwr, 0))! =-1) {if (Dup2 (FD, Stdin_fileno) < 0) {perror ("Dup2 St Din "); return (-1);} if (Dup2 (FD, Stdout_fileno) < 0) {perror ("dup2 STDOUT"); return (-1);} if (Dup2 (FD, Stderr_fileno) < 0) {perror ("dup2 STDERR"); return (-1);} if (fd > Stderr_fileno) {if (Close (FD) < 0) {perror ("close"); return (-1);}} return (0);}
so, to retrieve the console data of the process, simply redirect the standard output to the specified file and then read the file.
The article here is an example of a simple demo (here to complete the process communication through the kill signal, a bit rough)the code is as follows, save as daemon_example.c
#include <signal.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h>static int fd =-1; void sigroutine (int dunno) {switch (dunno) {case sigusr1:fprintf (stderr, "Get a signal--SIGUSR1 \ n"); if (fd! =-1) Close (FD); fd = open ("/tmp/console_temp.log", o_rdwr| o_append| O_creat, 0600); if (fd = =-1) break;dup2 (FD, Stdin_fileno);d up2 (FD, Stdout_fileno);d up2 (FD, Stderr_fileno); break;case sigusr2:fprintf (stderr, "Get a signal--SIGUSR2 \ n"); if (fd! =-1) close (FD); fd = open ("/dev/null", O_RDWR, 0); if (fd = = -1) break;dup2 (FD, Stdin_fileno);d up2 (FD, Stdout_fileno);d up2 (FD, Stderr_fileno); return;} int main () {signal (SIGUSR1, sigroutine); signal (SIGUSR2, Sigroutine);d Aemon (1,0); for (;;) {fprintf (stderr, "Test \ n");//continuously print testsleep (1);} return 0;}
Then, compile and execute this program: $ gcc-oDaemon_example Daemon_example. C$ chmod +x daemon_example $./Daemon_example
$ ps-ef| grep daemon_example
Root 11328 1 0 19:15? 00:00:00./daemon_example
As above, the process backstage run, get PID 11328
Next, write a script to test the program, save it as test.sh:
#!/bin/bashpid=$1ps-p $pid >/dev/nullif [! $-eq 0]; Thenecho PID does not exist!exit 1fiecho pid $pidtrap "KILL-USR2 $pid && exit 1" HUP INT QUIT termkill-usr1 $pid echo It works,please wait. Sleep 1tail-f-N 0/tmp/console_temp.logecho done!
Execute the script with the following results: $./test.sh 11328
PID 11328
It works,please wait.
Test
Testthen, press CTRL + C to exit the script, where the script notifies the process to redirect standard output and standard errors to/dev/null, continuing to run in the background. In this way, this script becomes the debugging tool of the background process, needs the background data to execute once, does not need to shut down. Of course, this is just an example, in the actual application to improve, such as kill signal to pipe or socket communication, cache files to limit the size, or automatic cleanup and so on.

Article at the end, is not a bit trickery, you have what better way to welcome comments exchange!
Reference: [1] Linux Gets the console data for the daemon process no flowering tree

Linux get console data for daemon processes

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.