Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wrote a small script to run as a daemon, everything seems to work when I run the command in unix:

php script.php

everything seems to work correctly, when I terminate that script and us the following, de script isn't running in the background:

php script.php &

What am I doing wrong? could it be a server problem or a script problem?

share|improve this question

migrated from stackoverflow.com Oct 20 '11 at 13:44

This question came from our site for professional and enthusiast programmers.

Maybe your script needs input from stdin? – dacwe Oct 20 '11 at 12:48
no it doesn't,it's just a test file that writes a string in a log file – Maurits Meester Oct 20 '11 at 12:57
In-depth discussion on php scripts as daemons: see this stackoverflow post. – sr_ Oct 20 '11 at 14:04

even if php doesn't look like it's going to write output, it does prepare itself to write headers at the very least. that behavior can be suppressed by using the -q option with php.

php -q script.php >/dev/null 2>&1 &

should get you where you need to be.

if you want to see whether or not php really ouputs anything, try

php -q script.php >/tmp/script.log 2>&1 &

Then see if /tmp/script.log is still 0 bytes after the script has run.

share|improve this answer

Try this php script.php > /dev/null 2>&1 &

share|improve this answer
this is not working either – Maurits Meester Oct 20 '11 at 13:23
Sorry to hear that. Clarify what you mean by not working. Also what happens when you just execute the script using php script.php? Do you see any output? – Dmitri Snytkine Oct 20 '11 at 13:26
the output I get when I run it with an ampersand is: [1] 24817 – Maurits Meester Oct 20 '11 at 13:38
...which means its this shell session's first job sent to the background and its pid is 24817. Sounds right, doesn't it? (Don't you get your prompt back? Have you checked ps aux | grep php for the script running in background?) – sr_ Oct 20 '11 at 13:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.