BTTB: looping for shell script under embedded linux
November 17th, 2010 mysurface Posted in cut, for, if, seq, Text Manipulation | Hits: 251172 | 19 Comments »
You may already realized Linux happened to appear at many places, such as web server, storage server, desktop, kiosk machine, mobile devices. Yes, more and more devices running embedded Linux. Yeah, Android is a modified version of Linux kernel too!
Scarcity is still an issue, embedded Linux can be very different to Linux that hosted on desktop or high-end servers. You may be provided with simple shell (sh but not bash, not ksh and not csh) and limited commands, it bundles into a BusyBox, The Swiss Army Knife of Embedded Linux.
I am a bit upset when I holding this so called Swiss Army Knife ( probably a customized one, super tiny one), why this command also don’t have, that command also not available. Well, I am still quite new to embedded Linux perhaps.
My busybox does not have bash, therefore a lots of bash syntax can’t be applied to my script, ouch! I do wrote some post about bash. Such as if … then … fi and download multiple files from a site using for loop like c in bash. The if…then…fi examples may still valid for shell script, because that is the fundamental one.
The very basic for loop for shell script is like this
for p in 1 2 3 4 5
do
echo $p
done
You can do simplified it by using command seq ( if seq is available in your busybox)
for p in `seq 1 5`
do
echo $p
done
Well, my swiss army knife doesn’t contain me seq command, it doesn’t have bash, no top command, giving me a fake ps with limited info. I can’t check my how busy is my processors and thread easily (because fake ps do not tells you that), I need to access it through /proc/[PID] and thank God, it does provide me the cut command.
Yes I able to cut by using my swiss army knife.
Due to the scarcity, I learn more about /proc. /proc is a pseudo-filesystem that provides tones of process information. Check out the proc 5 manual for detail information.
man 5 proc
What I am interested is the file /proc/[PID]/stat, the status information about the process. stat provides a long array of values, I am interested on column 1,2,14 and 15 which is PID, Command, utime and stime.
Ok, list me all the process’s utime and stime. I wrote a simple script call probe.sh.
#!/bin/sh
for p in /proc/[0-9]*;
do
cat $p/stat | cut -d" " -f1,2,14,15
done
The output looks decent, time takes less then 1 second.
...
6 (cpuset) 0 0
608 (scsi_eh_0) 0 1
612 (scsi_eh_1) 0 0
615 (scsi_eh_2) 0 1
661 (kpsmoused) 0 0
671 (kstriped) 0 0
677 (kondemand/0) 0 0
7 (khelper) 0 8
707 (usbhid_resumer) 0 0
854 (udevd) 0 11
real 0m0.965s
user 0m0.138s
sys 0m0.752s
But the list is long and overwhelming. What if I have targeted pid range?
#!/bin/sh
START=2000
END=3000
until [ $START -gt $END ]
do
p="/proc/$START/stat"
if [ -f $p ] ;
then
cat $p | cut -d" " -f1,2,14,15
fi
START=`expr $START + 1`
done
The output is promising, but it took too long to produce that.
2391 (syslog-ng) 0 0
2392 (syslog-ng) 1 11
2451 (gpm) 0 0
2882 (dhcpcd) 0 0
real 0m7.145s
user 0m0.959s
sys 0m5.640s
The expr for arithmetic function seems to consume a lots of processing time.
Another approach, which amazingly gives correct result and fast.
START=2000
END=3000
for p in /proc/[0-9]*;
do
pid=`basename $p`
# omg no basename? replace with the line below
# pid=`cat $p | cut -d"/" -f3`
if [ $pid -ge $START ]
then
if [ $pid -le $END ]
then
cat $p/stat | cut -d" " -f1,2,14,15
fi
fi
done
Check out the result. Outstanding isn’t it?
2391 (syslog-ng) 0 0
2392 (syslog-ng) 1 11
2451 (gpm) 0 0
2882 (dhcpcd) 0 0
real 0m0.550s
user 0m0.069s
sys 0m0.447s
I had briefly illustrate the looping mechanism on shell script, I hope that gives you some ideas while you working under embedded Linux.
January 1st, 2011 at 12:04 am
Hi, when you mentioned some optimization…
(System is running on 2 CPUs.)
NOT BAD:
time {
for i in /proc/[0-9]*; do pid=`basename $i`; done
}
real 0m0.706s
user 0m0.292s
sys 0m0.464s
??
BETTER:
time {
cd /proc
for i in [0-9]*; do pid=$i; done
}
real 0m0.010s
user 0m0.004s
sys 0m0.004s
NOT BAD:
time {
cd /proc
for i in [0-9]*
do
cat $i/stat | cut -d” ” -f1,2,14,15
done
}
real 0m0.729s
user 0m0.744s
sys 0m0.688s
BETTER:
time {
cd /proc
for i in [0-9]*; do cat $i/stat; done | cut -d” ” -f1,2,14,15
}
real 0m0.460s
user 0m0.444s
sys 0m0.336s
EVEN BETTER (sorry, no loop):
time {
cat /proc/[0-9]*/stat | cut -d” ” -f1,2,14,15
}
real 0m0.022s
user 0m0.008s
sys 0m0.020s
I home, this can help somebody just to have another angle of view (time and CPU consumption is small anyway).
Lukas
January 2nd, 2011 at 5:52 am
If test (“[“) is not shell built-in, then one test is faster than two tests…
[ $pid -ge $START -a $pid -le $END ]
Lukas
January 22nd, 2011 at 1:22 pm
Thanks Lukas, your optimization is awesome. :)
January 22nd, 2011 at 1:30 pm
First of all, thanks for the great tips on using /proc for getting process info, this post was very helpful in helping me understand the internals of Linux.
I tested the command line you proposed for those systems that don’t have the basename utility and got this:
cat: /proc/1: Is a directory
../Code/bash/pid-from-proc.sh: line 10: [: -ge: unary operator expected
for each running process.
I changed that line to this:
pid=$(cat $p/stat | cut -d” ” -f1)
and it worked. I used the $() construct for command substitution in place of backticks “ as it is the preferred method on modern systems.
Thanks again,
Carl
January 22nd, 2011 at 3:53 pm
@Carl, you can try the one suggested by Lukas too.
simply:
cd /proc
cat [0-9]*/stat | cut -d” ” -f1,2,14,15
if range is not important for you.
or
cd /proc
for i in [0-9]*;
do
if [ $i -ge $START -a $i -le $END ]; then
cat $i/stat;
fi;
done | cut -d” ” -f1,2,14,15
for the range version.
January 22nd, 2011 at 5:49 pm
Just a heads up on how the website handles code. What appears to be double quotes in the last line of code:
done | cut -d” ” -f1,2,14,15
is something else (“smart” quotes??), thus making cut and paste unworkable without further editing.
Rock on!
Carl
January 22nd, 2011 at 9:33 pm
@Carl, must be the html unicode, unless the code put into
<pre><code></code></pre>
March 12th, 2011 at 12:40 am
Hey very nice blog!! Man .. Excellent .. Amazing .. I will bookmark your web site and take the feeds also…I am happy to find numerous useful information here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .
September 10th, 2012 at 3:36 pm
Really nice blog, and helpful discussions. thanks for posting.
October 21st, 2014 at 4:52 am
This standard become too popular in the computer world
and is mostly followed in connection wuth all peripheral devices
like mouse, keyboards, printers, digital video camera,
external drives etc. Various solutions use different compression types, but you will be interested to know that Windows 7 uses VHD arrangement that Microsoft also
functions in its Windows Virtual PC software and it is server-based Hyper-V virtualization solutions.
These ports, also known as connections, enable the notebook to connect to a variety of devices suuch as cameras,
speakers, external hard disks, scanners, printers, etc.
Feel free to surff to my page; clé windows 7
December 2nd, 2015 at 9:46 pm
Very interesting. I love embedded systems. I created my own LED lamp based on RaspberryPi and work excellently. Here is a video: http://rootconsole.net/my-led-cube/
May 12th, 2016 at 1:54 pm
Great looking internet site. Presume you did a bunch of your ownyour very own html coding
May 12th, 2016 at 1:54 pm
Rather educational, looking forward to visiting again
June 30th, 2016 at 3:54 pm
Great article again. Thank you!
August 18th, 2016 at 12:27 am
You’re a very useful internet site; could not make it without ya!
madden nfl 17 http://www.getzcope.com/forum/comments.php?DiscussionID=2597
October 7th, 2017 at 7:05 pm
相关的主题文章: It was amazing to be a part of it, and unlike anything I’ve ever experienced.Dining. Now is the time to get the financial commitment. Whether someone carries a backpack or the newest Louis Vuitton,cheap oakley sunglasses sale, personally,oakley sunglasses outlet uk, I don’t care. sea 8f4e5f96. They are closing the huge space of this broken global marketplace and are offering a original shot for anyone in business to create new fron
January 7th, 2019 at 7:16 am
awesome! Thanks for the article!
October 23rd, 2019 at 8:10 pm
Divorce can be a major life change for each couple, but if you and your spouse have kids with them, you may feel as though your divorce is extra intricate. Child-raising can’t be an afterthought in the decisions that you make on your divorce; your better half might be going to be a huge part of one’s own life until finally your kids become adults. Coparenting after divorce might be hard, however, it doesn’t have to be more debilitating. You can find equally as many great parenting strategies for divorced couples as there are for married people. Needless to say, each and every divorce is exceptional in a unique method, but you’ll find a number of general rules that will help any bunch boost joyful children as soon as they divide up.
Visit article about how to get a divorce without lawyers