简体   繁体   English

在Linux中启动期间启动Java jar文件

[英]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 . 我在服务器PC上有一个jar文件(PC充当服务器), a.jar ,它将运行无限循环来监听来自客户端PC的 TCPIP连接。 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 . 连接完成后, 客户端PC可以将zip文件发送到服务器PCa.jar将解压缩并执行带有该zip的脚本,这将启动另一个全屏显示java文件b.jar

a.jar will also trying to run b.jar during bootup, if b.jar already exist. 如果b.jar已经存在, a.jar还会尝试在启动期间运行b.jar


Approach 1: 方法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 ). 我在/etc/init.d/中编写了一个脚本来启动a.jar ,因为它是无限循环,系统在启动期间永远挂起( 在终端屏幕中,甚至无法进入GUI )。 I have to boot into single user mode to remove that script in order to boot up normally. 我必须启动进入单用户模式才能删除该脚本才能正常启动。


Approach 2: 方法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. 当我更改脚本以运行a.jar作为守护进程时,它运行正常,监听连接,我可以连接到服务器pc并发送zip文件。

Problem: 问题:

  1. b.jar is not executed even when execution permission and files are all correctly set. 即使执行权限和文件都已正确设置,也不会执行b.jar
  2. After I sent a zip file, b.jar is executed ( because it is connected to a device and that device is activated ). 发送zip文件后,执行b.jar因为它连接到设备并且该设备已激活 )。 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 . 它可能看起来有点复杂,但是因为b.jar会不断更新,并且在更新后必须自动启动,因此我使用a.jar解压缩并执行新版本的b.jar

And b.jar is required to run as root as it needs to access serial port. 并且b.jar需要以root身份运行,因为它需要访问串行端口。

( Server pc is required to be automated, thus it is required to start b.jar during startup and remotely controlled ) 服务器pc需要自动化,因此需要在启动和远程控制期间启动b.jar


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 : 我认为如果你想将应用程序作为守护进程运行,最好的解决方案是使用Apache Jsvc

Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily. Jsvc是一组库和应用程序,用于使Java应用程序更容易在UNIX上运行。 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. Jsvc允许应用程序(例如Tomcat)以root身份执行某些特权操作(例如绑定到端口<1024),然后将身份切换到非特权用户。 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. 它可以通过Cygwin仿真层在Win32上运行,但是Win32用户可能更喜欢使用procrun,这允许应用程序作为Windows服务运行。

Approach 1 should work but you must run the java process in the background by adding & to the command. 方法1应该工作,但您必须通过添加&命令在后台运行java进程。

But there are better solutions to run java apps as a deamon. 但是有更好的解决方案来运行java应用程序作为deamon。 The above can be considered as a quick fix. 以上可以视为快速修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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