1

I'm working with a Python script and I have some problems on delaying the execution of a Bash script.

My script.py lets the user choose a script.sh, and after the possibility to modify that, the user can run it with various options.

One of this option is the possibility to delay of N seconds the execution of the script, I used time.sleep(N) but the script.py totally stops for N seconds, I just want to retard the script.sh of N seconds, letting the user continue using the script.py.

I searched for answers without success, any ideas?

6
  • 1
    I think you want to fork a different process, sleep in that process and then exec the Bash script Commented Mar 28, 2017 at 19:28
  • If you use the subprocess module and create your own Popen object, your bash script won't block and you can put the sleep command at the beginning of it. Commented Mar 28, 2017 at 19:32
  • Celery is a framework you can use for scheduling tasks (arbitrary function calls) in Python. Commented Mar 28, 2017 at 19:34
  • 1
    Sleeping for a fixed amount of time seems to be the wrong approach here. It would be much more usable, if your program just opened the script in the user's favorite text editor so they can make any modifications to it needed, then save and close the editor and have the modified script be executed. Commented Mar 28, 2017 at 19:36
  • 1
    @5gon12eder, ...indeed, that's the normal way to do it -- opening whichever editor is named in the environment variable $EDITOR (typically with a default of vi or otherwise something guaranteed to be available), and waiting for that editor to exit before proceeding. Witness git and svn prompting for commit messages that way, for example. Commented Mar 28, 2017 at 19:51

3 Answers 3

3

You can start the script in a New thread, sleeping before running it.

Minimal example:

import subprocess as sp
from threading import Thread
import time

def start_delayed(args, delay):
    time.sleep(delay)
    sp.run(args)

t = Thread(target=start_delayed, kwargs={'args': ['ls'], 'delay': 5})
t.start()
Sign up to request clarification or add additional context in comments.

3 Comments

Does this differ from what threading.Timer already provides in the standard library?
(btw, you've got so in one place, and sp in another).
@Charles Duffy I just die not know about Timer, that should be the excepted answer. Also thanks for spotting the typo
1

Consider using a Timer object from the threading module:

import subprocess, threading
t = threading.Timer(10.0, subprocess.call, args=(['script.sh'],))
t.start()

...the above running script.sh after a 10-second delay.


Alternately, if you want to efficiently be able to run an arbitrary number of scheduled tasks with only a single thread controlling them, consider using a scheduler from the tandard-library sched module:

import sched, subprocess

s = sched.scheduler(time.time, time.sleep)
s.enter(10, subprocess.call, (['script.sh'],))
s.run()

This will run script.sh after 10 seconds have passed -- though if you want it to run in the background, you'll want to put it in a thread (or such) yourself.

Comments

0

You should run sleep using subprocess.Popen before calling script.sh.

2 Comments

Why would you want to do that? It's much higher-overhead than using time.sleep().
...as I understand it the OP's argument against time.sleep() is that it ties up their Python script's execution flow, but blocking on a subprocess to exit will do the same thing.

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.