我可以等待不是当前Shell终端的子进程的进程终止吗? [英] Can i wait for a process termination that is not a child of current shell terminal?

查看:313
本文介绍了我可以等待不是当前Shell终端的子进程的进程终止吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本必须杀死由高可用性中间件管理的资源一定次数.它基本上检查资源是否正在运行,然后将其杀死,我需要proc被真正终止的时间戳.所以我做了这段代码:

#!/bin/bash
echo "$(date +"%T,%N") :New measures Run" > /home/hassan/logs/measures.log
for i in {1..50}

do
    echo "Iteration: $i"
    PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
    if [ -n "$PID" ]; then
        echo "$(date +"%T,%N") :Killing $PID" >> /home/hassan/logs/measures.log
        ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print "kill -9 " $2'} | sh
        wait $PID
    else
        PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
        until [ -n "$PID" ]; do
                sleep 2
        PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
        done
    fi

done

但是使用我的等待命令,我会收到以下错误消息:wait:pid xxxx不是此shell的子项

解决方案

我假设您从,然后启动此脚本以等待.问题在于,子进程不是

会话中未列出任何作业.但是,使用.脚本可以在当前的脚本中只有一个挂起进程(即vim).因此,请使用.启动上面的脚本,以免子级将被创建.

请注意,定义任何变量或更改目录(以及更多)会影响您的环境!例如.通过调用的问题,PID将可见! >

评论:

  • 请勿使用...|grep ...|grep -v ... |awk ---弯管蛇!改用...|awk...
  • 在大多数Linux-es中,您可以使用类似ps -o pid= -C pcmAppBin的方法来获取pid,因此可以避免使用完整的管道.
  • 要从的问题中调用外部程序,您可以尝试system("mycmd");内置

我希望这会有所帮助!

I have a script that has to kill a certain number of times a resource managed by a high avialability middelware. It basically checks whether the resource is running and kills it afterwards, i need the timestamp of when the proc is really killed. So i have done this code:

#!/bin/bash
echo "$(date +"%T,%N") :New measures Run" > /home/hassan/logs/measures.log
for i in {1..50}

do
    echo "Iteration: $i"
    PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
    if [ -n "$PID" ]; then
        echo "$(date +"%T,%N") :Killing $PID" >> /home/hassan/logs/measures.log
        ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print "kill -9 " $2'} | sh
        wait $PID
    else
        PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
        until [ -n "$PID" ]; do
                sleep 2
        PID=`ps -ef | grep  "/home/hassan/Desktop/pcmAppBin pacemaker_app/MainController"|grep -v "grep" | awk {'print$2'}`
        done
    fi

done

But with my wait command i get the following error message: wait: pid xxxx is not a child of this shell

解决方案

I assume that You started the child processes from and then start this script to wait for. The problem is that the child processes are not the children of the running the script, but the children of its parent!

If You want to launch a script inside the the current You should start with ..

An example. You start a vim and then You make is stop pressing ^Z (later you can use fg to get back to vim). Then You can get the list of jobs by using the˙jobs command.

$ jobs
[1]+  Stopped                 vim myfile

Then You can create a script called test.sh containing just one command, called jobs. Add execute right (e.g. chmod 700 test.sh), then start it:

$ cat test.sh 
jobs
~/dev/fi [3:1]$ ./test.sh
~/dev/fi [3:1]$ . ./test.sh 
[1]+  Stopped                 vim myfile

As the first version creates a new session no jobs are listed. But using . the script runs in the present script having exactly one chold process (namely vim). So launch the script above using the . so no child will be created.

Be aware that defining any variables or changing directory (and a lot more) will affect to your environment! E.g. PID will be visible by the calling !

Comments:

  • Do not use ...|grep ...|grep -v ... |awk --- pipe snakes! Use ...|awk... instead!
  • In most Linux-es you can use something like this ps -o pid= -C pcmAppBin to get just the pid, so the complete pipe can be avoided.
  • To call an external program from you could try system("mycmd"); built-in

I hope this helps a bit!

这篇关于我可以等待不是当前Shell终端的子进程的进程终止吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆