Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Created February 18, 2024 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegofcornejo/75ab34c8c4cabde13a10d51ee1715067 to your computer and use it in GitHub Desktop.
Save diegofcornejo/75ab34c8c4cabde13a10d51ee1715067 to your computer and use it in GitHub Desktop.
SYSTEMD - Services files examples
# This script is used to stop a process gracefully, and if it does not exit in a given time, it sends a SIGKILL to it.
#!/bin/bash
PID=$1
TIMEOUT=30 # Timeout in seconds
if [ -z "$PID" ]; then
echo "Usage: $0 <pid>"
exit 1
fi
kill -15 $PID # Send SIGTERM
for (( i=0; i<$TIMEOUT; i++ )); do
if ! kill -0 $PID 2>/dev/null; then
echo "Process $PID has exited gracefully."
exit 0
fi
sleep 1
done
echo "Process $PID did not exit in $TIMEOUT seconds, sending SIGKILL."
kill -9 $PID

Systemd Service Examples

Add a new service to systemd

sudo nano /etc/systemd/system/my-service.service

NOTE: And add the following content from section Service File Examples

Some useful commands

# 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

Using journalctl

# 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

Service File Examples

Simple Service

[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

Full Service with Environment Variable, custom log file and custom stop command

[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

Some useful logs definitions

# 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment