Linux Service Daemon Development and Shell script control Daemon__linux

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); > When I was developing Android, I encountered a service component that simply would not require the interface behind the process to silently perform the task. Now let's learn how the daemon service is implemented under Linux, and also learn the switches that control processes through shell scripts. </span>

Daemon implementation has the default mentality fork parent-child process, shut down the parent process, the child process with the console out of the operation, my side of the Daemon demo implementation is through the signal to control the output of the daemon process. First look at the code:

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h > #include <sys/ipc.h> #include <sys/shm.h> #include <signal.h> #include <syslog.h> #include
   <errno.h> int mysignal (int signo,void (*FUNC) (int)) {struct sigaction act,oact;
   Act.sa_handler = func;
   Sigemptyset (&act.sa_mask);
   act.sa_flags = 0; 
   
Return sigaction (signo,&act,&oact);
  } void Setdaemon () {pid_t pid,sid;
  PID = fork ();
    if (PID < 0) {printf ("Fork Failuer:%s\n", Strerror (errno));
  Exit (Exit_failure);
  } if (pid > 0) {exit (exit_success);
    if (SID = Setsid ()) < 0) {printf ("Set SID failure:%s\n", Strerror (errno));
  Exit (Exit_failure);
  } void Listenfifo () {const char* filename = "Shaofifo";
  int len = 0; Char buf[1024];
  memset (buf,0,sizeof (BUF));
  int fd = open (filename,o_rdonly);
    if (fd = = 1) {printf ("Open failure:%s\n", Strerror (errno));
  Exit (Exit_failure);
  Len = Read (fd,buf,sizeof (BUF));
    if (Len > 0) {if (Buf[strlen (BUF)-1] = = ' \ n ') {Buf[strlen (BUF)-1] = 0;
    Close (Stdout_fileno);

  Open (buf,o_wronly);
     } void catch_signal (int signo) {switch (signo) {case Sigint:listenfifo ();

   Break 
  int main (int arg,char* args[]) {Setdaemon ();
  Mysignal (sigint,catch_signal);
     while (1) {puts ("Hello World!!");
  Sleep (1);
return exit_success;
 }

Then we enter another console's device path in the pipe file, and the subprocess is the daemon shuts down the standard output and then receives the signal to read the device path in the pipe file, open the device, and continue the output

In the Shaofifo pipeline file input device path, the child process reads the output content here is mainly to learn the implementation of the Daemon Setsid () method is to let the child process out of the console

The general daemon needs to be controlled by a shell script, look at the implementation of the shell script

#! /bin/sh

whoami= ' WHOAMI '

pid= ' ps-u $WHOAMI | grep Service | awk ' {print '} '

if (test ' $ ' = ' start ') then
  if (test "$PID" = "") then
    ./service
   fi
fi

if (test "=" Stop ") then
  if (test" $PID "!=" ") then
  
   kill $PID
  fi
fi

if (test "$" = "status") then
  if (test "$PID" = "") then
   echo "not run"
  fi< C16/>if (test "$PID"!= "") then
   echo "Run"
  fi   

fi

  



Over here. The implementation of the daemon through shell scripting ends here.


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.