This is probably relatively easy for anyone who is more into linux than me.

I have the following command, to run a PHP process on background:

/usr/local/bin/php -r '$_COOKIE["PHPSESSID"]="cookie";$srv="server.name";$dir="/home/base/dir/";include("/home/x/public_html/newsletter/send.php");' > /home/x/public_html/newsletter_log.txt 2>&1 & echo $!

The paths have been replaced by fake ones.

The idea of this command is to run a process in background, retrieve the PID to the exec() function and allow the user to navigate to other pages.

Everything was working fine until today, where the user has to wait for the "background" process to finish.

This is quite awful, since the user has to wait a looooooooooooooong time for the process: if it takes 5 minutes, the person must watch on a boring page, without being able to do anything, until the process finishes.

I know that using & at the end of the script is the key, but as you can see, it is there!

Sorry for this idiotic noob question.


The command is fine! There's nothing wrong with it.

This was a gotcha that only clicked on me today.

The problem was that I had a session that I was opening.

I had this line, in PHP:


But I forgot to close it. My solution was similar to this:

$tmp = $_SESSION;
$_SESSION = $tmp;

This way, it will make a copy of the session, write the new data and close it.

Since the session wasn't closed, the new requests to open it would hang, since the file was locked. Closing it solved the issue.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.