在 Linux 环境中启动和停止可执行 JAR 的 Shell 脚本

use*_*567 5 linux command-line shell-script jar

我必须为可执行 jar(例如,executable.jar)编写启动和停止脚本。

如何使用shell脚本来做到这一点?

我使用.bat文件成功地为 Windows 环境做到了这一点。但无法设法为其编写一个 shell 脚本。

Has*_*tur 6

这是简单的版本:你可以写一个小脚本如下(保存为MyStart.sh)

#!/bin/bash
java -jar executable.jar &      # You send it in background
MyPID=$!                        # You sign it's PID
echo $MyPID                     # You print to terminal
echo "kill $MyPID" > MyStop.sh  # Write the the command kill pid in MyStop.sh
Run Code Online (Sandbox Code Playgroud)

当您使用 执行此脚本时/bin/bash MyStart.sh,它将在屏幕上打印该进程的 PID。

否则,您可以将属性更改为 MyStart.sh( chmod u+x MyStart.sh) 并使用./MyStart.sh.

要停止该进程,您可以通过命令行编写,kill 1234其中 1234 是脚本回答的 PID,或/bin/bash MyStop.sh

记得在使用后删除脚本 MyStop.sh。