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