Linux伺服器守護進程+自動啟動+服務配置筆記

來源:互聯網
上載者:User
轉自: http://www.cppblog.com/sunicdavy/archive/2012/02/28/166680.html1.1 為進程添加守護進程

參考連結http://yubosun.akcms.com/tech/linux-daemon-program.htm

   1:  #include <unistd.h> 
   2:  #include <signal.h> 
   3:  #include <sys/types.h> 
   4:  #include <sys/stat.h> 
   5:  #include <stdio.h> 
   6:  #include <stdlib.h>
   7:   
   8:  #ifndef NOFILE 
   9:  #define NOFILE 3 
  10:  #endif
  11:   
  12:  void init_daemon() 
  13:  { 
  14:          int pid; 
  15:          int i; 
  16:          if(pid = fork()) exit(0); //父進程,退出 
  17:   
  18:          else if(pid < 0) exit(1); //fork失敗 
  19:   
  20:          /* 子進程繼續執行 */ 
  21:          setsid(); //建立新的交談群組,子進程成為組長,並與控制終端分離 
  22:   
  23:          /* 防止子進程(組長)擷取控制終端 */ 
  24:          if(pid = fork()) exit(0); //父進程,退出 
  25:   
  26:          else if(pid < 0) exit(1); //fork錯誤,退出 
  27:   
  28:          /* 第二子進程繼續執行 , 第二子進程不再是會交談群組組長*/ 
  29:   
  30:          //for(i = 0; i < NOFILE; i++) /* 關閉開啟的檔案描述符*/ 
  31:          //{ 
  32:          //close(i); 
  33:          //} 
  34:          chdir("/tmp"); /* 切換工作目錄 */ 
  35:          umask(0); /* 重設檔案建立掩碼 */ 
  36:          return; 
  37:  }
  38:   
  39:  int main(int argc, char* argv[])
  40:  {    
  41:      FILE *fp; 
  42:   
  43:      signal(SIGCHLD, SIG_IGN); /* 忽略子進程結束訊號,防止出現殭屍進程 */ 
  44:   
  45:      init_daemon(); 
  46:   
  47:      while(1) 
  48:      { 
  49:              sleep(1);
  50:              
  51:  // 注意, 日誌寫到這個目錄
  52:              if((fp = fopen("/var/tmp/test.log", "a")) != NULL) 
  53:              { 
  54:                      fprintf(fp, "%s\n", "test message"); 
  55:                      fclose(fp); 
  56:              } 
  57:      } 
  58:   
  59:      return 0;
  60:  }
1.2 編寫服務指令碼

參考連結http://blog.sina.com.cn/s/blog_57421ff80100c7nn.html

紅色字是需要填寫的部分, 檔案頭部分可以選填

   1:  #!/bin/bash
   2:   
   3:  # chkconfig: 3 3 1
   4:   
   5:  # description: web kill center
   6:   
   7:  EXEC_PATH=/usr/local/bin
   8:   
   9:  EXEC=CenterServiced
  10:   
  11:  PID_FILE=/var/run/CenterServiced.pid
  12:   
  13:  DAEMON=/usr/local/bin/CenterServiced
  14:   
  15:  if ! [ -x $EXEC_PATH/$EXEC ] ; then
  16:   
  17:  echo "ERROR: $EXEC_PATH/$EXEC not found"
  18:   
  19:  exit 1
  20:   
  21:  fi
  22:   
  23:  stop()
  24:   
  25:  {
  26:   
  27:  echo "Stoping $EXEC ..."
  28:   
  29:  killall $DAEMON >/dev/null
  30:   
  31:  echo "Shutting down $EXEC: [ OK ]"
  32:   
  33:  }
  34:   
  35:  start()
  36:   
  37:  {
  38:   
  39:  echo "Starting $EXEC ..."
  40:   
  41:  $DAEMON > /dev/null &
  42:   
  43:  echo "Starting $EXEC: [ OK ]"
  44:   
  45:  }
  46:   
  47:  restart()
  48:   
  49:  {
  50:   
  51:  stop
  52:   
  53:  start
  54:   
  55:  }
  56:   
  57:  case "$1" in
  58:   
  59:  start)
  60:   
  61:  start
  62:   
  63:  ;;
  64:   
  65:  stop)
  66:   
  67:  stop
  68:   
  69:  ;;
  70:   
  71:  restart)
  72:   
  73:  restart
  74:   
  75:  ;;
  76:   
  77:  status)
  78:   
  79:  status -p $PID_FILE $DAEMON
  80:   
  81:  ;;
  82:   
  83:  *)
  84:   
  85:  echo "Usage: service $EXEC {start|stop|restart|status}"
  86:   
  87:  exit 1
  88:   
  89:  esac
  90:   
  91:  exit $?
  92:   
1.3 建立服務

參考連結http://hi.baidu.com/guanxiansun/blog/item/b4c7dcf55f6011e47709d724.html

將服務檔案拷貝到/etc/init.d下,去掉副檔名, 檔案名稱即是服務名

chmod +x ./wkcenter

如果不設定啟動, 那麼service中將無法找到該服務及操作

1.4 設定啟動順序

建立啟動連結

ln /etc/init.d/wkcenter /etc/rc3.d/S03wkcenter

建立關閉連結

ln /etc/init.d/wkcenter /etc/rc0.d/K03wkcenter

1.5 添加服務

chkconfig --add wkcenter

查看服務是否存在

chkconfig –-list | grep wkcenter

查看服務狀態

chkconfig wkcenter on

注意, 確認wkcenter在2,3,4,5中任意或者部分開啟, 必須為綠字. 灰字代表格服務無法開機啟動或者其他問題

1.6 測試

臨時開啟命令測試

service wkcenter start

1.7 QA

參考連結: http://blog.526net.com/?p=1706

1. 服務切記不可放在使用者home目錄, 最好放在/usr/local/bin目錄, 日誌寫到var中, 否則服務測試正常,但是無法自動啟動

2. Linux下, 父進程啟動的程式的生命期跟隨父進程, 父進程可以是終端, 父進程一旦終止, 子進程都必須結束. 因此守護進程需要脫離父進程,避免被父進程生命期控制

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.