简体   繁体   English

Csh脚本等待多个pid

[英]Csh script wait for multiple pid

Does the wait command work in a csh script to wait for more than 1 PID to finish? 在csh脚本中,wait命令是否可以等待超过1个PID完成?

Where the wait command waits for all the PID listed to complete before moving on to the next line wait命令等待所有列出的PID完成之前,移至下一行

eg 例如

wait $job1_pid $job2_pid $job3_pid
nextline

as the documentation online that I usually see only shows the wait command with only 1 PID, although I have read of using wait for multiple PID, like here : http://www2.phys.canterbury.ac.nz/dept/docs/manuals/unix/DEC_4.0e_Docs/HTML/MAN/MAN1/0522____.HTM 正如我通常在网上看到的文档仅显示了只有1个PID的wait命令,尽管我已经阅读过使用wait for Multiple PID,例如: http : //www2.phys.canterbury.ac.nz/dept/docs/手册/ UNIX / DEC_4.0e_Docs / HTML / MAN /男1/0522 ____。HTM

which says quote "If one or more pid operands are specified that represent known process IDs,the wait utility waits until all of them have terminated" 它说:“如果指定一个或多个表示已知进程ID的pid操作数 ,则wait实用程序将等到所有它们都终止为止”

No, the builtin wait command in csh can only wait for all jobs to finish. 不, csh的内置wait命令只能等待所有作业完成。 The command in the documentation that you're referencing is a separate executable that is probably located at /usr/bin/wait or similar. 您所引用的文档中的命令是一个单独的可执行文件,可能位于/usr/bin/wait或类似位置。 This executable cannot be used for what you want to use it for. 该可执行文件不能用于您要使用的用途。

I recommend using bash and its more powerful wait builtin, which does allow you to wait for specific jobs or process ids. 我建议使用bash及其更强大的wait内置,它确实允许您等待特定的作业或进程ID。


From the tcsh man page, wait waits for all background jobs. tcsh手册页上, wait等待所有后台作业。 tcsh is compatible with csh , which is what the university's documentation you linked is referring to. tcshcsh兼容,这是您链接的大学文档所参考的。

wait The shell waits for all background jobs. wait Shell等待所有后台作业。 If the shell is interactive, an interrupt will disrupt the wait and cause the shell to print the names and job numbers of all outstanding jobs. 如果外壳是交互式的,则中断将中断等待,并使外壳打印所有未完成作业的名称和作业编号。

You can find this exact text on the csh documentation here . 您可以在csh文档中找到此确切文本。

The wait executable described in the documentation is actually a separate command that waits for a list of process ids. 该文档中描述的wait可执行文件实际上是一个等待进程ID列表的单独命令。

However, the wait executable is not actually capable of waiting for the child processes of the running shell script and has no chance of doing the right thing in a shell script. 但是, wait可执行文件实际上不能够等待正在运行的shell脚本的子进程,并且没有机会在shell脚本中做正确的事情。

For instance, on OS X, /usr/bin/wait is this shell script. 例如,在OS X上, /usr/bin/wait是此shell脚本。

#!/bin/sh
# $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $
# This file is in the public domain.
builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}

Anyway, I can't get the /usr/bin/wait executable to work reliably in a Csh script ... because the the background jobs are not child processes of the /usr/bin/wait process itself. 无论如何,我无法使/usr/bin/wait可执行文件在Csh脚本中可靠地工作……因为后台作业不是/usr/bin/wait进程本身的子进程。

#!/bin/csh -f

setenv PIDDIR "`mktemp -d`"

sleep 4 &

ps ax | grep 'slee[p]' | awk '{ print $1 }' > $PIDDIR/job

/usr/bin/wait `cat $PIDDIR/job`

I would highly recommend writing this script in bash or similar where the builtin wait does allow you to wait for pids and capturing pids from background jobs is easier. 我强烈建议您使用bash或类似的脚本编写此脚本,其中内置的等待确实允许您等待pid,并且从后台作业捕获pid更容易。

#!/bin/bash

sleep 4 &
pid_sleep_4="$!"

sleep 7 &
pid_sleep_7="$!"

wait "$pid_sleep_4"
echo "waited for sleep 4"

wait "$pid_sleep_7"
echo "waited for sleep 7"

If you don't want to rewrite the entire csh script you're working on, you can call out to bash from inside a csh script like so. 如果您不想重写正在使用的整个csh脚本,则可以像这样在csh脚本中调用bash。

#!/bin/csh -f

bash <<'EOF'

sleep 4 &
pid_sleep_4="$!"

sleep 7 &
pid_sleep_7="$!"

wait "$pid_sleep_4"
echo "waited for sleep 4"

wait "$pid_sleep_7"
echo "waited for sleep 7"

'EOF'

Note that you must end that heredoc with 'EOF' including the single quotes. 请注意,您必须在此文档的末尾加上'EOF'包括单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM