I needed to run a Java program as a service on a server running RHEL. I used the script below to get it working. I got direction from
1. http://stackoverflow.com/questions/11203483/run-a-java-application-as-a-service-on-linux
2. http://www.apexninjas.com/blog/2011/02/start-java-program-as-linux-daemon/ and
3. http://fedoraproject.org/wiki/Packaging:SysVInitScript#Initscript_template
The third link provides a detailed explanation on init scripts; I recommend that, at the very least, you skim through it. I needed a cleaner implementation than link 1 and a less verbose implementation than link 2. This is what I eventually used:
#!/bin/sh # # myprogram Startup script for my Java program # # chkconfig: <startlevellist> <startpriority> <endpriority> e.g. chkconfig: 2345 85 15 Explained #http://fedoraproject.org/wiki/Packaging:SysVInitScript#.23_chkconfig:_line. Run levels are here. http://en.wikipedia.org/wiki/Runlevel # # description: My Java program that......\ # mows the Linux lawn ### BEGIN INIT INFO # Provides: # Required-Start: # Required-Stop: # Should-Start: # Should-Stop: # Default-Start: # Default-Stop: # Short-Description: Start and Stop my java program # Description: My Java program does this and that ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions exec=/path/to/the/jarfile.jar prog=myprogram #config="<path to your config file>" #Add a config file if you need one. Usually put in /etc/ pidfile=${PIDFILE-/var/run/myprogram.pid} logfile=/var/log/myprogram/myprogram.log errorlog=/var/log/myprogram/error.log [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=${LOCKFILE-/var/lock/subsys/$prog} start() { #[ -f $config ] || exit 6 #No config for now. You can add it here & uncomment #The check for if the service is already running is done prior to this in rh_status_q() echo -n $"Starting $prog: " # Start the service nohup java -jar $exec >>$logfile 2>>$errorlog & echo $! >$pidfile retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " # stop it here kill $(<$pidfile) rm -f $pidfile retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { restart } force_reload() { restart } rh_status() { # run checks to determine if the service is running or use generic status if [ -f $pidfile ] then if [ -e /proc/$(<$pidfile) ] then echo -ne "$prog (pid $(<$pidfile)) is running...\n" fi else echo -ne "$prog is stopped\n" fi } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q || exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
Place the script here: /etc/init.d/
Also, mkdir /var/log/myprogram
Now to start your service:
service myprogram start
To stop it:
service myprogram stop
To check status:
service myprogram status
Look through other init scripts in /etc/init.d/ for other ways to do this