Recalculate variable for each pass of an until loop
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.)
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!
#! /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!).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.