如果进程正在运行,请执行此操作

时间:2011-11-19 21:19:16

标签: java linux process

我有一个只有1个按钮的小应用程序。我想要它做的是检查按下按钮时进程是否正在运行。它是,做x,如果它不是y。

到目前为止,这是我的简单代码。我知道它只包括按钮和点击而没有别的。我真的很茫然。

public class toggle {
   public static void main(String[] args) {
      Display myDisplay = new Display();
      Shell myShell = new Shell(myDisplay);
      myShell.setText("uTorrent Toggle");
      myShell.setBounds(120, 120, 220, 120);
      myShell.setLayout(new FillLayout());
      final Button Start = new Button(myShell, SWT.PUSH);
      Start.setText("Start uTorrent");
      Start.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
             Start.setText("Kill uTorrent");
             try {
                Runtime.getRuntime().exec("/bin/bash -c \"utorrent start\"");
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
      });
      myShell.open();
      while (!myShell.isDisposed()) {
         if (!myDisplay.readAndDispatch()) myDisplay.sleep();
      }
      myDisplay.dispose();
 }
}

1 个答案:

答案 0 :(得分:0)

要检查进程是否正在运行,您可以使用“ps -efw”的输出或根据您的需要使用不同的参数。