Archive

Posts Tagged ‘scripting’

Looping over a range in bash

April 7th, 2009

I don’t know why I always forget how to do ranges, but I do. I guess it has something to do with the fact that I don’t expect it to be like C at all, and I don’t need to use it often enough to remember. I always assume it to be some oddball syntax like x in range 2..6 or something.

Do this:

for ((x=2;x<=6;x++)); do echo $x; done

And the output will be:

2
3
4
5
6

Almost random bash, for, loop, range, scripting, shell, syntax

How to start or stop a process that’s slow to respond

November 18th, 2008

Got a process that you want to restart in a script but it doesn’t respond nicely?  Use the sleep command in your script and check its status after you start, stop, or kill it.  After incrementally backing off a few times, waiting longer and longer, I give up and exit with an error.  But you could come back later, or basically raise an exception by saving the value of “$?”. You can do this as you start a process and want to make sure it’s fully up and running before moving on because it dies sometimes unexpectedly.  There’s a ton of uses for sleep.

DAEMON=myapp
sudo /etc/rc.d/init.d/$DAEMON start
sleep 1
if [ `sudo ps -ef | grep -c $DAEMON` == "1" ]; then
sleep 2
if [ `sudo ps -ef | grep -c $DAEMON` == "1" ]; then
sleep 3
if [ `sudo ps -ef | grep -c $DAEMON` == "1" ]; then
sleep 3
if [ `sudo ps -ef | grep -c $DAEMON` == "1" ]; then
echo
echo “ERROR: $DAEMON did not restart.”
echo “Quitting Early!…”
exit 1
fi
fi
fi
fi

Linux, Solaris howto, processes, scripting, shell, sleep


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics