Solved

Linux script, can I improve this while loop ( or is this good )

Posted on 2013-12-27
5
202 Views
Last Modified: 2013-12-27
I wrote the following script to kill all existing java processes

pkill -9 java
while [ $? -eq 0 ]
do
   pkill -9 java
done

Open in new window


This works well, I was just wondering if there is a more elegant way in which to write it

Is there a method to  place the 'pkill -9 java' into the 'while' line inside the brackets ?

Is there a shorter way to write this?

Thanks
0
Comment
Question by:Los Angeles1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39742083
You can also use if statement.

if ps aux|grep java
then
pkill -9 java
fi

Open in new window

0
 

Author Comment

by:Los Angeles1
ID: 39742128
I want to delete one to many java programs

What I want is to kill as many java processes that are actually running

Is there a way to write that with less lines ?

Thanks
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39742138
What do you mean by less lines? You can make any script into one liner.

if ps aux|grep java; then pkill -9 java; fi

Open in new window


Your script looks fine. pkill will terminate any process, I do not see a need to place them in the loop.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39742198
Do you mean something like this?

while pkill -9 java; do : ; done

?
0
 

Author Closing Comment

by:Los Angeles1
ID: 39742313
Yes, exactly, thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question