Oracle Weblogic nodemanager STOP script Bash and Python

Everyone who is using Oracle Weblogic must had a thought like  “Where the *** is the stop script to kill my nodemanager proces”…. Well, i did. So i wrote a simple stop script which can be better probally, but it does the job.

#!/bin/bash
PID=`ps -efw | grep -i "Xms128m" | grep -i nodemanager | awk '{print $2}'`
if [ ! -z $PID  ]; then
  echo "Killing nodemanager with PID: $PID"
  kill $PID
  CHECK=`ps -e -o pid | grep $PID`
  if [ -z $CHECK ]; then
    echo "Nodemanager stop success"
  fi
else
  echo "No nodemanager process running"
fi

Remember to copy this snippet into a bash file and set the correct permissions.

Some developers like to stop nodemanager programmaticly. I.E.
Note: You could use it to stop and start adminservers for example…
You could use following python definitions:

# RETURN LIST OF PROCESS IDS FROM GREPING PROCESS COMMANDS
def psgrep(str):
    pat = re.compile(str)
    cmd = "ps -e -o pid,command"
    lines = shlines("ps -e -o pid,command")
    pids = []
    for line in lines:
        cols = string.split(line)
        if cols[1] == "grep": continue
        for col in cols[1:]:
            if pat.search(col):
                pids.append(cols[0])
                continue
    return pids

def stop_process_tree(ppid):
    time.sleep(1)
    pids = get_child_processes(ppid)
    while len(pids) > 0:
        for pid in pids:
            stop_process_tree(pid)
        pids = get_child_processes(ppid)
    kill_process(ppid)
    return

def kill_process(pid):
    if pid == None or pid == '':
        return
    time.sleep(1)
    if shout("ps -o pid= --pid " + pid) != '':
        os.system("kill " + pid)
    for i in range(30):
        time.sleep(1)
        if shout("ps -o pid= --pid " + pid) == '':
            return
    os.system("kill -9 " + pid)
    for i in range(30):
        time.sleep(1)
        if shout("ps -o pid= --pid " + pid) == '':
            return
    error("Cannot kill " + pid)
    return

def get_child_processes(ppid):
    return string.split(shout("ps -o pid= --ppid " + ppid))
Advertisements

Oracle database 11g and Python cx_Oracle site-packages

Since Python and Jython  is a powerfull way to help Oracle Fusion Middleware developers, the need for database access grows. Therefore i write this installation plan to connect your Pyhton code to a Oracle 11g database using:

Instant Client Package – Basic
Instant Client Package – SQL*Plus
Instant Client Package – SDK

Follow the steps below:

  1. Download the software listed above.
  2. Extract all downloaded zips of the Oracle Instant Client to a dir which is called the ORACLE_HOME
    I used /apps/oracle/instantclient_11_2
  3. Extract the cx_Oracle dir to a tempory location, i used /apps/oracle/cx_Oracle-5.1.1
  4. Add following exports to your global Linux profile:
    </pre>
    PATH=$PATH:$HOME/bin
    ORACLE_HOME=/apps/oracle/instantclient_11_2
    PATH=$ORACLE_HOME/bin:$PATH
    PATH=$ORACLE_HOME/lib64:$PATH
    LD_LIBRARY_PATH=/apps/oracle/instantclient_11_2
    export ORACLE_HOME
    export LD_LIBRARY_PATH
    export PATH
    <pre>
  5. Go to /apps/oracle/instantclient_11_2 and generate some soft links.
    ln -s libclntsh.so.11.1 libclntsh.so
    ln -s libocci.so.11.1 libocci.so
    ln -s libocijdbc11.so libocijdbc.so
    ln -s libnnz11.so libnnz.so
    
  6. Go to the extracted cx_Oracle directory and run:
    python setup.py build
    python setup.py install
    
  7. Now start a Python shell and try:
Python 2.7.2 (default, Mar 20 2012, 03:06:10)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle

 If You get “missing library .so” errors:

  • Check your .bash_profile for the correct $PATH, $LD_LIBRARY_PATH and $ORACLE_HOME exports.
  • Check the permissions from the instantclient dir (/apps/oracle/instantclient_11_2)

That’s all it is to. Good luck with using your database!

Simplefy installation with bin file creation on Linux

While working at different company’s i needed to install the same software over and over … To make my life easier i use a Python script which creates a runable binary file. Like for example weblogic1035.bin. Run it an it will install without promt you anything…

  1. What do you need in the package directory?

install.sh which is the execution script for the specific installation, in this case, Oracle Weblogic Server 10.3.1.5

#!/bin/sh
BEA_HOME=/opt/oracle
JAVA_HOME=/opt/jdk1.7.0_01
JAVA=$JAVA_HOME/bin/java

$JAVA -jar wls1035_generic.jar -mode=silent -silent_xml=silent.xml -log=$BEA_HOME/wls_11g_install.log

–  All files needed to perform the installation, in this case a silent.xml


<?xml version="1.0" encoding="UTF-8"?>
<bea-installer>
  <input-fields>
    <data-value name="BEAHOME" value="/opt/oracle/11g" />
    <data-value name="WLS_INSTALL_DIR" value="/opt/oracle/11g/wlserver_10.3" />
    <data-value name="COMPONENT_PATHS" value="WebLogic Server"/>
  </input-fields>
</bea-installer>

– The product installer, in this case wls1035_generic.jar

– Once you put every install component in place, you can can invoke the makeinstaller script which is described below:


#!/usr/bin/python
import os
import os.path
import string
import sys

EXTRACT = """#!/bin/bash
EXTRACT_ONLY=0
if [ "$1" = "--extract" ];then
  EXTRACT_ONLY=1
fi
export TMPDIR="%s"
mkdir -p $TMPDIR
ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0;}' $0`

tail -n+$ARCHIVE $0 | tar xz -C $TMPDIR

if [ $EXTRACT_ONLY -eq 1 ]; then
  exit 0
fi

CHDIR=`pwd`
cd $TMPDIR
./install.sh
cd $CHDIR
rm -rf $TMPDIR
exit 0

__ARCHIVE_BELOW__
"""
def usage():
  print "Usage: makeinstaller.py <package_dir> [destination_dir]"
  sys.exit(-1)

def pack(path, dest):
  dir = string.split(path, "/")[-1]
  extract = EXTRACT % dir
  cdir = os.getcwd()
  os.chdir(path)
  os.system("tar chzf ../payload.tgz *")
  os.chdir("..")
  installer = dest + "/" + dir + ".bin"
  open(installer, "w").write(extract)
  os.system("cat payload.tgz >>" + installer)
  os.system("chmod a+x " + installer)
  os.system("rm -f payload.tgz")
  os.chdir(cdir)
  return

if __name__ == "__main__":
  if len(sys.argv) < 2 or len(sys.argv) > 3:
    usage()
  path = os.path.abspath(sys.argv[1])
  if len(sys.argv) == 3:
    dest = os.path.abspath(".")
  pack(path, dest)

– Put the script one level higher as the package dir and the destination dir.

– Run the script as follow:

makeinstaller.py <package_dir> [destination_dir]”

– Now you can run the created bin file, good luck !

Python: UnicodeEncodeError: ‘ascii’ codec can’t encode character…

Developers like python because it is a fast way to develop scripts to make life easier. This is a fact on Linux, but sometimes you are forced to use Micr$s$ft. With this force comes the encoding fact Ascii. This will cause UnicodeEncodingErrors when the developer tries to print to stout. Below a small piece of code to help those who are forced to use Windows.

  import sys
  import codecs
  # Wrap sys.stdout with a writer that knows how to handle encoding
  wrapped_stdout = codecs.getwriter('UTF-8')(sys.stdout)
  # Replace sys.stdout with the writer
  sys.stdout = wrapped_stdout</blockquote>