daemon的建立和使用
linux
建立守護進的關鍵步驟:數據庫
step 1.建立子進程,父進程退出ubuntu
step 2.在子進程中建立新會話bash
step 3.改變當前目錄爲根目錄session
step 4.重設文件權限掩碼app
step 5.關閉文件描述符ide
如下是一個簡單的daemon例子this
代碼:spa
附1rest
附2
將file.c編譯成名爲 simple-daemon
而後在腳本daemon-script中編輯程序的名字和路徑
將腳本daemon-script 複製到/etc/init.d/目錄下
而後執行 chkconfig - -add daemon-script
能夠看到chkconfig默認在run level 2 3 4 5下經過deamon-script腳原本啓動指定的程序simple-daemon,在run level 0 1 6下經過deamon-script腳原本關閉指定的程序simple-daemon。
也能夠在terminal中以管理員的身份執行
service daemon-script start //啓動,固然系統默認是開啓的
service daemon-script stop //中止
service daemon-script restart //重啓
附1:
//this app write the system time to testFile every two seconds //這個程序將會每間隔2秒在文件中寫入系統時間(date),你能夠在下面 arr 中定義文件的位置 // file.c // // Created by Jialin Wu on 11/12/13. // #include <stdio.h> #include <stdlib.h> #include <time.h> #include <fcntl.h> void daemonize(void) { pid_t pid; /* * Become a session leader to lose controlling TTY. *建立子進程,父進程退出 */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } else if (pid != 0) /* parent */ exit(0); //在子進程中建立新會話 setsid(); /*改變當前目錄爲/目錄 *若是程序要與數據庫有關,可能會在更改當前目錄後出不現莫名奇妙的錯誤,可嘗試不更改當前目錄 * Change the current working directory to the root. * if you are using database just ignore this and comment them. */ /*if (chdir("/") < 0) { perror("chdir"); exit(1); } */ //重設文件權限掩碼 umask(0); /*關閉文件描述符 * */ close(0); close(1); close(2); //Attach file descriptors 0, 1, and 2 to /dev/null.可選 open("/dev/null", O_RDWR); dup2(0, 1); dup2(0, 2); } int main(int argc, const char *argv[]) { int file; struct tm* newtime; time_t ttime; daemonize(); //這個functione的做用就是使之成爲daemon while(1){ ttime=time(NULL); newtime = localtime(&ttime); char* arr= "/Users/jialin/Desktop/testFile"; //you might change the file path you want file = open(arr,O_WRONLY|O_CREAT); system("date >> /Users/jialin/Desktop/testFile"); sleep(2); close(file); } return 0; }
附2:
#!/bin/bash ### BEGIN INIT INFO # # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: This file should be used to construct scripts to be placed in /etc/init.d. # # to use make this script work, for example: # if you are using ubuntu: # update-rc.d scriptName defaults 20 80 # for more information find update-rc.d # if you are using centOS or redHat: # chkconfig --add scriptName # defaults are 2345 on and 016 off so you can ignore the following steps if not try the followings: # chkconfig --level 2345 scriptName on # chkconfig --level 016 scriptNmae off # for more information find chkconfig ### END INIT INFO ########################### #start writing the script# ########################### # Source function library choose a library according to your linux distribution and now I'm using centOS #選擇function library ,不一樣的Linux發行版function library 的位置不不相同 ##ubuntu## #. /lib/lsb/init-functions 若是在是ubuntu下的,把這句去掉註釋,把下面centOS 加註釋 ##ubuntu## ##centOS or redHat## 我如今用的是centOS . /etc/rc.d/init.d/functions ##centOS or redHat## ## Fill in name of program here. PROG="guetoj_scheduler" PROG_PATH="/home/jialin/judger" ## Not need, but sometimes helpful (if $PROG resides in /opt for example). start() { $PROG_PATH/$PROG echo "$PROG started" } stop() { echo "begin stop" killall $PROG echo "$PROG stopped" } # Check to see if we are running as root first. if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi case "$1" in start) start exit 0 ;; stop) stop exit 0 ;; reload|restart|force-reload) stop start exit 0 ;; **) echo "Usage: $0 {start|stop|reload}" 1>&2 exit 1 ;; esac