Sometimes a program is used to execute shell commands. But what if I execute a command like top or ping? This kind of command will not be terminated directly, but will always be executed. How do I get its execution status and how do I terminate it?
滿天的星座2017-05-17 10:05:20
top
和ping
这类的命令都有终止的选项可以用,如ping
的-c
指定发送包的次数,top
的-bn
Specify the output mode and output times.
漂亮男人2017-05-17 10:05:20
Generally, this kind of continuous output command already has a parameter control to control the number of prints. For example, the top
就是 -n num
来指定打印num次, 而ping
则是通过-c num
来指定打印num次; 可以通过对应的选项, 来设置命令的执行次数, 如果命令不支持这样的选项控制, 那应该只能通过获取该进程的pid
, 然后通过kill
operation sends a termination signal to terminate the operation
黄舟2017-05-17 10:05:20
First of all, both the top and ping commands can be terminated: press q to exit the top command, and press Ctrl-C to exit ping
You can use subprocess to start the shell program, and then write strings to STDIN through PIPE, and then you can control these shell programs.