Home  >  Q&A  >  body text

java - How does the program execute the top or ping command in the shell?

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?

某草草某草草2713 days ago808

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-17 10:05:20

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

    reply
    0
  • 漂亮男人

    漂亮男人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

    reply
    0
  • 黄舟

    黄舟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.

    reply
    0
  • Cancelreply