Start a daemon with php
Asked Answered
M

2

6

I want to be able to stop/start a daemon (on Debian) by clicking a button on a website. I know the daemon works, because via SSH it does start and stop. I can even capture the status with

$status = exec("/etc/init.d/MyService.sh status | grep 'is running'");

But this doesn't work:

exec('/etc/init.d/MyService.sh start', $output);

There I get this error: Starting system MyService daemon: failed! I guess it has to do with permissions but I don't know how. The permissions of the .pid file is -rwxrw-rw-

I read this https://mcmap.net/q/1916902/-use-php-to-start-restart-process and this Starting a daemon from PHP but both didn't help either.

Any ideas?

Thanks

Magavern answered 25/4, 2014 at 14:9 Comment(3)
-rwxrw-rw- means only the owner can execute the file.. are you sure php is running as the owning user? You could do a chmod +x and try againOkeefe
When using fcgi, just change the script's owner to the one you want the service to run.False
Changed it to -rwxr-xr-x now. Still no success.Magavern
T
1

This is not certain, but a good guess would be that your php runs under a a different user than your ssh one. The one you use on ssh has some rights, the one under which php is running has others.

You can:

  1. Change your php user to be the same with the ssh one

  2. Change your file permissions to something like 777 (if security is not an issue)

  3. exec('sudo /etc/init.d/MyService.sh start', $output); - if you have sudo

  4. Change your file owner (chown)

Transfusion answered 25/4, 2014 at 14:15 Comment(4)
Probably a stupid question, but how can I find out what the php user is? Is it the one under which Apache runs?Magavern
Is it possible that the permissions issue resides with the PID file? I have tried all (chmod +x, chown) on the script file in etc/init.d/, but not on the PID file as it doesn't persist.Magavern
It is possible the process call something else and gets stuck there... Try changing the php user, if this doesn't work then at least you know the problem is somewhere else.Transfusion
changed the user in /etc/apache2/envvars now to the same user I run the script via SSH - still no successMagavern
C
1

You could use immortal a supervisor that allows you to manage a process via HTTP either locally or remotely by using Nginx to expose the "control" Unix-socket. You could stop/start the process, send custom signals besides querying the current status and get the reply in JSON.

For example, this is a run.yml used to start the command sleep:

cmd: sleep 30
log: /var/log/sleep.log

If immortaldir is used to start services on boot time, it will create the "control" socket here:

/var/run/immortal/sleep/immortal.sock

Therefore you could configure Nginx like this:

upstream immortal {
    server unix:/var/run/immortal/sleep/immortal.sock;
}

server {
listen 80 default_server;
server_name _;
location / {
    proxy_pass http://immortal/;
    proxy_http_version 1.1;  
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    }
}

You may need to change permission to the socket:

chmod 766 /var/run/immortal/sleep/immortal.sock

Then via your browser, you could stop:

http://<domain>/signal/stop

Or start your process:

http://<domain>/signal/start

Further steps would be to secure the URL since everyone with access to the web server could stop/start your processes

Crist answered 21/8, 2018 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.