Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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 –  val0x00ff yesterday
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 17 hours ago

2 Answers 2

pgrep -x script1 | xargs -I pid pkill -x -P pid java

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

share|improve this answer

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
share|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. –  val0x00ff yesterday
    
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. yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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