1

I need to run a Python script in the background after being called from a PHP file. The PHP file should continue to run independently of the Python script (i.e. it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself).

The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04.

4 Answers 4

2

You could use exec() to kick off the Python interperator and have it send its output to either a file or to /dev/null with redirection. Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result.

http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail.

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

2 Comments

This seems to be the answer I've been looking for. Thanks very much! (Will accept as answer when the time limit runs out)
As I said on the other answer suggesting the use of the ampersand (&), be aware that this could create zombie processes. It would be safer to make the Python script daemonize, as I have suggested.
1

PHP Process Control can be used for this. The proc_open command can be used to start a process. You can later check up on it, read it's output etc. View the manual entry: http://www.php.net/manual/en/function.proc-open.php and search around google for PHP Process Control

Comments

0

I'm guessing the PHP file is called via Apache, in which case you won't be able to fork(). You should make your Python script daemonize. Check out python-daemon.

Comments

0

You could use:

<?php
shell_exec('./test.sh &');
?>

where ./test.sh should be the execution line to your script

1 Comment

Note that this can create zombie processes.

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.