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.

5
  • Possible duplicate of php execute a background process Commented Aug 3, 2016 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. Commented Aug 3, 2016 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. Commented Aug 3, 2016 at 15:21
  • In the help center you might particularly want to read the minimal reproducible example guideline. Commented Aug 3, 2016 at 15:21
  • As an aside, the kill -9 has every antipattern which riddles the poor reinventions of pkill 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. Commented Aug 3, 2016 at 15:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.