大网数据广告活动
服务器运维问答

How to start a jar file using systemd

· 52CE 我爱测

I want to start java application on system boot using systemd . I tried to do by adding above scripts. But service is not starting. my-startup.service [Unit] Description=Startup [Service] ExecStart=/usr/local/sbin/my-startup.sh […

问题原文

原帖链接 · Janith · CC BY-SA 4.0

I want to start java application on system boot using systemd. I tried to do by adding above scripts. But service is not starting.

my-startup.service

[Unit]
Description=Startup

[Service]
ExecStart=/usr/local/sbin/my-startup.sh

[Install]
WantedBy=multi-user.target

my-startup.sh

cd /var/www/test.com
nohup java -jar *.jar>test.out 2>test.err &

But application not starting when server starting.

● my-startup.service - Startup
   Loaded: loaded (/etc/systemd/system/my-startup.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2018-12-12 16:22:52 +0530; 27s ago
  Process: 650 ExecStart=/usr/local/sbin/my-startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 650 (code=exited, status=0/SUCCESS)

Dec 12 16:22:52 localhost.localdomain systemd[1]: Started Startup.
Dec 12 16:22:52 localhost.localdomain systemd[1]: Starting Startup...

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

Using systemd directives in place of shell results in a more consistent environment. This includes not relying on $PATH or the working directory by providing full paths where they are known.

Neither & for job control, nor managing SIGHUP is needed. systemd service units are already backgrounded.

Assuming the program doesn't fork, in other words the java exec keeps running, that's Type=simple

[Unit]
Description=something

[Service]
ExecStart=/usr/bin/java -jar /var/www/test.com/something.jar
StandardOutput=file:/var/log/something.out.txt
StandardError=file:/var/log/something.err.txt
Type=simple
WorkingDirectory=/var/www/test.com

[Install]
WantedBy=multi-user.target

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。