Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a daemon from PHP

For a website, I need to be able to start and stop a daemon process. What I am currently doing is

exec("sudo /etc/init.d/daemonToStart start");

The daemon process is started, but Apache/PHP hangs. Doing a ps aux revealed that sudo itself changed into a zombie process, effectively killing all further progress. Is this normal behavior when trying to start a daeomon from PHP?

And yes, Apache has the right to execute the /etc/init.d/daemonToStart command. I altered the /etc/sudoers file to allow it to do so. No, I have not allowed Apache to be able to execute any kind of command, just a limited few to allow the website to work.

Anyway, going back to my question, is there a way to allow PHP to start daemons in a way that no zombie process is created? I ask this because when I do the reverse, stopping an already started daemon, works just fine.

like image 635
ThaMe90 Avatar asked Dec 05 '11 14:12

ThaMe90


People also ask

What is a daemon PHP?

A daemon is a Linux program that run in the background, just like a 'Service' on Windows. It can perform all sorts of tasks that do not require direct user input. Apache is a daemon, so is MySQL. All you ever hear from them is found in somewhere in /var/log , yet they silently power over 40% of the Internet.

How do I start the daemon process in Linux?

To create a daemon, you need a background process whose parent process is init. In the code above, _daemon creates a child process and then kills the parent process. In this case, your new process will be a subprocess of init and will continue to run in the background.


1 Answers

Try appending > /dev/null 2>&1 & to the command.

So this:

exec("sudo /etc/init.d/daemonToStart > /dev/null 2>&1 &");

Just in case you want to know what it does/why:

  • > /dev/null - redirect STDOUT to /dev/null (blackhole it, in other words)
  • 2>&1 - redirect STDERR to STDOUT (blackhole it as well)
  • & detach process and run in the background
like image 63
DaveRandom Avatar answered Oct 17 '22 07:10

DaveRandom