开发者

System_Daemon failed to open stream /var/log/mydaemonname.log

I'm trying to run this simple daemon by cli

function doTask(){

    echo 'mytest';
}
// Include PEAR's Daemon Class
require_once "/usr/share/php/System/Daemon.php";

// Bare minimum setup
System_Daemon::setOption("appName", "mydaemonname2");

try{
// Spawn Deamon!
System_Daemon::start();

// Your PHP Here!
while (true) {
    doTask();
}

// Stop daemon!
System_Daemon::stop();
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}



notice: Starting mydaemonname daemon, output in: '/var/log/mydaemonname.log'
[Feb 08 12:17:23]  warning: [PHP Error] file_put_contents(/var/log/mydaemonname.log): failed to open stream: Permission denied 

both in my local host and in my dev server.

If I try to roughly create and set chmod 777 file permission to /var/log/mydaemonname.log I get this other error

[Feb 08 12:30:31]   notice: Starting mydaemonname daemon, output in: '/var/log/mydaemonname.log'
[Feb 08 12:30:31]      err: Unable to create directory: '/var/run/mydaemonname' [l:1366]
[Feb 08 12:30:31]    emerg: Unable to write pid file /var/run/mydaemonname/mydaemonname.pid [l:1272]
[Feb 08 12:30:31]     info: Process was not daemonized yet, just halting current process

and开发者_如何学运维 if I try to set 777 file permission to /var/run/mydaemonname I get this in the log file

 notice: Starting mydaemonname daemon, output in: '/var/log/mydaemonname.log'
[Feb 08 12:17:06]      err: Unable to change group of file '/var/run/mydaemonname/mydaemonname.pid' to 0 [l:1425]
[Feb 08 12:17:06]     crit: Unable to change identity [l:1281]
[Feb 08 12:17:06]    emerg: Cannot continue after this [l:1283]

Can you help me, please ?

Bye


What user do you start the daemon as? if it's not root then this may be you answer:

The options "appRunAsUID" and "appRunAsGID" set the user thart the daemon should switch to use in the child (daemonized) process. It needs to start as user "root" in order to be able to have the correct access to the pidfile.

Therefore you should set the "appRunAsUID" and "appRunAsGID" to an unprivileged user that the daemonized script will run as, but you need to start the process as "root" to be able to write the pid and switch user.


Messages suggest that is a problem of permission, setting "appRunAsUID" and "appRunAsGID" = 0 (GID and UID of user root) you solve the problem.

Another problem related with this kind of problems is that. Since version 0.6.3, the pidfile needs to be in it's own subdirectory. When means that the first daemon run in default appPidLocation that is:

/var/run/< appName >/< daemon_name >.pid

And the second daemon that maybe you will run must coexist in the same directory. When you can define dynamically

        $path_pid =  '/var/run/'.System_Daemon::getOption('appName');
        System_Daemon::setOption("appPidLocation",$path_pid.'/'.$pidFile);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜