13

How can I spawn a process in a PHP page in order to start a program that survives the execution time of the request?

In other words, I want the page to have a normal lifetime (few milliseconds) but launch a program that keeps running on the server. Thanks

3
  • 1
    Here's a helpful tutorial on this topic that I've referenced many times. You may have to get a few pages in to get specifically to process forking, but the whole bit is worth absorbing if you want to get into process management with php. Commented Jan 21, 2012 at 21:15
  • @rdlowrey that link is no longer valid Commented May 31, 2018 at 4:59
  • 1
    @rineez try Wayback Machine Commented Nov 10, 2022 at 2:33

2 Answers 2

21

Use this code:

<?php exec('nohup /usr/bin/my-command > /dev/null 2>&1 &'); ?>

This forks the sub-process into the background and writes all of the output into /dev/null. That way PHP continues executing the script as if there won't be any output it has to wait for.

Sign up to request clarification or add additional context in comments.

2 Comments

what about windows? any equivalent?
I don't know about Windows. I am Linux User.
-1

I would suggest you to use CRON if you need to start a process on a time basis.

1 Comment

I don't the the original poster wants to know how to start a program at a certain time, which is what CRON does. Rather, the question is about having a web page written in PHP start a background process whenever someone goes to that page. That doesn't necessarily happen at a certain time.

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.