4 # Copyright (c) 1999, 2006 Tanuki Software Inc.
6 # Java Service Wrapper sh script. Suitable for starting and stopping
7 # wrapped Java applications on UNIX platforms.
10 #-----------------------------------------------------------------------------
11 # These settings can be modified to fit the needs of your application
15 APP_LONG_NAME="Freenet 0.7"
18 WRAPPER_CMD="/usr/bin/wrapper"
19 WRAPPER_CONF="/etc/freenet-wrapper.conf"
21 # Priority at which to run the wrapper. See "man nice" for valid priorities.
22 # nice is only used if a priority is specified.
24 # Note that Freenet will scale its usage within the specifed niceness, some
25 # threads will have a lower priority (higher nice value) than this. Also please
26 # don't renice Freenet once it's started.
29 # Location of the pid file.
30 PIDDIR="/var/freenet/"
32 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
33 # When launched with the 'start' command, it will also ignore all INT and
37 # If specified, the Wrapper will be run as the specified user.
38 # IMPORTANT - Make sure that the user has the required privileges to write
39 # the PID file and wrapper.log files. Failure to be able to write the log
40 # file will cause the Wrapper to exit without any way to write out an error
42 # NOTE - This will set the user which is used to run the Wrapper as well as
43 # the JVM and is not useful in situations where a privileged resource or
44 # port needs to be allocated prior to the user being changed.
47 # The following two lines are used by the chkconfig command. Change as is
48 # appropriate for your application. They should remain commented.
49 # chkconfig: 2345 20 80
50 # description: @app.long.name@
52 # Do not modify anything beyond this point
53 #-----------------------------------------------------------------------------
55 if [ "X`id -u`" = "X0" -a -z "$RUN_AS_USER" ]
57 echo "Do not run this script as root."
61 # and get java implementation too, Sun JDK or Kaffe
62 JAVA_IMPL=`java -version 2>&1 | head -n 1 | cut -f1 -d' '`
64 # sun specific options
66 #if [ "$JAVA_IMPL" = "java" ]
68 # echo Sun java detected.
69 # # Tell it not to use NPTL.
70 # # BAD THINGS happen if it uses NPTL.
71 # # Specifically, at least on 1.4.1. and 1.5.0b2, we get hangs
72 # # where many threads are stuck waiting for a lock to be
73 # # unlocked but no thread owns it.
75 # ## won't work on libc2.4 ... let's hope it's fixed
76 # if [[ -z "$(/lib/libc.so.6 | head -n 1 | grep 'release version 2.4')" ]]
78 # if [[ -d /lib/tls ]]
80 # LDPROP="set.LD_ASSUME_KERNEL=2.4.1"
86 # Get the fully qualified path to the script
97 # Resolve the true real path without any sym links.
99 while [ "X$CHANGED" != "X" ]
101 # Change spaces to ":" so the tokens can be parsed.
102 SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
103 # Get the real path to this script, resolving any symbolic links
104 TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'`
107 REALPATH="$REALPATH/$C"
108 while [ -h "$REALPATH" ] ; do
109 LS="`ls -ld "$REALPATH"`"
110 LINK="`expr "$LS" : '.*-> \(.*\)$'`"
111 if expr "$LINK" : '/.*' > /dev/null; then
114 REALPATH="`dirname "$REALPATH"`""/$LINK"
119 # Change ":" chars back to spaces.
120 REALPATH="`echo $REALPATH | sed -e 's;:; ;g'`"
121 SCRIPT="`echo $SCRIPT | sed -e 's;:; ;g'`"
123 if [ "$REALPATH" = "$SCRIPT" ]
131 # Change the current directory to the location of the script
132 cd "`dirname \"$REALPATH\"`"
134 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
135 # the working directory is later changed.
136 FIRST_CHAR="`echo $PIDDIR | cut -c1,1`"
137 if [ "$FIRST_CHAR" != "/" ]
139 PIDDIR="$REALDIR/$PIDDIR"
141 # Same test for WRAPPER_CMD
142 FIRST_CHAR="`echo $WRAPPER_CMD | cut -c1,1`"
143 if [ "$FIRST_CHAR" != "/" ]
145 WRAPPER_CMD="$REALDIR/$WRAPPER_CMD"
147 # Same test for WRAPPER_CONF
148 FIRST_CHAR="`echo $WRAPPER_CONF | cut -c1,1`"
149 if [ "$FIRST_CHAR" != "/" ]
151 WRAPPER_CONF="$REALDIR/$WRAPPER_CONF"
155 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
156 PIDFILE="$PIDDIR/$APP_NAME.pid"
158 LOCKFILE="$LOCKDIR/$APP_NAME"
162 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
173 #We use the 1.5 jvm if it exists
174 if [ -d /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/ ]
176 export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home"
184 # Resolve the architecture
185 DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
187 'amd64' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
193 'power' | 'powerpc' | 'power_pc' | 'ppc64')
196 'pa_risc' | 'pa-risc')
207 # Check if we are running on 64bit platform, seems like a workaround for now...
208 DIST_BIT=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
210 'amd64' | 'ia64' | 'x86_64' | 'ppc64')
213 # 'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
216 'sun4u' | 'sparcv9') # Are all sparcs 64?
222 *) # In any other case default to 32
227 # Decide on the wrapper binary to use.
228 # 64bit wrapper by default on 64bit platforms, because
229 # they might not have 32bit emulation libs installed.
230 # For macosx, we also want to look for universal binaries.
232 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
234 if [ -x "$WRAPPER_TEST_CMD" ]
236 WRAPPER_CMD="$WRAPPER_TEST_CMD"
238 if [ "$DIST_OS" = "macosx" ] # Some osx weirdness, someone please check that this still works
240 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
241 if [ -x "$WRAPPER_TEST_CMD" ]
243 WRAPPER_CMD="$WRAPPER_TEST_CMD"
245 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
246 if [ -x "$WRAPPER_TEST_CMD" ]
248 WRAPPER_CMD="$WRAPPER_TEST_CMD"
250 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
251 if [ -x "$WRAPPER_TEST_CMD" ]
253 WRAPPER_CMD="$WRAPPER_TEST_CMD"
255 if [ ! -x "$WRAPPER_CMD" ]
257 echo "Unable to locate any of the following binaries:"
258 echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
259 echo " $WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
262 # We need -Djava.net.preferIPv4Stack=true on FreeBSD, otherwise recent jvms thow an IllegalArgumentException when we create the socket
264 NO_WRAPPER="java -Djava.net.preferIPv4Stack=true -cp freenet-ext.jar:freenet.jar freenet.node.NodeStarter"
270 if [ ! -x "$WRAPPER_CMD" ]
272 echo "Unable to locate any of the following binaries:"
273 echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
275 NO_WRAPPER="java -cp freenet-ext.jar:freenet.jar freenet.node.NodeStarter"
280 # Build the nice clause
281 if [ "X$PRIORITY" = "X" ]
285 CMDNICE="nice -$PRIORITY"
288 # Build the anchor file clause.
289 if [ "X$IGNORE_SIGNALS" = "X" ]
294 ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
295 IGNOREPROP=wrapper.ignore_signals=TRUE
298 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
301 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
307 # Check the configured user. If necessary rerun this script as the desired user.
308 if [ "X$RUN_AS_USER" != "X" ]
310 # Resolve the location of the 'id' command
311 IDEXE="/usr/xpg4/bin/id"
317 echo "Unable to locate 'id'."
318 echo "Please report this message along with the location of the command on your system."
323 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
325 # Already running as the configured user. Avoid password prompts by not calling su.
329 if [ "X$RUN_AS_USER" != "X" ]
331 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
332 # able to create the lock file. The Wrapper will be able to update this file once it
333 # is created but will not be able to delete it on shutdown. If $2 is defined then
334 # the lock file should be created for the current command
335 if [ "X$LOCKPROP" != "X" ]
339 # Resolve the primary group
340 RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
341 if [ "X$RUN_AS_GROUP" = "X" ]
343 RUN_AS_GROUP=$RUN_AS_USER
346 chown $RUN_AS_USER:$RUN_AS_GROUP "$LOCKFILE"
350 # Still want to change users, recurse. This means that the user will only be
351 # prompted for a password once.
352 su -m $RUN_AS_USER -c "$REALPATH $1"
354 # Now that we are the original user again, we may need to clean up the lock file.
355 if [ "X$LOCKPROP" != "X" ]
360 # Wrapper is not running so make sure the lock file is deleted.
361 if [ -f "$LOCKFILE" ]
377 pid="`cat \"$PIDFILE\"`"
378 if [ "X$pid" != "X" ]
380 # It is possible that 'a' process with the pid exists but that it is not the
381 # correct process. This can happen in a number of cases, but the most
382 # common is during system startup after an unclean shutdown.
383 # So make sure the process is one of "ours" -- that we can send
384 # a signal to it. (We don't use ps(1) because that's neither
386 if ! kill -0 $pid 2>/dev/null
388 # This is a stale pid file.
390 echo "Removed stale pid file: $PIDFILE"
393 # Sometimes the pid exists and it's ours!
394 if ! test -f /proc/$pid/cwd/Freenet.pid
396 # This is a stale pid file.
398 echo "Removed stale pid file2: $PIDFILE"
404 echo "Cannot read $PIDFILE."
411 if ! kill -0 $pid 2>/dev/null
413 # Process is gone so remove the pid file.
420 echo "Running $APP_LONG_NAME..."
424 COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" $LDPROP $ANCHORPROP $LOCKPROP"
427 echo "$APP_LONG_NAME is already running."
433 echo "Starting $APP_LONG_NAME..."
437 if [ "$NO_WRAPPER" ] # Check if we don't have usable wrapper, and run without it
440 echo "Let's start the node without the wrapper, you'll have to daemonize it yourself."
442 else # Otherwise use the wrapper
443 COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" $LDPROP wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP"
447 echo "$APP_LONG_NAME is already running."
453 echo "Stopping $APP_LONG_NAME..."
457 echo "$APP_LONG_NAME was not running."
459 if [ "X$IGNORE_SIGNALS" = "X" ]
461 # Running so try to stop it.
465 # An explanation for the failure should have been given
466 echo "Unable to stop $APP_LONG_NAME."
471 if [ -f "$ANCHORFILE" ]
473 # An explanation for the failure should have been given
474 echo "Unable to stop $APP_LONG_NAME."
479 # We can not predict how long it will take for the wrapper to
480 # actually stop as it depends on settings in wrapper.conf.
481 # Loop until it does.
485 while [ "X$pid" != "X" ]
487 # Show a waiting message every 5 seconds.
488 if [ "$CNT" -lt "5" ]
492 echo "Waiting for $APP_LONG_NAME to exit..."
495 TOTCNT=`expr $TOTCNT + 1`
504 if [ "X$pid" != "X" ]
506 echo "Failed to stop $APP_LONG_NAME."
509 echo "Stopped $APP_LONG_NAME."
518 echo "$APP_LONG_NAME is not running."
521 echo "$APP_LONG_NAME is running ($pid)."
527 echo "Dumping $APP_LONG_NAME..."
531 echo "$APP_LONG_NAME was not running."
538 echo "Failed to dump $APP_LONG_NAME."
541 echo "Dumped $APP_LONG_NAME."
549 checkUser $1 touchlock
554 checkUser $1 touchlock
564 checkUser $1 touchlock
580 echo "Usage: $0 { console | start | stop | restart | status | dump }"