linux script to kill java process
You can simply use pkill -f
like this:
pkill -f 'java -jar'
EDIT: To kill a particular java process running your specific jar use this regex based pkill command:
pkill -f 'java.*lnwskInterface'
Run and kill java program in background in Linux
start.sh:
javaProgram &
pid=$!
echo $pid >/var/run/javaProgram.pid
stop.sh:
kill $(cat /var/run/javaProgram.pid)
Kill a java process (in linux) by process name instead of PID
Here is the command to kill the Java process by is Process Name instead of its ProcessID.
kill -9 `jps | grep "DataNode" | cut -d " " -f 1`
Let me explain more, about the benefit of this command. Lets say you are working with Hadoop cluster. Its often required that you check java daemons running with jps
command. Lets say when you give this command on worker nodes, you see following output.
1915 NodeManager
18119 DataNode
17680 Jps
Usually, if we want to kill DataNode process, we would use following command
kill -9 18119
But, it is little bit difficult to type the PID
, to use kill
command. By using the command, given in this answer, it is easy to write the name of the process. We can also prepare shell scripts to kill commonly used deamons in hadoop cluster,
or we can prepare one shell script and can use parameter as process name.
How to quickly kill java processes in bash?
You can save the PIDs when you start the processes so you can use them later:
nohup ./start-gossip &
START_GOSSIP_PID=$!
nohup ./start &
START_PID=$!
nohup ./start-admin &
START_ADMIN_PID=$!
...
kill -9 $START_GOSSIP_PID
kill -9 $START_PID
kill -9 $START_ADMIN_PID
This has the advantage (over pkill
) of not killing off any other processes that coincidentally have similar names. If you don't want to perform the kill operation from the script itself, but just want to have the PIDs handy, write them to a file (from the script):
echo $START_GOSSIP_PID > /some/path/start_gossip.pid
Or even just do this when you launch the process, rather than saving the PID to a variable:
nohup ./start-gossip &
echo $! > /some/path/start_gossip.pid
Stop Java from killing Bash script that started it
Well, after a lot of searching and some prompting from the comments, I found that the issue lied with how the program was initially being started.
As mentioned in the update, the first run is always started by an ssh connection. I knew there was a slight problem with this ssh connection, as it seemed to hold onto the connection no matter what I did. It turns out that this was causing the problem that resulted in the Bash instance and the Java instance remaining attached.
The solution for this problem was found here: jsch ChannelExec run a .sh script with nohup "lose" some commands
After managing to get the initial setup to start with nohup properly, the issue has gone away.
Kill a Specific set of process from shell script
Not sure if this works but I think this is what it would look like based on Stephen P's comment. This assumes the java program only spawns chrome processes as children and no other processes.
#!/bin/bash
$openjava=""
while true
do
java -jar uploadv2.jar &
$openjava="$openjava $!" # List of all java programs running
# The $! gets the most recently spawned process id
for process in $openjava
do
childProc="$childProc `pgrep -P $process`" #get chrome processes spawned by java
done
for tab in childProc
do
kill $tab #kill the child processes (chrome tabs)
done
done
Related Topics
Colored Shell Script Output Library
List Files Over a Specific Size in Current Directory and All Subdirectories
Android Studio Error After ./Studio.Sh
Error When Trying to Run .Asm File on Nasm on Ubuntu
Shared Libraries: Windows VS Linux Method
How to Mount an Iso Inside a Docker Container
Installing Openssh on the Alpine Docker Container
Convert a Fixed Width File from Text to CSV
How to Open Port 2195 in Iptables Centos 6 for Activating the Apns
Bash Script to Calculate Time Elapsed
I Want to Contribute to the Linux Kernel
How to Convert an Ssl Certificate in Linux
Linux Script with Curl to Check Webservice Is Up
Convert Multipage PDF to a Single Image
How to Ssh into Remote Linux by Ngrok
Setting Up Permissions for Wordpress on Amazon Ec2 (Amazon Linux)
Lsb_Release: Command Not Found in Latest Ubuntu Docker Container