18

I have searched a lot to find the exact answer but didn't find any.

many people mentioned that we should & at end of command to don't wait for response.
for example to run bg.php in background , this was recommended:

exec("/usr/bin/php bg.php &");  

but it doesn't work for me. and the main script waits for complete execution of the bg.php.

I also read somewhere to write bg.php output in a logfile but my background script doesn't produce any output. It does some process and then write something in database.

I just want my script to run bg.php and don't wait for it to end.

please help me including correct code.

2 Answers 2

23

You have to reroute programs output somewhere too, usually /dev/null

exec($cmd . " > /dev/null &");
Sign up to request clarification or add additional context in comments.

2 Comments

works fine! but what is /dev/null directory? does this command write the output to a file?
/dev/null just discards the data sent to it, you can alternatively route it to some file if you need the program output
1

Have you considered using screen? You can start up a screen session that runs in a detached process.

screen -d -m -S my_bg_session /usr/bin/php bg.php

Then you can connect to it later if it is still running:

screen -r my_bg_session

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.