简体   繁体   English

如何使用shell脚本运行依赖于正在运行的进程的第二个进程?

[英]How to run a second process with dependency to a running process using shell script?

I want to do an automation for 2 processes using shell script.我想使用 shell 脚本对 2 个进程进行自动化。 I have 2 programs, a is a workload, b is a CPU profiler to profile the cpu when a is running.我有 2 个程序, a是工作负载, b是 CPU 分析器,用于在a运行时分析 CPU。

Previously, I run these programs manually, by opening 2 terminals.以前,我通过打开 2 个终端手动运行这些程序。 First, run a in the first terminal, then in another terminal, I get the process ID of a , and finally run ./b [pid-of-a] .首先,在第一终端运行,然后在另一个终端,我得到进程ID,最后运行./b [pid-of-a] This has caused me to miss the profiling of the first few seconds of process a .这导致我错过了进程a的前几秒钟的分析。

I tried:我试过:

./a &
pid=$! & 
./b pid

But it does not work the way I wanted.但它并没有像我想要的那样工作。 It runs b first and returns an error because the PID of a does not exist.它运行B首先与由于所涉及的PID不存在将返回错误。 I can't use && as well because it will wait a to finish first before b starts which is not the way I want.我也不能使用&& ,因为它会在b开始之前先等待a完成,这不是我想要的方式。

What modification should I do to my code regarding such dependency?关于这种依赖,我应该对我的代码做哪些修改?

Don't set pid in the background, and remember to put a $ when you want to expand it:后台不要设置pid ,要展开的时候记得加$

./a & 
pid=$!
./b "$pid"

Or just要不就

./a &  
./b $!

Write a bash script which will check for ProcessA using pgrep and gets its pid编写一个 bash 脚本,该脚本将使用 pgrep 检查 ProcessA 并获取其 pid

 #!/bin/sh while true do pid=`pgrep -f processA` if [ ! -z $pid ] then ./processB $pid break fi done

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

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