Reputation: 1934
I want to run multiple commands in a single cmd and i want in between command to have a delay.
I have the following code
import os
os.system('cmd /k "command1" & "command2" & "command3"')
How can i add some time.sleep between these commands?
Upvotes: 2
Views: 7115
Reputation: 3264
Of the Many ways of doing this, this one is perhaps the simplest:
import os
os.system('cmd /K "command1 & timeout 30 & command2 & timeout 30 & command3"')
This uses the timeout
function present in the CMD
environment, and allows the other commands to run correctly.
Note: the CMD
option /K
will leave garbage shell sessions on the system, instead you should use /C
as it will cause the CMD
instance to terminate when all of the commands are completed or an error is encountered.
ie:
import os
os.system('cmd /C "command1 & timeout 30 & command2 & timeout 30 & command3"')
of course you might want to control the time in seconds for each timeout:
eg:
import os
WaitSeconds1=30
WaitSeconds2=90
os.system('cmd /C "command1 & timeout '+WaitSeconds1+' & command2 & timeout '+WaitSeconds2+' & command3"')
All of that said, I want to be sure you understand that CMD will not execute these commands in parallel, so if the timeout is to avoid parallel processing, instead of to wait for additional things register etc.
I am aware that not every command waits to have it's work finished before exiting out (I'm looking at you, among others DiskPart
! [Although, TBQF, Diskpart has valid reasons for being set up the way it is, it just needs to be improved for better feedback in scripting.) ).
However if you know that isn't the case, you can just use:
import os
os.system('cmd /C "command1 & command2 & command3"')
And, as mentioned by @Arty you can always replace the single ampersand &
with a double ampersand &&
in any of these examples to only have it execute the next command when the previous command completes successfully, if that is a needed conditional. :)
Upvotes: 4
Reputation: 16737
If it doesn't matter for you to use precisely time.sleep()
, if it matters for you just to sleep somehow then you can use built-in command sleep <seconds>
(or timeout <seconds>
instead of sleep
), like in following code:
import os
sleep_seconds = 2
os.system(f'cmd /k "echo abc && sleep {sleep_seconds} && echo def && sleep {sleep_seconds} && echo ghi"')
Also probably you wanted /c
instead of /k
, because /k
executes commands string and leaves cmd console running, while /c
closes console.
Also you may use subprocess.run()
+ time.sleep()
, like following:
import subprocess, time
sleep_seconds = 2
subprocess.run(['echo', 'abc'], check = True)
time.sleep(sleep_seconds)
subprocess.run(['echo', 'def'], check = True)
time.sleep(sleep_seconds)
subprocess.run(['echo', 'ghi'], check = True)
Notice! In the first example above you may want to use &
instead of &&
if needed by your task. Also in the second example set check = False
if you want to model &
behaviour and check = True
if you want to model &&
behaviour.
Upvotes: 1
Reputation: 53
You could try using the time
module offered by Python.
Between the python commands to run the os commands, you can use:
import time
time.sleep(n)
where n is the number of seconds by which you want the delay to happen.
Upvotes: 0