Lines 1-176
Link Here
|
1 |
#! %%PYTHON_CMD%% |
|
|
2 |
|
3 |
################################################################################ |
4 |
# Author: Jean-Baptiste Quenot <jb.quenot@caraldi.com> |
5 |
# Purpose: Manage resin pid file and log files |
6 |
# Date Created: 2005-01-21 15:43:19 |
7 |
# Revision: $FreeBSD: head/www/jetty8/files/jettyctl.in 310487 2013-01-16 10:21:51Z olgeni $ |
8 |
################################################################################ |
9 |
# Copyright (c) 2004, Jean-Baptiste Quenot <jb.quenot@caraldi.com> |
10 |
# All rights reserved. |
11 |
# |
12 |
# Redistribution and use in source and binary forms, with or without |
13 |
# modification, are permitted provided that the following conditions are met: |
14 |
# |
15 |
# * Redistributions of source code must retain the above copyright notice, this |
16 |
# list of conditions and the following disclaimer. |
17 |
# * Redistributions in binary form must reproduce the above copyright notice, |
18 |
# this list of conditions and the following disclaimer in the documentation |
19 |
# and/or other materials provided with the distribution. |
20 |
# * The name of the contributors may not be used to endorse or promote products |
21 |
# derived from this software without specific prior written permission. |
22 |
# |
23 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
27 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
31 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
32 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 |
################################################################################ |
34 |
# |
35 |
# Files handled by this script (pid file, log files) must reside in a writable |
36 |
# directory, ie the directory must be owned by the user running the program. |
37 |
|
38 |
import sys, os, signal, time, stat, re |
39 |
|
40 |
# -socketwait 12345 |
41 |
# -stdout $APP_HOME/log/stdout.log |
42 |
# -stderr $APP_HOME/log/stderr.log |
43 |
|
44 |
def readProcessId(): |
45 |
f = open(PID_FILE, 'r') |
46 |
pid = int(f.readline()) |
47 |
f.close() |
48 |
return pid |
49 |
|
50 |
def isProgramRunning(pid): |
51 |
# Send a dummy signal to the process. If it died, an exception is |
52 |
# thrown |
53 |
try: |
54 |
os.kill(pid, signal.SIGCONT) |
55 |
return 1 |
56 |
except OSError: |
57 |
return 0 |
58 |
|
59 |
def usage(): |
60 |
print >> sys.stderr, "Usage: %s {start|stop|restart}" % sys.argv[0] |
61 |
|
62 |
def start(): |
63 |
cwd = os.getcwd() |
64 |
if os.path.exists(PID_FILE): |
65 |
# Read the process id |
66 |
pid = readProcessId() |
67 |
|
68 |
if isProgramRunning(pid): |
69 |
print >> sys.stderr, '%s already started' % APP_NAME |
70 |
sys.exit(3) |
71 |
|
72 |
if not(os.path.exists(COMMAND)): |
73 |
print >> sys.stderr, '%s cannot be found' % COMMAND |
74 |
sys.exit(3) |
75 |
|
76 |
# Append program output to a log file |
77 |
l = open(LOG_FILE, 'a') |
78 |
orig_stderr = os.dup(sys.stderr.fileno()) |
79 |
os.dup2(l.fileno(), sys.stdout.fileno()) |
80 |
os.dup2(l.fileno(), sys.stderr.fileno()) |
81 |
|
82 |
finfo = os.stat(COMMAND)[stat.ST_MODE] |
83 |
executable = stat.S_IMODE(finfo) & 0111 |
84 |
if not(executable): |
85 |
sys.stderr = os.fdopen(orig_stderr, 'w') |
86 |
print >> sys.stderr, 'Cannot run %s, execute bit is missing' % COMMAND |
87 |
sys.exit(5) |
88 |
|
89 |
if APP_HOME: |
90 |
# Change current directory to APP_HOME |
91 |
os.chdir(APP_HOME) |
92 |
|
93 |
# Start program in the background |
94 |
pid = os.spawnv(os.P_NOWAIT, COMMAND, ARGS) |
95 |
|
96 |
# Wait a little |
97 |
time.sleep(.4) |
98 |
(status_pid, status) = os.waitpid(pid, os.WNOHANG) |
99 |
|
100 |
# Check program exit status, if available |
101 |
if status_pid != 0 and os.WIFEXITED(status): |
102 |
sys.stderr = os.fdopen(orig_stderr, 'w') |
103 |
print >> sys.stderr, 'Could not start %s. Check %s for errors.' % (APP_NAME, LOG_FILE) |
104 |
sys.exit(2) |
105 |
|
106 |
# It's alive, so write down the process id |
107 |
os.chdir(cwd) |
108 |
f = open(PID_FILE, 'w') |
109 |
print >> f, pid |
110 |
f.close() |
111 |
|
112 |
def warnNotRunning(): |
113 |
if sys.argv[1] == "stop": |
114 |
print >> sys.stderr, '%s is not running' % APP_NAME |
115 |
else: |
116 |
print >> sys.stderr, 'Warning: %s was not running' % APP_NAME |
117 |
|
118 |
def cleanup(): |
119 |
os.unlink(PID_FILE) |
120 |
|
121 |
def stop(): |
122 |
if os.path.exists(PID_FILE): |
123 |
# Read the process id |
124 |
pid = readProcessId() |
125 |
else: |
126 |
warnNotRunning() |
127 |
return |
128 |
|
129 |
if not(isProgramRunning(pid)): |
130 |
warnNotRunning() |
131 |
cleanup() |
132 |
return |
133 |
|
134 |
# Terminate program |
135 |
os.kill(pid, signal.SIGTERM) |
136 |
|
137 |
while isProgramRunning(pid): |
138 |
time.sleep(.1) |
139 |
|
140 |
cleanup() |
141 |
|
142 |
if __name__ == '__main__': |
143 |
LOG_FILE = "%%LOG_FILE%%" |
144 |
APP_NAME = "%%APP_NAME%%" |
145 |
APP_HOME = "%%APP_HOME%%" |
146 |
PID_FILE = "%%PID_FILE%%" |
147 |
COMMAND = "%%PREFIX%%/bin/java" |
148 |
ARGS = [COMMAND] |
149 |
|
150 |
ARGS += sys.argv[1:-1] |
151 |
|
152 |
ARGS += [ |
153 |
"-Djetty.home=%%APP_HOME%%", |
154 |
"-jar", |
155 |
"%%APP_HOME%%/start.jar" |
156 |
] |
157 |
|
158 |
os.environ['PATH'] = "%%LOCALBASE%%/bin:/usr/bin:/bin" |
159 |
|
160 |
if len(sys.argv) < 2: |
161 |
usage() |
162 |
sys.exit(1) |
163 |
|
164 |
if sys.argv[-1] == "start": |
165 |
start() |
166 |
|
167 |
elif sys.argv[-1] == "stop": |
168 |
stop() |
169 |
|
170 |
elif sys.argv[-1] == "restart": |
171 |
stop() |
172 |
start() |
173 |
|
174 |
else: |
175 |
usage() |
176 |
sys.exit(1) |