简体   繁体   English

无法通过 systemd 使用 userdata 启动应用程序作为服务

[英]Failing to start an app as service through systemd using userdata

I want to start my app as service from systemd but the app is not starting.我想从 systemd 将我的应用程序作为服务启动,但该应用程序没有启动。

My unit file appstart.service looks like this:我的单元文件 appstart.service 如下所示:

[Unit]
Description=Application start

[Service]
Type=simple
User=ec2-user
ExecStart=/usr/bin/bash /home/ec2-user/project/restartScript.sh
SyslogIdentifier=App_start

[Install]
WantedBy=multi-user.target

RestartScript.sh should start the java app: RestartScript.sh 应该启动 java 应用程序:

#!/bin/bash
export SPRING_PROFILES_ACTIVE="tst,development"
cd /home/ec2-user/project
pkill java
/usr/bin/java -jar /home/ec2-user/project/app.jar >>/home/ec2-user/project/web.log 2>>/home/ec2-user/project/web-error.log &

I am starting the app as a service this way using User Data on AWS EC2 instance:我正在使用 AWS EC2 实例上的用户数据以这种方式将应用程序作为服务启动:

#!/bin/bash
mkdir /home/ec2-user/project
cd /home/ec2-user/project
sudo wget -P /home/ec2-user/project/ https://tst.s3.eu-west-1.amazonaws.com/app.jar
chown -R ec2-user:ec2-user /home/ec2-user/project
sudo wget -P /home/ec2-user/project/ https://tst.s3.eu-west-1.amazonaws.com/restartScript.sh
sudo chmod 755 /home/ec2-user/project/restartScript.sh
cd /etc/systemd/system/
sudo wget /etc/systemd/system/ https://tst.s3.eu-west-1.amazonaws.com/appstart.service
sudo su 
systemctl daemon-reload
systemctl enable appstart.service
systemctl start appstart.service
exit

The output I am getting when I start the EC2 instance this way is:当我以这种方式启动 EC2 实例时,我得到的 output 是:

$ systemctl status appstart.service
● appstart.service - Application start
   Loaded: loaded (/etc/systemd/system/appstart.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Thu 2022-08-25 13:35:52 UTC; 4min 19s ago
  Process: 7328 ExecStart=/usr/bin/bash /home/ec2-user/project/restartScript.sh (code=exited, status=0/SUCCESS)
 Main PID: 7328 (code=exited, status=0/SUCCESS)

Aug 25 13:35:52 ip-x-x-x-x.tst.local systemd[1]: Started Application start.

When I try to do systemctl start appstart.service Nothing changes.当我尝试做 systemctl start appstart.service 没有任何变化。 The application is not working.应用程序不工作。

Any idea why is this happening?知道为什么会这样吗?

OS on the machine:机器上的操作系统:

$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"

Nothing seems to be wrong with this, the service is running and the application runs and finishes successfully.这似乎没有什么问题,服务正在运行,应用程序运行并成功完成。 The Active state of the service becomes inactive (dead) in case of failure it should be Failed and Main PID is (code=exited, status=0/SUCCESS) .服务的Active state 在发生故障时变为inactive (dead) ,它应该是Failed并且Main PID(code=exited, status=0/SUCCESS)

To verify that the service is running correct, write this line somewhere in RestartScript.sh :要验证服务是否正确运行,请在RestartScript.sh的某处写下这一行:

echo "Test" > test.txt

After starting the service you will find the created file near RestartScript.sh file.启动服务后,您会在RestartScript.sh文件附近找到创建的文件。

I managed to resolve the issue by changing the WantedBy section in appstart.service file to default.target.我设法通过将 appstart.service 文件中的 WantedBy 部分更改为 default.target 来解决该问题。

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

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