Solved

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

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

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

856 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