5

次のように、RedisのUpstartスクリプトを作成しました。

description "Redis Server"

start on runlevel [2345]
stop on shutdown
expect daemon

exec sudo -u redis /usr/local/bin/redis-server /etc/redis/redis.conf

respawn
respawn limit 10 5

次に、redis.confを介してredisを次のように構成します。

daemonize yes

すべてのドキュメントと私自身の実験によると、Redisはデーモン化された形式で2回フォークし、「expectデーモン」は機能するはずですが、Upstartスクリプトは常に前の親のPID(PID-1)を保持しています。誰かがこれを機能させましたか?

4

2 に答える 2

6

次のupstartconfigは、ubuntu12.04のupstart1.5で、redis.confdaemonizeがyesに設定された状態で機能しているようです。

description "redis server"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

setuid redis
setgid redis
expect fork

exec /opt/redis/redis-server /opt/redis/redis.conf

respawn
于 2012-04-30T17:28:55.617 に答える
3

他の人も同じ問題を抱えています。この要点を参照してください。

デーモン化オプションがアクティブ化されると、Redisはプロセスがすでにデーモンであるかどうかをチェックしません(getppidへの呼び出しはありません)。体系的にフォークしますが、一度だけです。これはやや珍しいことです。他のデーモン化メカニズムでは、getppidの初期チェックとforkを2回(setsid呼び出しの前後)呼び出す必要がある場合がありますが、Linuxではこれは厳密には必要ありません。

デーモン化の詳細については、このFAQを参照してください。

Redisデーモン化機能は非常にシンプルです。

void daemonize(void) {
    int fd;

    if (fork() != 0) exit(0); /* parent exits */
    setsid(); /* create a new session */

    /* Every output goes to /dev/null. If Redis is daemonized but
     * the 'logfile' is set to 'stdout' in the configuration file
     * it will not log at all. */
    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
        if (fd > STDERR_FILENO) close(fd);
    }
}

Upstartのドキュメントには次のように書かれています。

expect daemon
Specifies that the job's main process is a daemon, and will fork twice after being run.
init(8) will follow this daemonisation, and will wait for this to occur before running
the job's post-start script or considering the job to be running.
Without this stanza init(8) is unable to supervise daemon processes and will
believe them to have stopped as soon as they daemonise on startup.

expect fork
Specifies that the job's main process will fork once after being run. init(8) will
follow this fork, and will wait for this to occur before running the job's post-start
script or considering the job to be running.
Without this stanza init(8) is unable to supervise forking processes and will believe
them to have stopped as soon as they fork on startup.

したがって、Redis側でデーモン化を非アクティブ化するか、upstart構成でexpectデーモンではなくexpectforkを使用しようとします。

于 2011-12-30T13:20:23.470 に答える