Setting Up a Systemd Service for Your Application
This guide explains how to create a startup script and configure a systemd service to ensure your application starts automatically when the server boots.
1. Prepare the Startup Script (start.sh
)
1.1 Create the start.sh
Script
- Log in to your server.
- Navigate to the directory where you want to create the script (e.g.,
/opt/o11
):cd /opt/o11
- Create and edit the
start.sh
file:sudo nano start.sh
- Add the following content to
start.sh
(adjust the paths and options based on your application’s requirements):#!/bin/bash # Command to start your application # Replace this with your application's specific command /home/o11/start -p 9200 -allow 127.0.0.1
- Save and close the editor (Ctrl+X, then Y, then Enter in nano).
- Make the script executable:
sudo chmod +x start.sh
2. Create the Systemd Service File
2.1 Create the Service File
- Open a text editor to create the service file:
sudo nano /etc/systemd/system/o11.service
- Add the following content to the file:
[Unit] Description=o11 Service After=network.target [Service] Type=simple ExecStart=/opt/o11/start.sh WorkingDirectory=/opt/o11 Restart=always RestartSec=3 [Install] WantedBy=multi-user.target
- Save and close the editor (Ctrl+X, then Y, then Enter in nano).
2.2 Reload Systemd Configurations
Reload the systemd configuration to recognize the new service:
sudo systemctl daemon-reload
2.3 Enable and Start the Service
- Enable the service to start automatically at boot:
sudo systemctl enable o11.service
- Start the service immediately:
sudo systemctl start o11.service
2.4 Check the Service Status
Verify that the service is running:
sudo systemctl status o11.service
If there are errors, check the logs:
journalctl -u o11.service
3. Restart the Server
- Restart the server to verify that the service starts automatically:
sudo reboot
- After the reboot, check the service status:
sudo systemctl status o11.service
This setup ensures your application is configured to start automatically using systemd.