Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command with delay using subprocess

Tags:

python

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.

like image 458
fredrik Avatar asked May 05 '14 15:05

fredrik


People also ask

Does subprocess call wait?

The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code. It accepts one or more arguments as well as the following keyword arguments (with their defaults): stdin=None, stdout=None, stderr=None, shell=False.

How do you run a command in subprocess?

subprocess. Subprocess allows you to call external commands and connect them to their input/output/error pipes (stdin, stdout, and stderr). Subprocess is the default choice for running commands, but sometimes other modules are better.

How do you pass command line arguments in subprocess?

You can pass a string to the command to be executed by subprocess. run method by using “input='string'” argument. As you can see, the code above passes “data. txt” as a string and not as a file object.

How to execute system commands using Python subprocess?

We will use Python subprocess module to execute system commands. We can run shell commands by using subprocess.call () function. See the following code which is equivalent to the previous code.

How do I stop a subprocess if it is taking too long?

subprocess.run includes the timeout argument to allow you to stop an external program if it is taking too long to execute: If we run this code, we’ll receive output like the following: The subprocess we tried to run used the time.sleep function to sleep for 2 seconds.

What are some like commands for subprocess in Linux?

like: os.system, os.spawn*, os.popen*, popen2.* commands. Let’s start looking into the different functions of subprocess. Run the command described by “args”. Note, the default value of the shell argument is False.

How to kill a Python subprocess after a specific timeout?

Python will make a best effort to kill the subprocess after the timeout number of seconds, but it won’t necessarily be exact. Sometimes programs expect input to be passed to them via stdin. The input keyword argument to subprocess.run allows you to pass data to the stdin of the subprocess. For example:


1 Answers

this is because subprocess can work in two modes: either you fork() the process specified by the tuple passed as argument, or you execute the string with a shell. The difference is the shell argument. So what you might want to do is:

command = "sleep 10 && sudo /etc/init.d/tractor-blade restart"
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT)

or:

time.sleep(10)
command = ['sudo', '/etc/init.d/tractor-blade', 'restart' '&']
subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

from the documentation:

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

like image 121
zmo Avatar answered Oct 12 '22 09:10

zmo