Viewed   335 times

I have a simple PHP function that is supposed to execute a Pyton script when its called. I have tried this sort of function multiple times in my php programs, but somehow this time this function is not executing the python script at all. When I access the script from the command prompt and run python testing.py then it successfully gets executed. One thing that I want to mention that this script has some serious implementations of python's NLTK library, and takes more than 20 seconds to execute and perform its operations (i.e. data process and storing to db). Is this delay in the execution which is causing this problem or is there something else that I am missing this time?

function success(){
   $mystring = exec('python testing.py');
   $mystring;
   if(!$mystring){

        echo "python exec failed";
            }
   else{
   echo "<br />";
   echo "successfully executed!";
   }

 Answers

5

you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:

$mystring = exec('/usr/bin/python /home/user/testing.py');

and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.

Sunday, August 14, 2022
5

Using PDO prepared statements:

$placeholders = str_repeat('?, ', count($array)-1) . '?';
$stmt = $pdo->prepare("SELECT * FROM table WHERE field IN ($placeholders)");
$stmt->execute($array);

$placeholders will contain a sequence of ?, ?, ? placeholders, with the same number of ? as the size of the array. Then when you execute the statement, the array values are bound to the placeholders.

Saturday, August 27, 2022
 
xorlev
 
2

Typically this is a path problem. The application you're trying to call is probably not in the search path of the web server's user account. I only call applications from PHP using the full path. I store that path in settings files so it works across any system it's used on.

Tuesday, November 8, 2022
 
4

No. When a process is killed, the operating system releases all operating system resources (memory, sockets, file handles, …) previously acquired by that process.

Sunday, September 18, 2022
 
vsb
 
vsb
5

To run python script via /etc/rc.local:

1) Edit the file using sudo /etc/rc.local;

2) Add the following to the file right before exit 0:

(sleep 10;python /home/pi/Cigar-Box/CigarBox.py)&

The parentheses allows your to run multiple commands in the background. The sleep 10 will delay the running of script by 10 seconds, as some of the services that your script depend on may not be available yet at the time of booting rc.local.

Alternatively you can use crontab @reboot to automate the execution of your script.

Using crontab:

1) run command line sudo crontab -e;

2) add the command to the end of the file:

@reboot /usr/bin/python /home/pi/Cigar-Box/CigarBox.py
Saturday, November 5, 2022
 
eeshwar
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :