LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2010, 12:11 PM   #1
wideyes
LQ Newbie
 
Registered: Dec 2009
Posts: 19

Rep: Reputation: 0
Recalculate variable for each pass of an until loop


Hello. I am trying to write a script that will find any user on the server running more than one instance of the epiphany web browser process, then kill all their epiphany pids. (This is necessary sometimes to cure lockups, and repeat processes are the telltale sign in this case). I have tried implementing the until loop:

Code:
USER=`ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}'`

until [[ "$USER" == "" ]]

do
   ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill

done
Unfortunately, the script only calculates the USER variable once at the beginning of the script. Fair enough, that's how it's written. My question is, how would I get the USER variable to update for each pass of the until loop so that it could cure multiple users of this ailment if necessary? That is, calculate $USER, kill their epiphany pids, repeat those two steps until the $USER variable comes up blank, then exit.

Thanks for any thoughts.
 
Old 02-11-2010, 12:45 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You need to create your variable in the loop somewhere, I would probably do it something like this (untested).

Code:
for i in $(ps aux | grep epiphany | grep -v grep | sort | uniq -d -w5 | awk '{print $1}'); do 
  killall -u $i epiphany 
done
(edit: note this doesn't recalculate the variable every run, but it will run multiple iterations for the users it finds -- eg: if there are three users with epiphany open multiple times it will loop three times.)

Last edited by rweaver; 02-11-2010 at 01:53 PM.
 
Old 02-11-2010, 01:12 PM   #3
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,741

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
Thumbs up

Quote:
Originally Posted by wideyes View Post
Your script:
Code:
USER=`ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}'`

until [[ "$USER" == "" ]]
do
   ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill

done
What about:
Code:
set `ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}'`

while [ $# -gt 0 ]
do
     USER=$1
     ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill
     shift
done
or even:
Code:
ps aux | grep epiphany | grep -v grep | sort | uniq -d -w 5 | awk '{print $1}' | while read USER
do
     echo "Whacking processes for user ${USER}..."
     ps aux | grep epiphany | grep -v grep | grep $USER | awk '{print $2}' | sudo xargs kill
done
I'm not entirely sure this is what will do the trick (not being sure how to duplicate your problem to test the above code)
 
Old 02-11-2010, 02:12 PM   #4
wideyes
LQ Newbie
 
Registered: Dec 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Aha! I had a suspicion shift could be used on the variable, but I didn't think to assign USER to argument $1 (which, in hindsight, is pretty obvious). Good idea.

I like the succinctness of rweaver's suggestion as well. I'm still learning what I can and can't accomplish with the for loop. I'm going to go and test these little lumps. I'll update with my thoughts!
 
Old 02-11-2010, 07:07 PM   #5
wideyes
LQ Newbie
 
Registered: Dec 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Ok! Here's the complete script I went with:
Code:
#! /bin/bash
echo "Scanning workstations for frozen internet browsers..."
TEST=`ps aux | grep epiphany | grep -v grep | sort | uniq -d -w5 | awk '{print $1}'`
if [[ $TEST == "" ]];
  then
        echo "No frozen browsers found. If problem persists, restart the workstation."
  else
        for USER in $TEST
          do
                killall -u $USER epiphany 2> /dev/null
                echo "$USER has been exorcised!"
        done
fi
Many thanks for all the suggestions.

I had initially

I noticed that the killall command outputs some errors that make it appear to be not working (maybe due to my script), but it is effective, so I redirected standard error to the bitbucket. If I like it well enough, I'll just set it up as a cron task to run every 15 min. or so (my workplace is full of folks who just love to pull up densely resource-hungry webpages on their thin clients. If only I could script human bahavior!).
 
Old 02-12-2010, 01:23 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
The disadvantage of set and shift solutions is that they loose the original command arguments (unless done in a subshell). rweaver's for <var> in <list> solution does not have this disadvantage.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass the variable contain lrios Programming 7 04-18-2008 04:56 PM
bash how to pass value to variable in this function? babag Programming 10 04-02-2008 11:42 PM
pass javascript variable to php ALInux Programming 6 01-06-2006 06:20 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM
Pass text to variable Zed Linux - Software 6 05-12-2003 03:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration