The Linux daemon only runs once.

Source: Internet
Author: User

The Linux daemon only runs once.

1. Daemon

Daemon is a special process running in the background. It is independent of the control terminal and periodically executes a task or waits for some events to be processed.

2. Run the program only once.

There are many ways to run the program only once. One method here is to create a file with an odd name (ensure that it is not the same as the system file or other files ), if a file exists, the program is no longer running and the system prompts that the program is running. If the file does not exist, the program can be run.

3. Test code

This Code adds additional system logs to record operation information.

1 # include <stdio. h> 2 # include <unistd. h> 3 # include <stdlib. h> 4 # include <syslog. h> 5 # include <sys/types. h> 6 # include <sys/stat. h> 7 # include <fcntl. h> 8 # include <errno. h> 9 10 # define FILE "/var/cxl_single_file" 11 12 // function declaration 13 void create_daemon (void); 14 void delete_file (void ); 15 16 int main (int argc, char ** argv) 17 {18 int fd =-1; 19 20 // open LOG system 21 openlog (argv [0], LOG_PID | LOG_CO NS, LOG_USER); 22 23 // open the FILE. If the FILE does not exist, create it. If yes, the error 24 fd = open (FILE, O_RDWR | O_TRUNC | O_CREAT | O_EXCL, 0664) is returned ); 25 if (fd <0) 26 {27 28 if (errno = EEXIST) 29 {30 printf ("% s is running... \ n ", argv [0]); 31 _ exit (-1); 32} 33} 34 syslog (LOG_INFO," Create file success! "); 35 36 // The registration process cleanup function 37 atexit (delete_file); // after the Kill-9 PID, this function does not run 38 39 close (fd); 40 closelog (); 41 42 // create daemon 43 create_daemon (); 44 while (1); 45 46 return 0; 47} 48 49 // create_daemon () create a daemon 50 void create_daemon (void) 51 {52 int I = 0, cnt = 0; 53 pid_t pid =-1; 54 55 pid = fork (); 56 if (-1 = pid) 57 {58 perror ("fork"); 59 _ exit (-1); 60} 61 if (pid> 0) 62 {63 // The sub-process waits for the parent process to exit 64 _ exit (0); 65} 66 67/* sub-process execution */68 // setsid () the sub-process creates a new session from the console 69 pid = setsid (); 70 if (-1 = pid) 71 {72 perror ("setsid "); 73 _ exit (-1); 74} 75 76 // chdir () call this function to set the current working directory to/77 chdir ("/"); 78 79 // umask () is set to 0 to cancel any File Permission shielding 80 umask (0); 81 82 // close all file descriptors 83 // sysconf () obtain the maximum number of file descriptors in the current system. 84 cnt = sysconf (_ SC _OPEN_MAX); 85 for (I = 0; I <cnt; I ++) 86 {87 close (I); 88} 89 90 // locate 0, 1, and 2 to/dev/null 91 open ("/dev/null", O_RDWR ); 92 open ("/dev/null", O_RDWR); 93 open ("/dev/null", O_RDWR); 94} 95 96 // delete the file 97 void delete_file (void) 98 {99 remove (FILE); 100}

 

Related Article

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.