1

I have two script files running simultaneously. I just need to kill the java process running in one script without affecting the java process in the another one.

  • 2
    In both scripts you can get the process id of the java application. E.g runJavaApp & then in the next line scr1javaAppId=$! You can kill the running process using kill -s 15 $scr1javaAppid – Valentin Bajrami Aug 20 '14 at 8:44
  • 1
    Are you trying to do this from the script itself, or are you trying to do this by hand from outside the scripts? The above comment only applies in the first case. – Barmar Aug 20 '14 at 20:38
2
pgrep -x script1 | xargs -I pid pkill -x -P pid java

Would kill the java processes whose parent process is called script1.

| improve this answer | |
0

There are many ways to implement it, to make it robust, to avoid race on temp files and etc.. but just that you are able to start with:

script1:
# add the line:
# $! returns the process id  of last job run in background
java -jar myjar1 &
echo $! > /tmp/script1.txt
...
# kill the script 2
# -9 SIGKILL or -15 SIGTERM
kill -9 `cat /tmp/script2.txt`

script2:
# add the line:
# $! returns the process id  of last job run in background
java -jar myjar2 &
echo $! > /tmp/script2.txt
...
# kill the script 1
# -9 SIGKILL or -15 SIGTERM
kill -9 `cat /tmp/script1.txt`

to make it better you could check if the file exist before to "cat it" with

if [ -e "/tmp/scriptN.txt" ]; then
    kill -9 `cat /tmp/scriptN.txt`
fi
| improve this answer | |
  • kill -9 is the worst idea I've ever seen. kill -9 doesn't send a signal to the parent process which subsequently will signal it's children to finish work. It just kills the process without notification. You'd need to send the SIGTERM to the process. Another clean way to kill the process is using pkill which will defaultly send SIGTERM. Though pkill is not availiable on all modern OS'es. – Valentin Bajrami Aug 20 '14 at 10:00
  • i gave him the option.. he can simply decide.. its in the comment -9 or -15..btw kill send as well, as default SIGTERM. man 1 kill, if you need help. – VP. Aug 20 '14 at 10:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.