简体   繁体   English

在Linux上运行“ top”命令几分钟,然后在perl中退出系统命令

[英]Running 'top' command of linux for few minutes and then come out of system command in perl

I am new to Perl. 我是Perl的新手。 I am working on a web UI where I have to give CPU and memory monitoring data, so I am using top and gnuplot command. 我正在使用必须提供CPU和内存监视数据的Web UI,因此我正在使用top和gnuplot命令。 I am able to do it through my terminal. 我可以通过终端来做到这一点。 but when I am executing same commands with a perl script its not working. 但是当我用perl脚本执行相同的命令时,它不起作用。 The problem I am having is that whenever I am executing top command in my terminal than I have to wait for few minutes and then I have to plot it using GNUPLOT but when I am doing the same work using system command in perl I am unable to give that delay. 我遇到的问题是,每当我在终端中执行top命令时,都必须等待几分钟,然后才必须使用GNUPLOT对其进行绘制,但是当我在perl中使用system命令执行相同的工作时,我无法给那个延迟。

Here is what I am doing 这是我在做什么

system "top -p 1758 -b -d5 | tee -a stats.log";
system "./top_stats.sh -f stats.log"

Here 1758 is the app ID whose top data I have to monitor and stats.log file is the one where I am saving logs and then using this stats.log file as input to top_stats.sh script I am plotting graphs. 这里的1758是应用程序ID,我必须监视其最重要的数据,而stats.log文件是我保存日志的位置,然后将这个stats.log文件用作top_stats.sh脚本的输入(正在绘制图形)。 This top_stats.sh script takes the log file and uses gnuplot to plot data 此top_stats.sh脚本获取日志文件并使用gnuplot绘制数据

Now the problem is whenever I am executing this first system command in terminal I have to wait for some time say 2 to 3 minutes to have ample number of data points and then I have to press Ctrl+C to come out of top command and then run the script. 现在的问题是,每当我在终端中执行此第一个系统命令时,我都必须等待一段时间(例如2到3分钟)以拥有足够数量的数据点,然后我必须按Ctrl + C才能显示出顶部命令,然后运行脚本。 but here as soon as this first system command is encountered it is executed and then next command is executed without any delay so I am not getting any data points to plot the graph. 但是在这里,一旦遇到第一个系统命令,它将立即执行,然后立即执行下一个命令,因此我没有得到任何数据点来绘制图形。 Is there any way I can execute my first command and wait for 3 minutes without coming out of system command and then execute the next command.?? 有什么方法可以执行我的第一个命令并等待3分钟而又不会退出系统命令,然后执行下一个命令。

Is there any way I can execute my first command and wait for 3 minutes without coming out of system command and then execute the next command.?? 有什么方法可以执行我的第一个命令并等待3分钟而又不会退出系统命令,然后执行下一个命令。

Why not specify the number of iteration for top . 为什么不为top指定迭代次数。 So your command will rougly run for (N-1)*5 seconsds. 因此,您的命令将运行(N-1)* 5秒。 37 iterations with an interval of 5 seconds are going to take 36*5 seconds: 以5秒为间隔的37次迭代将花费36 * 5秒:

system "top -p 1758 -b -d5 -n37 | tee -a stats.log && ./top_stats.sh -f stats.log";

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

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