I have a constellation with 3 involved scripts, written in php, shell and python. All are running on a raspberry pi B+ with the latest raspbian.
The python script switches a wireless socket with this lib: http://github.com/xkonni/raspberry-remote.git and does the following:
#!/usr/bin/env python
import time
import subprocess
import RPi.GPIO as GPIO
import sys
channelCode = sys.argv[1]
unitCode = sys.argv[2]
duration = sys.argv[3]
# Use the pins as they are on the board.
GPIO.setmode(GPIO.BOARD)
# Pin 18
GPIO.setup(12, GPIO.IN)
while 1:
if GPIO.input(12) == GPIO.LOW:
subprocess.call('sudo send ' + channelCode + ' ' + unitCode + ' 1', shell=True)
time.sleep(float(duration))
subprocess.call('sudo send ' + channelCode + ' ' + unitCode + ' 0', shell=True)
# 1 sec sleep to get just one low, instead of multiple because of high sensitivity.
time.sleep(1)
# Don't slow down system because of endless loop.
time.sleep(0.01)
The endless loop is for recognizing a light-barrier interrupt and shall run still the user interrupts it via a webinterface.
Because of the needed sudo privileges for the lib, I created a bash script, that could be executed with sudo by www-data (only this script - security).
The shell script starts the python script with the endless loop or kills it:
#!/bin/bash
# If true, then python script shall be activated, if it is false it should be killed.
if [ $1 = "true" ]
then
python /var/www/html/cat-feeder/app/Resources/pi/light-barrier.py $2 $3 $4 &
fi
if [ $1 = "false" ]
then
kill -9 `ps -ef | grep light-barrier.py | grep -v grep | awk '{print $2}'`
fi
exit 0
In php the shell script is executed with arguments for killing the python script and end the loop or start the script:
if ($lightBarrierActive === true) {
exec('sudo /var/www/html/cat-feeder/app/Resources/pi/catfeeder-sudo-script.sh false &');
}
else {
exec('sudo /var/www/html/cat-feeder/app/Resources/pi/catfeeder-sudo-script.sh true 11010 4 1 &');
}
Now the problem: the php exec command with the kill does work just fine. But the exec command with the arguments for the shell script for activating the python script causes a hanging of the php script.
The weird thing: executing the shell script from command line with the exact same command is working!
I tried executing with the nohup command, but same thing.
I also tried this: PHP hanging while exec() bash script which I think doesn't trigger the shell script, because it doesn't produce any output.
sudorequirements is to configuresudoto allow as little as possible, not to wrap each script in yet another wrapper which also requiressudo. Concretely, allowing a daemon to runsendviasudowithout requiring a password prompt would remove the requirement to runsudoanywhere else in this chain, if I am reading your code correctly.kill -9has every antipattern which riddles the poor reinventions ofpkillwhich we see here multiple times every week. Find and read one of those, so we don't have to continue flogging the moist spot where a live horse used to be standing.