我有一个脚本可以启动、终止或重新启动多个 java 程序。但是,当我执行它时,它最终会在后台运行多次。
这是我的脚本:
#!/bin/bash
export jvmArgs="-jar -Xms1024m -Xmx2048m -Djava.security.egd=file:/dev/../dev/urandom"
function killer () {
app=$1
kill -9 $(ps -ef | grep ${app} | grep -v grep | awk '{print $2}');
}
function start_acolds ()
{
basePath=/path/to/apps/
cd ${basePath}
cd ${basePath}app0 && nohup java ${jvmArgs} app0.jar >/dev/null 2>&1&
cd ${basePath}app1 && nohup java ${jvmArgs} app1.jar >/dev/null 2>&1&
cd ${basePath}app2 && nohup java ${jvmArgs} app2.jar >/dev/null 2>&1&
cd ${basePath}app3 && nohup java ${jvmArgs} app3.jar >/dev/null 2>&1&
cd ${basePath}app4 && nohup java ${jvmArgs} app4.jar >/dev/null 2>&1&
cd ${basePath}app5 && nohup java ${jvmArgs} app5.jar >/dev/null 2>&1&
cd ${basePath}app6 && nohup java ${jvmArgs} app6.jar >/dev/null 2>&1&
cd ${basePath}app7 && nohup java ${jvmArgs} app7.jar >/dev/null 2>&1&
cd ${basePath}app8 && nohup java ${jvmArgs} app8.jar >/dev/null 2>&1&
}
function kill_acolds(){
killer app0.jar
killer app1.jar
killer app2.jar
killer app3.jar
killer app4.jar
killer app5.jar
killer app6.jar
killer app7.jar
killer app8.jar
}
case "$1" in
start)
echo "Iniciando servicios... "
start_acolds
;;
stop)
echo "Deteniendo servicios..."
kill_acolds
;;
restart)
kill_acolds
start_acolds
;;
*)
echo "Modo de empleo: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
当我执行它时,它会完成它的工作,并启动\杀死所有程序,但后来我在以下输出中看到了这一点ps
:
user1@UbuntuMachine:/$ ps -ef|grep -v grep|grep acoldp.sh.dc
user1 12241 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12242 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12243 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12244 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12245 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12246 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12247 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12248 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12249 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12252 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12253 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12254 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12257 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
user1 12258 1 0 11:24 pts/1 00:00:00 /bin/bash ./acoldp.sh.dc restart
那么,为什么会陷入这样的困境呢?我的意思是,它完成了它的工作,但它让我在后台处理所有这些事情。