|
Home > Archive > Unix Programming > June 2006 > Slow script - Possible to improve?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Slow script - Possible to improve?
|
|
|
| Hi all,
I've got the following little script which gets a list of services in
/etc/init.d and echos them out - with a dot in front if it can see them
running.
ls -1 /etc/init.d | while true
do
read a || break
if ps -u root | grep "$a" &> /dev/null
then
echo ". $a"
else
echo " $a"
fi
done
Unfortunately it's pretty slow - I was wondering could this be made
faster with a nice bit of perl/awk/sed etc?
Thanks
alexs
| |
| spibou@gmail.com 2006-06-21, 1:22 pm |
|
alexs wrote:
> Hi all,
>
> I've got the following little script which gets a list of services in
> /etc/init.d and echos them out - with a dot in front if it can see them
> running.
>
> ls -1 /etc/init.d | while true
> do
> read a || break
> if ps -u root | grep "$a" &> /dev/null
> then
> echo ". $a"
> else
> echo " $a"
> fi
> done
>
> Unfortunately it's pretty slow - I was wondering could this be made
> faster with a nice bit of perl/awk/sed etc?
>
> Thanks
> alexs
The first idea which comes to mind is to put the output of ps -u root
in a temporary file and use grep "$a" on the file.
| |
| spibou@gmail.com 2006-06-21, 1:22 pm |
|
spibou@gmail.com wrote:
> alexs wrote:
>
>
> The first idea which comes to mind is to put the output of ps -u root
> in a temporary file and use grep "$a" on the file.
Or better yet replace the 'ps -u root | grep "$a"' part with
'pgrep -u root "$a"'
| |
| Mark Rafn 2006-06-21, 1:22 pm |
| alexs <spam@alexs.org> wrote:
>I've got the following little script which gets a list of services in
>/etc/init.d and echos them out - with a dot in front if it can see them
>running.
>ls -1 /etc/init.d | while true
>do
> read a || break
> if ps -u root | grep "$a" &> /dev/null
> then
> echo ". $a"
> else
> echo " $a"
> fi
>done
>Unfortunately it's pretty slow - I was wondering could this be made
>faster with a nice bit of perl/awk/sed etc?
There's a lot I'd change, before even looking at performance.
- there may be services you want to monitor that aren't in init.d (started
by rc.local or some other process).
- there are likely a lot of things in init.d that are not used by a given
system or at your current runlevel.
- there are likely init.d scripts that start processes which have a
different name than the init.d script
But if you're just looking for a quick and dirty fix, the key is to figure out
where you're spending time, and do it faster or less often (or in some cases,
do it in parallel).
In this case, one problem jumps out: you're running ps once for every file
in init.d, when you could run it once before the loop.
Oh, and your syntax looks a bit strange. While true with a break is an odd
construct. Try:
psout=`ps -u root | cut -c 25-`
ls -1 /etc/init.d | while read name
do
if echo $psout | grep "$name" > /dev/null
then
echo ". $name"
else
echo " $name"
fi
done
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
| |
| Chris F.A. Johnson 2006-06-21, 7:22 pm |
| On 2006-06-21, Mark Rafn wrote:
> alexs <spam@alexs.org> wrote:
>
>
> There's a lot I'd change, before even looking at performance.
> - there may be services you want to monitor that aren't in init.d (started
> by rc.local or some other process).
> - there are likely a lot of things in init.d that are not used by a given
> system or at your current runlevel.
> - there are likely init.d scripts that start processes which have a
> different name than the init.d script
>
> But if you're just looking for a quick and dirty fix, the key is to figure out
> where you're spending time, and do it faster or less often (or in some cases,
> do it in parallel).
>
> In this case, one problem jumps out: you're running ps once for every file
> in init.d, when you could run it once before the loop.
>
> Oh, and your syntax looks a bit strange. While true with a break is an odd
> construct. Try:
> psout=`ps -u root | cut -c 25-`
Most versions of ps have an option (o) for selecting which
information to print. That would make cut unnecessary.
> ls -1 /etc/init.d | while read name
You don't need the -1 option when the output is not a terminal.
In fact, you don't need ls at all:
cd /etc/init.d
for name in *
do
> do
> if echo $psout | grep "$name" > /dev/null
You don't need grep:
case $psout in
*"$name"*) echo ". $name" ;;
*) echo " $name" ;;
esac
> then
> echo ". $name"
> else
> echo " $name"
> fi
> done
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
|
|
Chris F.A. Johnson wrote:
> On 2006-06-21, Mark Rafn wrote:
>
> Most versions of ps have an option (o) for selecting which
> information to print. That would make cut unnecessary.
>
>
> You don't need the -1 option when the output is not a terminal.
>
> In fact, you don't need ls at all:
>
> cd /etc/init.d
> for name in *
> do
>
>
> You don't need grep:
>
> case $psout in
> *"$name"*) echo ". $name" ;;
> *) echo " $name" ;;
> esac
>
>
>
Thanks for all the replies. Id tried putting ps output in a var
already, but forgot to mention that - that hadn't really helped.
I'm using your solution chris - much much better! thanks.
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence
Also thanks for this - It maybe going in a script idea i've had for
service control using a program i wrote called bosh (bosh.sf.net)
</shameless self promotion>
|
|
|
|
|