【问题标题】:Run two daemon jobs in two different directories在两个不同的目录中运行两个守护程序作业
【发布时间】:2014-11-09 01:48:29
【问题描述】:

我想编写一个 bash 脚本,它会在两个不同的目录中启动两个守护程序作业。这个脚本应该在前台运行,然后当我按下 ctrl-c 时,脚本会退出并关闭两个守护进程。

假设这两个守护进程是两个可执行文件daemon1和daemon2,它们分别需要启动的两个目录是dir1和dir2。

我将如何编写这样的脚本?

【问题讨论】:

    标签: linux bash shell scripting


    【解决方案1】:

    您可以使用“&”运算符在后台运行子进程,在父进程中检索它们的 PID(进程 ID),并在它们两者上“等待”。在 Ctrl-C bash 将中断“等待”并退出两个子进程。

    #!/bin/bash
    
    cd ~/Documents
    ping localhost &
    pid1=$!
    
    cd ~/Downloads
    ping localhost &
    pid2=$!
    
    wait $pid1 $pid2
    

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2012-06-25
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2018-08-31
      • 2012-02-01
      • 2015-11-20
      • 1970-01-01
      相关资源
      最近更新 更多