Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

757 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

17 Experts available now in Live!

Get 1:1 Help Now