PHP: Run Python command line for background process

Posted: May 20, 2016 in Php, Python
Tags: , ,

Hi,

Here is my step to perform background process with Python using PHP command:

Server: Linux Ubuntu 14.04,
PHP 5.5
Python 2.7.6

# Make sure your python script is executeable
chmod +x /path/to/python-script.py

function run_command(array $input)
{
 $arg1 = $input['arg1'];
 $arg2 = $input['arg2'];
 
 $command = "python myscript.py $arg1 $arg2";
 
 #change to my python project directory
 #enable debugging with 2>&1
 #throw output in $output

 exec("cd /home/python_project/app && $command 2>&1", $output);
 var_dump($output);
}

Ok Good Luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s