Tag Archives: coding

Bash scripts based on running processes

At work, our ERP system is built on Oracle running on a clustered Linux (Red Hat Enterprise) environment. Since this is a high availability/failover setup the processes running our development databases can be on either server for any number of reasons. I wanted to make installing code a bit easier on the server so I made some bash scripts to partially automate the install for me. Not wanting to get errors when it tries to install code on the wrong server, I found this little command to determine if the database is running on a server and thus let the script go on with the install.

ps -U oracle -u oracle u | grep -q pmon_dev2

Lets break this down into each part. The first part, ps -U oracle -u oracle u, gets the currently running processes running as “oracle” (first “-U” for real and second “-u” for effective ID) in a user-oriented format (last “u”). This is similar to an example in the MAN page for PS and I just replaced the usernames to fit my case. The pipe (“|”) takes the output from the left side, the PS command in this case, and uses it as input for the right side. The second part, grep -q pmon_dev2, searches for “pmon_dev2” in the output from PS, the “-q” option suppresses output and exits successfully if any match is found.

So I just put this at the beginning of my script:

if ! ps -U oracle -u oracle u |grep -q pmon_dev2
then
echo "DEV2 not running"
exit 1
fi

This exits the script right away if the database isn’t running on the server. If it is, then the script goes on to get the latest code from source control run the code install and exit. If you can do this with no user input, then you can setup a CRON job to run the script to automatically get any updates.

[googleplusauthor]

We Interrupt Your Regular Broadcast to Bring You This Important Message.

After spending most of this week working with and debugging approximately 8,000 lines of PL/SQL contained in two packages, I want to mention “Refactoring” by Martin Fowler. It’s one of the few books I had to buy in college and have kept since then. If your code smells, clean it up… That is all.

Now back to your regularly scheduled programming.

[googleplusauthor]