Issue
Hi I am new to Python scripting. I am trying to execute jboss startup shell script through python. The shell script when executed directly works fine, but when trying to execute through python it is not working fine. I have here by attached my shell script and also python script as well. Please help me.
First, the shell script being run -- start_jboss.sh
:
#export JAVA_HOME=/opt/appserver/jdk1.7.0_79/
export INSTANCE=jbstd-rss-D04
export JBOSS_HOME=/opt/appserver/JBOSS/jboss-eap-6.4/
export CONF_HOME=/opt/appserver/JBOSS/jbstd-rss-D04/standalone/
export LOGFILEPATH=/opt/logs/jboss/${INSTANCE}
export JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=512m -Xverify:none -
Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -
Dsun.rmi.dgc.server.gcInterval=3600000 -server -XX:+DoEscapeAnalysis -
XX:+UseCompressedOops -XX:+UseParallelGC -XX:+UseParallelOldGC -Xms1024M -
Xmx1024M -verbose:gc -Xloggc:/opt/logs/jboss/${INSTANCE}/gc.log -
XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -
XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=2 -XX:GCLogFileSize=20M"
nohup sh ${JBOSS_HOME}/bin/standalone.sh -
Djboss.server.base.dir=${CONF_HOME} -
Djboss.server.config.dir=$CONF_HOME/configuration/ -c standalone-full-ha.xml
-Djboss.node.name=jbstd-rss-D01
Djavax.net.ssl.trustStore=/opt/appserver/JBOSS/SSL/truststores/truststore.jks -Djavax.net.ssl.trustStorePassword=Was6user1 -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.keyStore=/opt/appserver/JBOSS/SSL/keystores/keystore.jks -Djavax.net.ssl.keyStorePassword=Was6user1 -Djboss.server.log.dir=${LOGFILEPATH} -DlogFilePath=${LOGFILEPATH} -Dorg.jboss.as.logging.per-deployment=false -Djboss.socket.binding.port-offset=300 -DRSS_HOME=/opt/appserver/rss >> /dev/null &
Second, the Python wrapper, start_jboss_service.py
:
#!/bin/python
import sys
import os
import subprocess
#get the instance names to start instances
input=str(sys.stdin.read())
instances=input.split(',')
numberOfInstances=len(instances)
jboss_home="/opt/appserver/JBOSS/"
if (numberOfiInstances > 0):
for i in (0,numberOfInstances):
cmd="%s%s/start_jboss.sh"%(jboss_home,instances[i])
subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
...being started as follows:
python start_jboss_service.py jbstd-rss-D04
Solution
Issue One: Blocking Subprocess Output
You're redirecting output to a pipeline, but then you're never actually consuming content from that pipeline. Even though nohup
will redirect to /dev/null
after it starts, anything that tries to write prior to that point will be blocked on the buffer being consumed, preventing anything in your script after that attempted write from taking place.
You have two options:
- Remove
stdout=subprocess.PIPE
, and let your script write directly to the Python process's original stdout. - Call
communicate()
on thePopen
object you've constructed.
Taking the former approach, your command may simply become:
for instance in string.split(input, ','):
if not instance: continue # ignore trailing or doubled-up commas
subprocess.Popen(['%s%s/start_jboss.sh' % (jboss_home, instance)])
Issue Two: Reading Command-Line Arguments
If you run python start_jboss_service.py jbstd-rss-D04
, sys.stdin.read()
does not return jbstd-rss-D04
: This is not passed on standard input, but on the argument list.
Thus, you need to remove the line sys.stdin.read()
, and instead parse the argument list.
And if you accept multiple arguments, you should pass each instance name as a separate argument -- at which point you don't need to split on commas at all:
for instance in sys.argv[1:]:
subprocess.Popen(['%s%s/start_jboss.sh' % (jboss_home, instance)])
Answered By - Charles Duffy Answer Checked By - Mary Flores (WPSolving Volunteer)