The Linux daemon implementation program runs only once

Source: Internet
Author: User
Tags syslog system log

1. Daemon Process

A daemon (Daemon) is a special process that runs in the background, independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences.

2. Let the program run only once

If you let the program run only once, there are a number of methods, here is a way to create a strange name of the file (guaranteed not with the system files or other files), to determine that the file exists so that the program is no longer running and prompts the program is running, if it does not exist can be run.

3. Test code

This code adds additional system log, which records the 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 Ten #defineFILE "/var/cxl_single_file" One  A //function Declaration - voidCreate_daemon (void); - voidDelete_file (void); the  - intMainintargcChar**argv) - { -     intFD =-1; +  -     //Open Log System +Openlog (argv[0], Log_pid |log_cons, log_user); A      at     //open file, not present, create, present error prompt -FD = open (FILE, O_RDWR | O_trunc | O_creat | O_EXCL,0664); -     if(FD <0 ) -     { -          -         if(errno = =eexist) in         { -printf"%s is running...\n", argv[0]); to_exit (-1); +         } -     } theSyslog (Log_info,"Create file success!"); *      $     //registering process cleanup functionsPanax NotoginsengAtexit (Delete_file);//This function does not run after Kill-9 PID -      the Close (FD); + Closelog (); A      the     //Creating Daemons + Create_daemon (); -      while(1); $      $     return 0; - } -  the //Create_daemon () Create daemon - voidCreate_daemon (void)Wuyi { the     inti =0, cnt =0; -pid_t PID =-1; Wu      -PID =fork (); About     if( -1==pid) $     { -Perror ("Fork"); -_exit (-1); -     } A     if(PID >0 ) +     { the         //child process waits for parent process to exit -_exit (0); $     } the      the     /*here is the child process execution*/ the     //Setsid () child process creates a new session period, leaving the console thePID =Setsid (); -     if( -1==pid) in     { thePerror ("Setsid"); the_exit (-1); About     } the      the     //chdir () Call this function to set the current working directory to/ theChDir"/"); +      -     //umask () set to 0 to cancel any file permission masking theUmask0);Bayi      the     //Close all file descriptors the     //sysconf () Gets the maximum number of file descriptors in the current system -CNT =sysconf (_sc_open_max); -      for(i =0; I < CNT; i++ ) the     { the Close (i); the     } the      -     //position 0, 1, 2 to/dev/null theOpen"/dev/null", O_RDWR); theOpen"/dev/null", O_RDWR); theOpen"/dev/null", O_RDWR);94 } the  the //Deleting Files the voidDelete_file (void)98 { About Remove (FILE); -}

The Linux daemon implementation program runs only once

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.