Link to home
Start Free TrialLog in
Avatar of Los Angeles1
Los Angeles1

asked on

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

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
Avatar of Mazdajai
Mazdajai
Flag of United States of America image

You can also use if statement.

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

Open in new window

Avatar of Los Angeles1
Los Angeles1

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, exactly, thanks