Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Startup (/etc/init.d/glassfish) script for Glassfish 3.1 on Centos 6.
#!/bin/env python
# glassfish Startup script for the Glassfish java application server.
# chkconfig: 2345 85 15
# description: GlassFish is an open source application server project started \
# by Sun Microsystems for the Java EE platform and now sponsored \
# by Oracle Corporation. The supported version is called Oracle \
# GlassFish Server. GlassFish is free software, dual-licensed \
# under two free software licences: the Common Development and \
# Distribution License (CDDL) and the GNU General Public License \
# (GPL) with the classpath exception. (This text is from wikipedia)
# processname: glassfish
# pidfile: /var/run/
# Used and tested on centos 6.
# Author:
# These values should be changed to reflect your mysql servers ip.
# The linux user whish glassfish should be started with.
# The path to where you have your glassfish server installed.
# Import used python modules.
from socket import *
import sys
import os
import pwd
import datetime
import time
import subprocess
import shlex
from optparse import OptionParser
# String codes affecting output to shell.
BOLD = "\033[1m"
RESET = "\033[0;0m"
# Globals set with set_global_options_and_args.
ARGS = None
def print_verbose(message, verbose = 1, add_new_line=True):
if (verbose <= OPTIONS.verbose):
if (add_new_line):
message += "\n"
def is_server_alive(server, port):
Check if PORT on SERVER is responding, this assumes the server is alive.
result = -1
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((server, int(port)))
return (result == 0)
def wait_for_server_to_start(server, port):
Wait until SERVER PORT gets opened.
message = "Wait until port %s on server %s opens" % (port, server)
print_verbose(message, 1, False)
while(not is_server_alive(server, port)):
print_verbose(".", 1, False)
print_verbose(".", 1)
def wait_for_mysql_to_start():
Wait for all mysql servers defined in global MYSQL_IPS to start.
for ip in MYSQL_IPS:
wait_for_server_to_start(ip, 3306)
def is_glassfish_user():
Is this program executed by the user defined in global OWNER?
return (pwd.getpwuid(os.getuid()) == OWNER)
def shellquote(s):
return "'" + s.replace("'", "'\\''") + "'"
def x(cmd):
Execute the shell command CMD as the user defined in global OWNER.
if not is_glassfish_user():
cmd = "su %s -c %s" % (OWNER, shellquote(cmd))
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
return (stdout, stderr,
def write_to_log(domain_name, message, verbose=1):
Write MESSAGE to screen and the startup log for the gf domain DOMAIN_NAME.
The logs are stored in
print_verbose(message, verbose)
file_name = "%s/domains/%s/logs/startup.log" % (GLASSFISH_PATH, domain_name)
now ="%Y-%m-%d %H:%M")
message = now + " - " + message;
cmd = "echo '%s' >> %s" % (message, file_name)
print_verbose(BOLD + "Command: " + RESET + cmd, 3)
(stdout, stderr, pid) = x(cmd)
result = stdout + stderr
if (result):
print_verbose(result, 1)
def asadmin_exec(domain_name, command):
Execute COMMAND with glassfish asadmin.
asacmd = "%s/bin/asadmin %s" % (GLASSFISH_PATH, command)
print_verbose(BOLD + "Command: " + RESET + asacmd, 2)
write_to_log(domain_name, "Command: " + asacmd, 99)
(stdout, stderr, pid) = x(asacmd)
if (stdout):
write_to_log(domain_name, stdout, 2)
if (stderr):
write_to_log(domain_name, stderr, 1)
return pid
def start(domain_name):
write_to_log(domain_name, "Starting Glassfish (%s)." % domain_name)
asadmin_exec(domain_name, "start-domain " + domain_name)
write_to_log(domain_name, "Started.", 2)
def stop(domain_name):
write_to_log(domain_name, "Stopping Glassfish (%s)." % domain_name)
asadmin_exec(domain_name, "stop-domain " + domain_name)
write_to_log(domain_name, "Stopped.", 2)
def restart(domain_name):
write_to_log(domain_name, "Retart Glassfish (%s)." % domain_name)
asadmin_exec(domain_name, "restart-domain " + domain_name)
write_to_log(domain_name, "Restarted.", 2)
def set_global_options_and_args():
Set cmd line arguments in global vars OPTIONS and ARGS.
usage = "usage: %prog [-anvqh] {start|stop|restart}"
parser = OptionParser(usage=usage)
parser.add_option("-n", "--no-db-check", action="store_true",
help="Don't check for a started database server.")
parser.add_option("-a", "--all-domains", action="store_true",
help="Commands affect both domain1 and domain2.")
parser.add_option("-v", "--verbose", default=1,
action="count", dest="verbose",
help="Makes the output more verbose. Add more v:s for " +
"verbosity. Mostly useful for debugging.")
parser.add_option("-q", "--quiet",
action="store_const", const=0, dest="verbose",
help="Don't print any output.")
(OPTIONS, ARGS) = parser.parse_args()
if len(ARGS) != 1:
parser.error("incorrect number of arguments")
def main():
# Check for all mysql servers to start before starting glassfish.
# This might be neceasary if your applications uses mysql.
if not OPTIONS.no_db_check:
# Handle the dynamic arguments on from the command line.
commands = {'start' : start, 'stop': stop, 'restart' : restart}
command = ARGS[0].lower()
if command in commands:
if (OPTIONS.all_domains):
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment