Instance parsing: write a simple Linux Service Control script

Source: Internet
Author: User
Article Title: instance parsing: compile a simple Linux Service Control script. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems, open-source and other basic categories, first compiled a program used as a service, the function is very simple, write the current time to a file every second:
 
  
     
      void recordTime(){    const char pa[256] = "//home//projects//testService//recordTime";    ofstream fout;    fout.open(pa, ios::app);    time_t currTime;    struct tm *tp;    char buf[256];    while(1)    {        currTime = time(NULL);        tp = localtime(&currTime);        strftime(buf, 256, "%B %e, %Y, %H:%M:%S", tp);        fout<<"current time is "<
      <<
     


 
Then compiled into an executable file. I named it testService.
 
Then place a script file in/etc/init. d, which contains the function implementation of the Service Startup, shutdown, and restart:
 
  

     
      start(){    echo "start testService"    /home/projects/testService/testService &    exit 0;}stop(){    echo -n "stop testService"    if pkill testService        then        echo  "              [ok]"    else        echo  "              [failed]"    fi}case "$1" instart)    start    ;;stop)    stop    ;;restart)    stop    start    ;;*)    echo "usage: $0 start|stop|restart"    exit 0;esac
     

 
All functions are clear at a glance. Call the start () function to start the testService program in the background. stop () is used to stop the program. restart () simply executes the stop () and start () functions. When you need to start or stop the service, you only need to give the program a start/stop parameter.
 
In this way, this simple service is complete.
 

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.