Linux daemon sample program

Source: Internet
Author: User

# Include <stdio. h> <br/> # include <fcntl. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> <br/> # include <PWD. h> <br/> # include <time. h> <br/> # include <signal. h> </P> <p> int daemon (INT, INT); <br/> char * daemon_log = "/u01/log/daemon. log "; <br/> int add_log (char *, char *); <br/> void sig_handler (INT); <br/> void restart_sleep (INT ); <br/> int is_daemon = 1; <br/> int is_root = 1; </P> <p> int main () <br/> {<br /> Fprintf (stdout, "daemon start/N"); <br/> int ret; <br/> // whether to switch to a common user <br/> If (! Is_root) {<br/> If (getuid () = 0 | geteuid () = 0) {<br/> char * username = "flynetcn "; <br/> struct passwd * PW; <br/> If (PW = getpwnam (username) = 0) {<br/> fprintf (stderr, "Can't find the USER % s/n", username); <br/> return 1; <br/>}< br/> If (setgid (PW-> pw_gid) <0 | setuid (PW-> pw_uid) <0) {<br/> fprintf (stderr, "failed to assume identity of USER % s/n", username); <br/> return 1; <br/>}< br/>} <Br/>}< br/> // prevents zombie sub-processes <br/> struct sigaction SA, oldsa; <br/> SA. sa_handler = sig_handler; <br/> sigemptyset (& SA. sa_mask); <br/> SA. sa_flags = sa_restart; <br/> If (sigaction (sigchld, & SA, & oldsa) =-1) {<br/> fprintf (stderr, "sigaction (sigchld) error/N "); <br/> return 1; <br/>}< br/> // switch to the daemon process <br/> If (is_daemon) {<br/> ret = daemon (1, 0); <br/> If (ret =-1) {<br/> fprintf (stderr, "failed to daemon/n "); <Br/> return 1; <br/>}</P> <p>/** <br/> * Do Something <br/> */<br/> int crontimers [3] [2]; <br/> // Task 1 <br/> crontimers [0] [0] = 3; <br/> crontimers [0] [1] = 6; <br/> // Task 2 <br/> crontimers [1] [0] =-1; <br/> crontimers [1] [1] = 6; <br/> // Task 3 <br/> crontimers [2] [0] =-1; <br/> crontimers [2] [1] = 36; <br/> // tasks comm <br/> char * croncomm [3]; <br/> croncomm [0] = "Echo task1>/dev/null 2> & 1"; <Br/> croncomm [1] = "Echo task2>/dev/null 2> & 1 "; <br/> croncomm [2] = "Echo task3>/dev/null 2> & 1"; <br/> char Buf [2, 1024]; <br/> char tbuf [50]; <br/> time_t timestamp; <br/> struct TM * now; <br/> int time_left; <br/> int cron_num; <br/> cron_num = sizeof (croncomm)/sizeof (char *); <br/> int Hok, I; <br/> while (1) {<br/> timestamp = Time (null); <br/> now = localtime (& timestamp); <br/> strftime (tbuf, 30, "% Y-% M-% d % H: % m: % s ", now); <br/> // <br/> sprintf (BUF," % s ------/N ", tbuf); <br/> add_log (BUF, daemon_log); <br/> for (I = 0; I <cron_num; I ++) {<br/> Hok = 0; <br/> sprintf (BUF, "task % d:/N", I); <br/> add_log (BUF, daemon_log ); <br/> If (crontimers [I] [0] =-1 | now-> tm_hour = crontimers [I] [0]) {<br/> Hok = 1; <br/> add_log ("Hok/N", daemon_log); <br/>} else {<br/> continue; <br/>}< br/> If (crontimers [I] [1] = now-> T M_min & croncomm [I]) {<br/> add_log ("Mok/N", daemon_log); <br/> sprintf (BUF, "Run % s/n ", croncomm [I]); <br/> add_log (BUF, daemon_log); <br/> ret = fork (); <br/> If (ret = 0) {<br/> System (croncomm [I]); <br/> exit (0 ); <br/>}< br/> timestamp = Time (null); <br/> now = localtime (& timestamp ); <br/> time_left = 60-now-> tm_sec; <br/> restart_sleep (time_left ); <br/>}< br/>/** <br/> * end <br/> */ </P> <p >}</P> <p> int add_log (char * STR, char * logfile) <br/>{< br/> file * FP; <br/> If (FP = fopen (logfile, "A") = NULL) {<br/> return-1; <br/>}< br/> If (! (* Str) | fputs (STR, FP) = EOF) {<br/> fclose (FP); <br/> return-1; <br/>}< br/> fflush (FP); <br/> fclose (FP); <br/> return 0; <br/>}</P> <p> int daemon (INT nochdir, int noclose) <br/>{< br/> int FD, RET; <br/> ret = fork (); <br/> switch (RET) {<br/> case-1: Return-1; <br/> case 0: break; <br/> default: exit (0); <br/>}< br/> If (setsid () =-1) <br/> return-1; <br/> If (nochdir = 0) {<br/> If (chdir ("/")! = 0) {<br/> perror ("chdir"); <br/> return-1; <br/>}< br/> If (noclose = 0 & (FD = open ("/dev/null", o_rdwr, 0 ))! =-1) {<br/> If (dup2 (FD, stdin_fileno) <0) {<br/> perror ("dup2 stdin "); <br/> return-1; <br/>}< br/> If (dup2 (FD, stdout_fileno) <0) {<br/> perror ("dup2 stdout"); <br/> return-1; <br/>}< br/> If (dup2 (FD, stderr_fileno) <0) {<br/> perror ("dup2 stderr"); <br/> return-1; <br/>}< br/> If (FD> stderr_fileno) {<br/> If (close (FD) <0) {<br/> perror ("close"); <br/> return-1; <br/>}< br/> return 0; <br/>}</P> <p> void sig_handler (INT sig) <br/>{< br/> If (Sig = sigchld) {<br/> int status; <br/> pid_t PID; <br/> pid = wait (& status ); <br/>}< br/> // prevents signal interruption <br/> void restart_sleep (INT Sec) <br/>{< br/> int left; <br/> left = sec; <br/> while (left> 0) {<br/> left = sleep (left); <br/>}< br/>}

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.