简体   繁体   English

如何在运行时在Shell脚本中启用某些命令

[英]how to enable some commands during run time in a shell script

I have written 1 shell script to run Jstack command for a particular Process ID (PID). 我已经编写了1个Shell脚本来为特定的进程ID(PID)运行Jstack命令。

But some time it may happen that multiple PIDs are there in a server for Java process. 但是有时可能会在服务器中为Java进程提供多个PID。

At that case i want to run that many Jstack commands giving respective PIDs as input to the command. 在那种情况下,我想运行那么多的Jstack命令,将各自的PID作为命令的输入。

Eg. 例如。 If one application has 2 servers (1 tomcat and 1 jboss), then I need to run 2 JStack commands to capture 2 different logs for 2 processes. 如果一个应用程序有2个服务器(1个tomcat和1个jboss),那么我需要运行2个JStack命令来为2个进程捕获2个不同的日志。

So how to handle or check so that the script will automatically decide how many PIDs r there for java process and will run the commands written inside the script? 那么,如何处理或检查脚本以自动确定Java进程中有多少个PID,并运行脚本中编写的命令?

My script is getting all the PIDs active by 我的脚本正在通过以下方式激活所有PID

PID1=$(ps -ef|grep java|grep jboss| awk '{print $2}' )

and

PID2=$(ps -ef|grep java|grep tomcat| awk '{print $2}' )

after that I am running Jstack commands as 之后,我作为

jstack $PID1 > jStack1.txt & and jstack $PID2 > jStack2.txt &

you need to combine the pids into one list and loop round them. 您需要将这些pid合并到一个列表中并对其进行循环。

So something like this to get a seperate file for each pid: 所以像这样为每个PID得到一个单独的文件:

for pid in $( ps -ef | egrep "tomcat|jboss" | awk '{print $2}')
do
    jstack $pid > jstack.$pid.txt
done

Following on from your last comment 接下来的最后一条评论

I'm not sure what you are trying to do with the array and multiple jstack calls in the loop as it will iterate once for each pid, not give you two pids in the loop, and the $0 & $1 indices don't make sense (did you mean just 0 & 1?), and you are using $N each time but the increment for it is commented out so will stay as 0. 我不确定您要在循环中使用数组和多个jstack调用做什么,因为它将为每个pid迭代一次,而不是在循环中给您两个pid,并且$ 0和$ 1索引没有意义(您的意思是仅是0&1吗?),并且您每次都使用$ N,但是它的增量已被注释掉,因此将保持为0。

If you are sure there can only be two pids, one for tomcat and one for jboss, then your inital code with sleeps added would do it: 如果您确定只能有两个pid,一个为tomcat,一个为jboss,则您的初始代码加上睡眠可以做到这一点:

#!/bin/bash 
Sleep1=$1 

# sleep for the first requested time
sleep $Sleep1

# do the tomcat jstack
PID1=$(ps -ef | grep java| grep tomcat | awk '{print $2}')
jstack $PID1 > jstack.tomcat.$PID1.txt

# sleep for another 60secs
sleep 60

# do the jboss jstack
PID2=$(ps -ef | grep java| egrep "jboss|JBoss" | awk '{print $2}')
jstack $PID2 > jstack.jboss.$PID1.txt

If there can be multiple tomcat processes and multiple jboss processes, then you need two loops: 如果可以有多个tomcat进程和多个jboss进程,则需要两个循环:

#!/bin/bash 
Sleep1 = $1 

# sleep for the first requested time
sleep = $Sleep1 

# Do all the tomcat jstacks
for pid in $(ps -ef | grep java| grep "tomcat" | awk '{print $2}')
do 
    jstack $pid > jstack.tomcat.${pid}.txt ) 
done

# sleep for another 60secs
sleep 60

# Do all the jboss jstacks
for pid in $(ps -ef | grep java| egrep "jboss|JBoss" | awk '{print $2}')
do 
    jstack $pid > jstack.jboss.${pid}.txt ) 
done

Or some combinations of these methods could be used depending on exactly what you are after. 或者可以根据您的实际情况使用这些方法的某些组合。

To get the pid you can just use pgrep instead of ps/grep/grep/awk : 要获取pid,您可以使用pgrep代替ps/grep/grep/awk

for pid in $(pgrep -f "tomcat|jboss")
do
  jstack $pid >> jStack1.txt 
done

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

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