bash 在Linux中使用bash脚本捕获tomcat的pid以杀死
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24287091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
capture pid of tomcat to kill using bash script in Linux
提问by user3752761
I am trying to write a bash script that finds the PID
of tomcat6, kills it, starts it again, then waits 1 minute, then kills the process again and starts it again.
我正在尝试编写一个 bash 脚本来找到PID
tomcat6 的 ,杀死它,再次启动它,然后等待 1 分钟,然后再次杀死进程并再次启动它。
This is what I have so far but I am struggling to have the script kill tomcat using the previous pid:
这是我到目前为止所拥有的,但我正在努力让脚本使用以前的 pid 杀死 tomcat:
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
sleep 1m
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
Thanks
谢谢
回答by fatihc
T_PID=$(ps aux | grep Tomcat6 | awk 'NR==1{print }')
kill -9 $T_PID
service Tomcat6 start
sleep 1m
If grep gives multiple outputs, NR==x will only get the PID of desired line.
如果 grep 给出多个输出,NR==x 只会得到所需行的 PID。
回答by chrisaycock
You can just use
你可以使用
pkill tomcat6
回答by Hastur
To do it only one time it's enough (if you need sudo).
只做一次就足够了(如果你需要 sudo)。
sudo service tomcat6 restart ; sleep 1m ; sudo service tomcat6 restart ;
or even better
甚至更好
sudo /bin/bash -c "service tomcat6 restart; sleep 1m; service tomcat6 restart;"
This can be a never ending loop that you have to interrupt by hands. Take it like a scratch.
这可能是一个永无止境的循环,您必须手动中断。把它当作划痕。
#!/bin/bash
while :
do
pkill tomcat6 # or pkill -9 tomcat6
service tomcat6 start
sleep 1m
done
If I remember well it exists the possibility to restart that service. If so you can use even
如果我没记错的话,它存在重新启动该服务的可能性。如果是这样,您甚至可以使用
#!/bin/bash
while :
do
service tomcat6 restart
sleep 1m
done