Solved

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

Posted on 2013-12-27
5
194 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
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux ssh 4 59
linux pull logs and export FTP 4 60
Virtualizing very old guest OS 4 80
Run same command on multiple files in Linux 3 3
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now