Archive

Posts Tagged ‘shell’

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

Control VMware virtual machines from the command line

March 27th, 2009

The vmrun command makes administering VMware virtual machines fast and easy when you’re stuck dealing with VMware Server 2.0. I really hate the web interface. It’s not responsive and in my opinion, a huge step backwards from the old 1.x consoles. If you’re trying to start, stop, pause, take a snapshot, or revert to a snapshot through the web gui, you’re going to spend about 10x more time launching the gui, waiting, waiting, and more waiting while the web app loads. I prefer to use the command line for just about everything, so using vmrun was a natural fit for me. In a one liner, I can do all these things without ever leaving the keyboard.

# vmrun –help

vmrun version 2.0.0 build-122956
Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]
AUTHENTICATION-FLAGS
--------------------
These must appear before the command and any command parameters.
   -h <hostName>  (not needed for Workstation)
   -P <hostPort>  (not needed for Workstation)
   -T <hostType> (ws|server|server1)
     for example, use '-T server' for Server 2.0
                  use '-T server1' for Server 1.0
   -u <userName in host OS>  (not needed for Workstation)
   -p <password in host OS>  (not needed for Workstation)
   -gu <userName in guest OS>
   -gp <password in guest OS>

Power Commands

start           Path to vmx file
stop            Path to vmx file
reset           Path to vmx file
suspend         Path to vmx file
pause           Path to vmx file
unpause         Path to vmx file

Snapshot Commands

snapshot             Path to vmx file
deleteSnapshot       Path to vmx file
revertToSnapshot     Path to vmx file

Easy enough… So here’s a couple of examples:

$ vmrun -u root -h ‘https://localhost:8222/sdk’ -p start “/home/vmware/Fedora10.vmx”
$ vmrun -u root -h ‘https://localhost:8333/sdk’ -p pause “/home/vmware/Gentoo.vmx”

ooo! Shiny... pause, shell, snapshot, start, stop, vmrun, vmware

See console messages in remote shells

March 14th, 2009

In Ubuntu, Fedora and other systems I’ve seen rsyslog running on, to see the console messages you have to have physical access to the server usually through a KVM or IP-KVM setup. Kernel messages are sent to /dev/console while mail, crit, debug, and others get sent to files.

to tail

Any of these message can be monitored remotely since they’re output as files by using the tail utility. Executing tail with the –follow switch in a shell allows you to watch the ouput as it is written to a file, in real time. So all you have to do is write kern messages to a file too…

$ sudo tail -n 88 -f /var/log/messages

or not to tail

But why tail a file when you could just send the kernel messages straight to a user account name. Instead of specifying a path to a file within rsylog.conf, add a user name instead or in addition to a file path.

kern /dev/console,rootninja

Now whenever i’m logged in as rootninja it’s just like i’m right there at the console.

log spillage

If you’re spewing out logs faster than rsyslog can handle and you’re comfortable with the possibility of losing data if you get a system crash between the write and the next disk sync, you can begin each file entry with a minus sign to omit syncing and squeeze out that last bit of performance. I wouldn’t suggest this unless you know you need it. When debugging talky apps, this would be better than just writing out your own files. I only mention this because I haven’t convinced any of the developers I work with to write to syslog for any of the apps they’re writing, even when these apps are interrelated… but that’s a whole new story!

advanced stuff

I haven’t even scratched the surface. I like being able to use templates for output and to send logs to remote IP’s, but other features of rsyslog are beyond anything that I have needed. You can use z0 through z9 for compressing messages sent over TCP. Rsyslog will compress any messages over 60 bytes long. I think the CPU overhead is probably a bad trade-off for just a minor improvement. It’s usually safe to try out new settings since misconfigurations in rsyslog.conf are usually ignored (such as using templates before defining them), so your configurations probably won’t fall down go boom, but that also makes it less obvious to know if things are working like you want.

From syslog’s website: http://www.rsyslog.com/

Rsyslog is an enhanced multi-threaded syslogd with a focus on security and reliability. Among others, it offers support for on-demand disk buffering, reliable syslog over TCP, SSL, TLS and RELP, writing to databases (MySQL, PostgreSQL, Oracle, and many more), email alerting, fully configurable output formats (including high-precision timestamps), the ability to filter on any part of the syslog message, on-the-wire message compression, and the ability to convert text files to syslog. It is a drop-in replacement for stock syslogd and able to work with the same configuration file syntax.

Go here if you want to join the rsyslog mailing list: http://lists.adiscon.net/mailman/listinfo/rsyslog

Uncategorized kernel, messaes, performance, remote, rsyslog, shell, syslog

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