1

I have a python script, which should execute a shell script. But it should sleep 30 seconds before executing. I want the script to sleep in the background and then execute it.

The Shell Command should be like this: If I type this command directly to the console, it works.

(sleep 30 && /root/bin/myscript.sh parameter1 parameter2) > /dev/null 2>&1

Now from python: (I have divided the commands)

subprocess.call(['/bin/sleep', '30', '&'])
subprocess.call(['/root/bin/myscript.sh', str(dom), str(limit), '&'])

(str(dom) and str(limit) are the 2 parameters)

I get this error:

/bin/sleep: invalid time interval `&'

Why does it take & as parameter instead of 30?

3
  • Why don't you just use time.sleep(secs)? docs.python.org/2/library/time.html#time.sleep Commented Nov 19, 2013 at 13:02
  • I want to go back to the console and dont wait 20 second until the script is executed. Commented Nov 19, 2013 at 13:05
  • Calling sleep in the background will simply cause myscript.sh to run in parallel with, not after, the sleep. Multiple calls to subprocess.call do not queue the processes they start. Commented Nov 19, 2013 at 13:26

4 Answers 4

3

The error is because & is being passed to the /bin/sleep command as an argument, whereas you are using it in the context of telling the shell to execute the command in the background.

If you want to use '&' or '&&' you can use os.system:

os.system('sleep 1 && echo foo && sleep 5 && echo bar')

or to run nonblocking (in the background) you can use the standard &:

os.system('sleep 1 && echo foo && sleep 5 && echo bar &')

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that works :) Now, I only need to implement the parameters str(dom) and str(limit). Like os.system('sleep 20 && /root/bin/myscript.sh str(dom) str(limit) &') Of course not as string :D
Be careful if you are using untrusted, user-provided strings (e.g. from a webserver), as otherwise you may vulnerable to shell injection.
It's an internal server. And the python script is from xen ;) I tried it with os.system('/bin/sleep 20 && /root/bin/myscript.sh %s %s &' % (str(dom), str(limit))) Error: sh: Syntax error: "&" unexpected
That's odd, &ing a os.system command works for me. What environment is this?
Debian 6.0.4 with Xen. Python 2.7.2+ is running. But with your sample command, it works. :S
|
1

You can't do what you are wanting to do, force a python script to run in the background every time. Python also can't run a task directly in the background. You might look into Threading, which might accomplish what you want, but without knowing why you want to go back to the command prompt, it's difficult. I think what you might want is a threading timer specifically, an example of which can be found here.

from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

1 Comment

I dont want to run the python script in the background. Only the 2 shell commands :)
1

How about

subprocess.Popen(["sh","-c","sleep 30; /root/bin/myscript.sh parameter1 parameter2"])

You can build the command line for parameter1, parameter2 using string concatenation.

But as described in PearsonArtPhoto's answer, using thread is better option.

Comments

1

Call with one string:

prc = subprocess.Popen('/bin/sleep 3; echo Some string', shell=True)

Note, I remove &.

2 Comments

subprocess.Popen run in backgroung and not block main Pythjon process. See update.
Yes, you fixed it :-)

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.