用法
作为非守护进程
运行
daemonize no
作为守护进程
运行
daemonize yes
注意事项:
默认情况下,Redis不作为守护进程运行。
如果以守护进程运行,会创建一个.pid
文件存储进程号。
redis源码
条件判断
// https://github.com/redis/redis/blob/6.2.6/src/server.c
7996 int background = server.daemonize && !server.supervised;
7997 if (background) daemonize();
启动逻辑
// https://github.com/redis/redis/blob/6.2.6/src/server.c
7121 void daemonize(void) {
7122 int fd;
7123
7124 if (fork() != 0) exit(0); /* parent exits */
7125 setsid(); /* create a new session */
7126
7127 /* Every output goes to /dev/null. If Redis is daemonized but
7128 * the 'logfile' is set to 'stdout' in the configuration file
7129 * it will not log at all. */
7130 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
7131 dup2(fd, STDIN_FILENO);
7132 dup2(fd, STDOUT_FILENO);
7133 dup2(fd, STDERR_FILENO);
7134 if (fd > STDERR_FILENO) close(fd);
7135 }
7136 }
原生注释
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no
本文属于原创,首发于微信公众号【小易哥学呀学】,如需转载请后台留言。
加微信入交流群。vx:17610015120