【问题标题】:wait for two PID in c-shell在 c-shell 中等待两个 PID
【发布时间】:2013-08-07 13:07:14
【问题描述】:

以下作品适合我:

>sleep 20 &
[1] 30414
>sleep 30 &
[2] 30415
>wait $30414 $30415

这一切正常,直到我想将它写入 tmp.csh

在我的 tem.csh 文件中

sleep 20 &
set pid1=$!
sleep 30 &
set pid2=$!

说到“等待”

wait $pid1 $pid2 => too many arguments
wait $pid1 => too many arguments
wait \$$pid1 => too many arguments
wait $($pid1) => Illegal variable name

我该怎么写呢?

这个问题是为了解决 How can I wait until specified "xterm" finished?

【问题讨论】:

  • 避免使用 C-shell。阅读C shell considered harmful。改用zshbashfish shell。阅读Advanced Bash Scripting Guide
  • 请注意$30414 已扩展为空!尝试以echo 为前缀的wait 命令以确保......
  • echo $pid1 echo $pid2 给出正确的数字。我猜这是我的 $($pid1) 语法错误?
  • 真正切换到更现代的外壳。如您所见,没有人回答,因为我们都忘记了 C-shell(我在 1990 年代曾在 Sun4/110 工作站上使用过它)。
  • 不是每个人都有选择使用外壳的奢侈。我必须在工作中使用 csh(在我的脚本中也是如此),因为我们有一些工具不能很好地与 bash 配合使用。我认为假设 OP 使用 csh 是省事的,因为他们必须......像 Fortran 一样,你不会为了好玩而拿起它。

标签: linux shell pid csh


【解决方案1】:

“等待”命令不会等待特定的 PID。在 CSH 中尝试以下操作以等待特定的 PID:

#!/bin/csh -f

sleep 30 &
set pid1 = $!
sleep 40 &
set pid2 = $!

while ( `ps -p "$pid1,$pid2" | wc -l` > 1 )
  sleep 1
end

【讨论】:

    【解决方案2】:

    这在 tcsh 中对我有用:

    #!/bin/tcsh
    
    time
    sleep 10 &
    sleep 5 &
    wait
    time
    

    看起来 wait 不需要任何参数,只是等到每个 backgorunded 进程完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2012-09-16
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      相关资源
      最近更新 更多