sudo nano /etc/systemd/system/my-service.service
NOTE: And add the following content from section Service File Examples
# Start the service
sudo systemctl start my-service
# Stop the service
sudo systemctl stop my-service
# Restart the service
sudo systemctl restart my-service
# Check the status of the service
sudo systemctl status my-service
# Enable the service to start on boot
sudo systemctl enable my-service
# Disable the service to start on boot
sudo systemctl disable my-service
# Reload the service
# Needed after making changes to the service file)
sudo systemctl daemon-reload
# View the logs
sudo journalctl -u my-service
# View the logs in real-time
sudo journalctl -u my-service -f
# View the logs in real-time with a specific number of lines
sudo journalctl -u my-service -n 100 -f
# View the logs with a specific time range
sudo journalctl -u my-service --since today
sudo journalctl -u my-service --since "2024-02-16 18:17:16" --until "2024-02-16 18:17:18"
# View the logs in a specific format
# Verbosity levels: short, short-precise, short-monotonic, verbose, export, json, json-pretty, cat
sudo journalctl -u my-service --output json # or --output=json-pretty
# View the logs with a specific priority level (1 to 7)
# 1: Alert, 2: Critical, 3: Error, 4: Warning, 5: Notice, 6: Informational, 7: Debug
sudo journalctl -u my-service -p err -b # or -p 3
# View the logs with specific fields
# Verbosity levels: short, short-precise, short-monotonic, verbose, export, json, json-pretty, cat
sudo journalctl -u my-service --fields MESSAGE,MESSAGE_ID,SYSTEMD_UNIT
[Unit]
Description="My App - API"
After=syslog.target network.target
[Service]
User=myapp
Group=myapp
Environment="JAVA_HOME=/path/to/java/home"
ExecStart=${JAVA_HOME}/bin/java -jar /var/myapp/myapp.jar
ExecStop=/bin/kill -15 $MAINPID
SuccessExitStatus=143
[Unit]
Description="My App - API"
After=syslog.target network.target
[Service]
User=diego
Group=diego
Environment="APP_HOME=/home/diego/my-app-api"
Environment="LOG_HOME=/home/diego/my-app-api/logs"
Environment="JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre"
ExecStart=/bin/sh -c '${JAVA_HOME}/bin/java -jar ${APP_HOME}/myappapi.war > ${LOG_HOME}/out.log 2> ${LOG_HOME}/error.log'
# ExecStart=/bin/sh -c '${JAVA_HOME}/bin/java -jar ${APP_HOME}/${APP_NAME}.war > ${APP_HOME}/logs/${APP_NAME}.log 2>&1'
ExecStop=${APP_HOME}/scripts/stop-myapp.sh $MAINPID
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
NOTE: View stop command in file stop-myapp.sh
# Log to journal
StandardOutput=journal
StandardError=journal+console
# Log to file
# Logs files need to be created before
ExecStartPre=/bin/mkdir -p /var/log/myapp
ExecStartPre=/bin/touch /var/log/myapp/myapp.log
ExecStartPre=/bin/chown myuser:mygroup /var/log/myapp/myapp.log
StandardOutput=append:/var/log/myapp/myapp.log
StandardError=append:/var/log/myapp/myapp.log
# Java Log4j configuration
Environment="LOG4J_CONFIG=/path/to/log4j-config.xml"
ExecStart=/bin/sh -c '${JAVA_HOME}/bin/java -Dlog4j.configurationFile=${LOG4J_CONFIG} -jar /var/myapp/myapp.jar'