1

I have a script that starts, kills or restarts several java programs. However, when I execute it, it ends up running multiple multiple times in the background.

This is my script:

#!/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

When I execute it, it does its job, and starts\kills all programs, but then I see this in the output of 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

So, why is this getting stuck like this? I mean, it does its job but it bugs me having all that in the background.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.