Search code examples
pythonsubprocesscallos.system

Call subprocess in python service


I have a strange problem.. I've created a python script (modbus_sensor.py), which runs as a service. Modbus_sensor.py calls around 15 other python scripts by a scheduler.

When I run modbus_sensor.py like: "python modbus_sensor.py" it works fantastic and all the sub scripts are running.
But when I start the script like a service: "service modbus_daemon start", the main script starts, but it doesn't call the other scripts.

def ExecuteReadChange(sScriptName = '', sArg1 = '', sArg2 = ''):
# os.system ('sudo python ' + sScriptName + ' ' + sArg1) #Call every script

# os.system ('sudo /usr/bin/python /usr/local/bin/modbus_sensor/test.py') # returns 256
# subprocess.call('sudo python test.py', shell=True)
# subprocess.call('sudo /usr/bin/python /usr/local/bin/modbus_sensor/test.py', shell=True) # returns 1

I have tried everything like, with/without complete path.

What can be the problem?


Solution

  • You might have to disable requiretty in your /etc/sudoers settings:

    Use the visudo command to edit /etc/sudoers

    Replace:

    Defaults    requiretty
    

    With:

    Defaults    !requiretty