java - How does the program execute the top or ping command in the shell?
某草草
某草草 2017-05-17 10:03:20
0
3
835

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?

某草草
某草草

reply all(3)
滿天的星座

topping这类的命令都有终止的选项可以用,如ping-c指定发送包的次数,top-bnSpecify the output mode and output times.

漂亮男人

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

黄舟

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template