I'm trying to execute this command from a python script using subprocess: sleep 10 && sudo /etc/init.d/tractor-blade restart &
I want the python script to finish (return code 0). Then, 10 seconds later I wish the command to get executed.
This is what I have:
import sys, subprocess
command = ['sleep', '10', '&&', 'sudo', '/etc/init.d/tractor-blade', 'restart' '&']
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Catch stdout
sys.stdout.flush()
for line in iter(p.stdout.readline, b''):
print(">>> " + line.rstrip())
But this is what happens:
>>> sleep: invalid time interval `&&'
>>> sleep: invalid time interval `sudo'
>>> sleep: invalid time interval `/etc/init.d/tractor-blade'
>>> sleep: invalid time interval `restart'
>>> sleep: invalid time interval `&'
>>> Try `sleep --help' for more information.
I am guessing my formatting is wrong?
I need to make the python script complete before the command is being executed, which is why I am trying to add a delay to the command. My sudoers allows for this `tractor-blade' to get executed with NOPASSWD, thus does not require a password.