How to create SystemCTL Service on Amazon EC2 to run application on start

Category: Engineering Insights :: Published at: 06.05.2024

There are two simple ways to run application on Amazon EC2.

One is to run screen command and run application there as a process.

Second one, probably better, is to create SystemCTL Service which you can run later.

This is how to do it:

So, let's assume we have AWS EC2 Instance, i will use Amazon Linux 2.

These are the steps to create and run service:

  • Go to SystemCTL directory:
cd /etc/systemd/system
  • Create new service - we will call it backend as we're trying to run Spring Boot Backend App
sudo touch backend.service
  • Open file in Vim / Nano etc
sudo vim backend.service
  • This is the content of the service:
[Unit]
Description=Describe what this service is doing

[Service]
ExecStart=/usr/bin/java -jar your-app-0.0.1-SNAPSHOT.jar # This is the command which you're using to run app
Restart=always # This means, service will always try to be online
WorkingDirectory=/home/ec2-user # Directory from where you're running command

# Stardard Output logs
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=backend

[Install]
WantedBy=multi-user.target # This defines when the service should be starting
  • After this you need to restart daemon and run service
sudo systemctl daemon-reload

sudo systemctl enable backend.service
sudo systemctl start backend.service
sudo systemctl status backend.service

- Click if you liked this article

Views: 589