python执行cmd命令,怎么让他执行Ctrl+C的效果结束命令?
我在用ping监控一个服务器的网络状态,我执行了ping命令后,他会一直这么ping下去,不停止,怎么让他在10秒后执行ctrl+c的效果
def re(cmd):
while True:
os.system(cmd);
re("ping 192.168.1.1 -t")
他会这样一直ping下去,想了半天也想不出怎么让他10秒后执行ctrl+c结束的执行效果,请教大神,怎么让他执行结束命令;
10秒后停止命令,类似执行ctrl+c的效果;
Just use kill
os.system has very weak control, you need the .kill method of the subprocess module to send signals.
But if it’s just ping, why don’t you just let it ping 10 times? What do you want to do?
I don’t know if I can meet your needs
The main problem is that the executed command never returns, os.system cannot determine whether the command has been executed and executed successfully
If you can try a simple cmd command
os.system('start ping www.baidu.com -t')
Register
signal.SIGINT
SignalI tried it, and I can probably write it like this: