Java running as a Unix service [duplicate] Java running as a Unix service [duplicate] unix unix

Java running as a Unix service [duplicate]


Well, if you want to run your java program even when you exit out of your shell, the following is the most simple way:

$nohup java -jar program.jar &


You need to create an appropriate script in /etc/init.d and link it to /etc/rcX.d directories. The script should support at least start, stop, and status parameters. During start it should run java command with appropriate arguments, probably via nohup java <arguments> &. Then you should save PID of your newly-started process to file /var/run/yourservice.pid. stop command should read this PID file and kill this service.The details vary from distribution to distribution, most distributions provide some macros to make whole job easier. It's best to look at examples of other services in /etc/init.d for your distribution.

Additionally:If your service isn't accessed from other computers from the network, but it opens some port, make it unavailable with firewall.

If your service processes some 'delicate' data, it's good to add another user and invoke an appropriate sudo command in your /etc/init.d file.


You can start it as:

java -jar program.jar

Unix daemons are normally started by init or started by a script in /etc/init.d or /etc/rc.d, and started at specific runlevels - normally by soft links in /etc/rcX.d. (where X is the intended "runlevel" which is normally 3.

I think debian are moving to using "upstart", a init-replacement. It uses config files in /etc/init to define jobs, and they are quite easy to write. Check that out.

Daemons traditionally closes stdin, sdtout and stderr, and does a "double fork" when starting, in order to detach from the session and also to signal that they are ready to handle whatever they should handle. This is not really necessary, as long as the daemon is not started from the terminal.

If you want a simple shell wrapper to start you program; you just need to write a small shell script:

#!/bin/sh/full/path/to/java -jar /full/path/to/program.jar

... and make it executable (chmod 755 )