简体   繁体   中英

Start Java jar file during Startup in Linux

Situation :

I have jar file on server PC (PC act as server), a.jar , which will be running infinite loop to listen for TCPIP connection from client PC . When connection has been made, client PC can send a zip file to server PC , a.jar will unzip it and execute a script with that zip, which will start another full screen display java file, b.jar .

a.jar will also trying to run b.jar during bootup, if b.jar already exist.


Approach 1:

I wrote a script in /etc/init.d/ to start a.jar , as it's infinite loop, the system hang forever during bootup ( in terminal screen, can't even get into GUI ). I have to boot into single user mode to remove that script in order to boot up normally.


Approach 2:

When I changed the script to run a.jar as a daemon, it runs normally, listen for connection, I can connect to the server pc and send zip file.

Problem:

  1. b.jar is not executed even when execution permission and files are all correctly set.
  2. After I sent a zip file, b.jar is executed ( because it is connected to a device and that device is activated ). But the full screen display is not shown like it did when executed via terminal.

Reasons of doing so

It may seems a bit complicated, but because b.jar will be constantly updated and it has to be auto start when after update, thus I use a.jar to unzip and execute new version fo b.jar .

And b.jar is required to run as root as it needs to access serial port.

( Server pc is required to be automated, thus it is required to start b.jar during startup and remotely controlled )


Question

May I know is there any better way of doing this? Or what did I do wrong? Can someone show me a better script to start it during startup?

I think that if you want to run the application as a daemon, the best solution is using Apache Jsvc :

Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily. Jsvc allows the application (eg Tomcat) to perform some privileged operations as root (eg bind to a port < 1024), and then switch identity to a non-privileged user. It can run on Win32 via the Cygwin emulation layer, however Win32 users may prefer to use procrun instead, which allows the application to run as a Windows Service.

Approach 1 should work but you must run the java process in the background by adding & to the command.

But there are better solutions to run java apps as a deamon. The above can be considered as a quick fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM