beemurt
Posts: 16
Joined: Fri May 24, 2019 4:27 pm

Execute pkill command for python script using PHP[SOLVED]

Sun Jun 30, 2019 3:26 pm

Good Afternoon All

I have been trying to have PHP execute a pkill command in order to kill a python script which needs to run in a loop.

Basically my web page updates a database, and executes a python script. The python script runs based on the updated info in the database. However this is an endless loop and i need to kill it once the off button is clicked.

Code: Select all

<?php
else if(isset($_POST['off'])){
	
			$killcommand = escapeshellcmd("/usr/bin/sudo pkill -f alarmTest.py");
			$killpy = exec($killcommand);
			echo $killpy;
	
			$result = $conn->query("UPDATE database_Table_Name SET status = '0' WHERE id = 1;");
			if (!$result){
			echo $conn->error;
			} 
				 
print "some text/html here";
}
?>
I get the below errors from my apache error log (sudo cat /var/log/apache2/error.log)

/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file
/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file
/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file
/usr/bin/sudo: /usr/bin/sudo: cannot execute binary file
sh: 1: /usr/bin/: Permission denied
sh: 1: /usr/bin/: Permission denied
sh: 1: /usr/bin/: Permission denied
sh: 1: /usr/bin/: Permission denied

Is my php code incorrect or is this a permissions issue? If it is permissions can i allow this one command to be ran as root?

Any help at all is much appreciated!
Last edited by beemurt on Tue Jul 02, 2019 8:40 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 39130
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Execute pkill command for python script using PHP

Sun Jun 30, 2019 5:10 pm

Apache2 runs with userid=www-data group=www-data
It's a massive security hole to allow www-data to run sudo.

If you insist on carving holes in your system security you'll need to add www-data to the sudoers file with sudo visudo.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

beemurt
Posts: 16
Joined: Fri May 24, 2019 4:27 pm

Re: Execute pkill command for python script using PHP

Sun Jun 30, 2019 6:32 pm

Hi DougieLawson,

Thanks for the reply.

Security is out of scope for what I am doing so I am not too concerned.

www-data is however already in the sudoers file, as below.

www-data ALL=(ALL) NOPASSWD: /usr/bin/python
www-data ALL=(ALL) NOPASSWD: ALL

Yet it appears I still cant run the kill command.

beemurt
Posts: 16
Joined: Fri May 24, 2019 4:27 pm

Re: Execute pkill command for python script using PHP[SOLVED]

Tue Jul 02, 2019 8:53 am

I have updated this post encase anyone else is struggling with a similar problem. After days of banging my head of a brick wall I finally got it working.

It appears I was trying to fix the wrong problem! The issue wasn't that the kill command wasn't working; it was that my python code was hanging up the system and preventing any other interactions. The answer was to make the python script run in the background.

This was the incorrect code I was trying to run when initially calling the python script:

Code: Select all

<?php
		$command = escapeshellcmd("/usr/bin/python alarmTest.py");
		$output1 = shell_exec($command);
		echo $output1;
?>
A simple solution (below) and my code worked perfectly, exiting using a simple if block within the python which evoked sys.exit(). This meant no need for the pkill command.

Code: Select all

<?php
		$command = system("python alarmTest.py > /dev/null 2>&1 &");
		$output1 = shell_exec($command);
		echo $output1;
?>
Hope this is useful
Thanks
B

Return to “Other programming languages”