Take the 2-minute tour ×

I have this in one PHP file:

echo shell_exec('nohup /usr/bin/php -f '.CRON_DIRECTORY.'testjob.php > /dev/null 2>&1 &');

and in testjob.php I have:

file_put_contents('test.txt',time()); exit;

And it all runs just dandy. However if I go to processes it's not terminating testjob.php after it runs.

share|improve this question
Does it do that when you run it from command line without nohup? –  Amir Raminfar Jul 24 '11 at 20:46
try adding at the top: ini_set("log_errors","1"); ini_set("error_log","Errors..txt"); --and -f is optional –  Dagon Jul 24 '11 at 20:52
How come that you expect the script to terminate that fast? Maybe it's by design of that script that it runs a bit longer from time to time? nohup will take care it won't stop even the shell from shell_exec already got closed. Looks like that you actually intend here for not terminating it as long as it runs. –  hakre Jul 24 '11 at 22:06
add comment

3 Answers

up vote 0 down vote accepted

Remove & from the end of your command. This symbol says nohup to continue running in background, thus shell_exec is waiting for task to complete... and waiting... and waiting... till the end of times ;)

I don't even understan why would you perform this command with nohup.

echo shell_exec('/usr/bin/php -f '.CRON_DIRECTORY.'testjob.php > /dev/null 2>&1');

should be enough.

share|improve this answer
I want it to run in the background but I want it to end after it's done running –  Macmee Jul 25 '11 at 14:21
I had my test script to hand still, so I just tried removing the nohup and the final &. Now test.php hangs around until testjob.php has completed. Putting just the nohup back in did not help. Putting in just the & and it works (testjob.php is independent of test.php). CONCLUSION: the final ampersand is needed; nohup may be optional, but does no harm. –  Darren Cook Jul 26 '11 at 0:12
Okay. Then do the following: perform your shell_exec and exit; or die(); right after it. Script should still run. You can check whether it is running or not by command ps auwx | grep php. Or you can check whether it has affected your data as you planned. –  Nemoden Jul 26 '11 at 0:50
add comment

(Having to post this as an answer instead of comment as stackoverflow still won't let me post comments...)

Works for me. I made testjob.php exactly as described, and another file test.php with just the given line (except I removed CRON_DIRECTORY, because testjob.php was in the same directory for me).

To be sure I was measuring correctly, I added "sleep(5)" at the top of testjob.php, and in another window I have:

watch 'ps a |grep php'

running. This happens:

  1. I run test.php
  2. test.php exits immediately but testjob.php appears in my list
  3. After 5 seconds it disappears.

I wondered if shell might matter, so I switched from bash to sh. Same result.

I also wondered if it might be because your outer script is long-running. So I put "sleep(10)" at the bottom of test.php. Same result (i.e. testjob.php finishes after 5 seconds, test.php finishes 5 seconds after that).

So, unhelpfully, your problem is somewhere other than the code you've posted.

share|improve this answer
I did the same yesterday and it worked for me as well. Think it's worth an answer though. I assume there is a glitch in the script OP calls which makes it never ending from time to time. –  hakre Jul 25 '11 at 9:34
add comment

You're executing PHP and make that execution a background task. That means it will run in background until it is finished. shell_exec will not kill that process or something similar.

You might want to set an execution limit, PHP cli has a setting of unlimited by default. See as well set_time_limit PHP Manual;

So if you wonder why the php process does not terminate, you need to debug the script. If that's too complicated and you're unable to find out why the script runs that long, you might just want to terminate the process after some time, e.g. 1 minute.

share|improve this answer
add comment

Your Answer


