0

I want to start my app as service from systemd but the app is not starting.

My unit file appstart.service looks like this:

[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:

#!/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:

#!/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:

$ 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. 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"
Alex
  • 97
  • 8
  • Can you successfully run `RestartScript.sh` script? if yes What is it's output? – Ali-Ibrahim Aug 26 '22 at 09:47
  • When I start the script manually, it starts the app. So, manually, the script runs but through appstart.service it does not. – Alex Aug 26 '22 at 10:21
  • Nothing seems to be wrong, even the `status` shows success on execution `Main PID: 7328 (code=exited, status=0/SUCCESS)` Can you share the `java` code you are trying to run? – Ali-Ibrahim Aug 26 '22 at 11:14

2 Answers2

0

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).

To verify that the service is running correct, write this line somewhere in RestartScript.sh:

echo "Test" > test.txt

After starting the service you will find the created file near RestartScript.sh file.

Ali-Ibrahim
  • 835
  • 1
  • 6
  • 16
  • I have added echo "Test" > test.txt into the script but I don't have test.txt file in /home/ec2-user/project folder. It seems like it is not running the script at all. Any idea why? – Alex Aug 31 '22 at 11:20
  • That's weird!!! Can you remove everything else in the script file and just leave the `echo` command? And let me know if it creats the file. – Ali-Ibrahim Aug 31 '22 at 12:44
  • I have one last thing to try, remove `cd` from you script, and in your `[Service]` section add `WorkingDirectory=/home/ec2-user/project` Can you try it now? – Ali-Ibrahim Aug 31 '22 at 19:06
  • I tried that as well and now I get test.txt file but the app is not started. When I start it manually with the exact same commands as are in RestartScript.sh, it starts without issues. – Alex Sep 06 '22 at 09:55
0

I managed to resolve the issue by changing the WantedBy section in appstart.service file to default.target.

Alex
  • 97
  • 8