I believe os.system()
will suit your needs as:-
- It shows real time output, as if you were running the command on
a terminal
- When a command is successfully executed, returns a
code (ex. returns 0 if command is successful)
- It could be interrupted, via
Control + C
(Keyboard Interrupt).
You given compound command could be stripped, to create each individual command, like:-
echo 'Process started'; ping 127.0.0.1 -i 2 ; echo 'Process finished'
Becomes:-
echo 'Process started'
ping 127.0.0.1 -i 2
echo 'Process finished'
no you can store each command in a separate variable/list. And then pass it to os.system()
.
Example:-
import os
os.system("echo 'Process started'")
os.system("ping 127.0.0.1 -i 2")
os.system("echo 'Process Finished'")
SAMPLE OUTPUT:-
'Process started'
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
'Process Finished'
P.S.:- This is just a layman example of os.system()
for your case. You can further extend it to failsafe versions of it.