1

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.

kinske
  • 597
  • 8
  • 24
  • Possible duplicate of [php execute a background process](http://stackoverflow.com/questions/45953/php-execute-a-background-process) – magnetik Aug 03 '16 at 14:28
  • Your set-up is very complex, and seems to be very prone to security problems. The solution to `sudo` requirements is to configure `sudo` to allow as *little* as possible, not to wrap each script in yet another wrapper which also requires `sudo`. Concretely, allowing a daemon to run `send` via `sudo` without requiring a password prompt would remove the requirement to run `sudo` anywhere else in this chain, if I am reading your code correctly. – tripleee Aug 03 '16 at 15:20
  • But wrapping the code in three separate scripts in different languages makes it hard to see what exactly you need help with, and also requires us to have at least a passing understanding of all three languages. – tripleee Aug 03 '16 at 15:21
  • In the [help] you might particularly want to read the [mcve] guideline. – tripleee Aug 03 '16 at 15:21
  • As an aside, the `kill -9` has every antipattern which riddles the [poor reinventions of `pkill`](http://stackoverflow.com/questions/22586681/cant-kill-pid-from-script-loop/22586737) which 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. – tripleee Aug 03 '16 at 15:22

0 Answers0