In this tutorial, we will guide you through the process of creating a systemd service to ensure your application automatically starts after the system boots up. This is particularly useful for server setups, ensuring the continuous operation of your application.
👉 Linux: Systemd Service
Step 1: Open and Edit the Systemd Service File
1.1 Open the systemd service file using the following command:
sudo nano /etc/systemd/system/your.service
1.2 Once the file is open, add the following configuration:
[Unit]
Description=Dubly Backend
After=network.target
[Service]
WorkingDirectory=/home/your/path
ExecStart=conda activate dub && gunicorn --bind 0.0.0.0:5000flask_app:app
Restart=always
User=ubuntu
[Install]
WantedBy=multi-user.target
Save and exit the editor.
Step 2: Adjust Permissions for the Service File
Run the following command to set appropriate permissions for the service file:
sudo chmod 644 /etc/systemd/system/your.service
Step 3: Enable and Start the Systemd Service
3.1 Reload systemd to apply the changes:
sudo systemctl daemon-reload
3.2 Enable the newly created service to start on boot:
sudo systemctl enable your.service
3.3 Start the service for the current session:
sudo systemctl start your.service
Step 4: Check Service Status and Logs
To ensure everything is functioning as expected, you can check the status and logs of the systemd service:
sudo systemctl status your.service
Additionally, view the service logs:
journalctl -u your.service
Step 5: Test the Running Service
Verify that your application is accessible and operational:
curl -X POST localhost:5000/api/your-endpoint
Congratulations! You’ve successfully created a systemd service for your application, ensuring it starts automatically after system boot. Adjust the paths and configurations according to your specific setup and requirements.
👉 MacOS: Launchd Service
# Create the launchd service
cd path/to/where/you/want/
nano your.plist
Add
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.youappname</string>
<key>ProgramArguments</key>
<array>
<string>/Users/username/anaconda3/envs/dub/bin/gunicorn</string>
<string>--bind</string>
<string>0.0.0.0:5000</string>
<string>flask_app:app</string>
</array>
<key>WorkingDirectory</key>
<string>/path/to/src</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/path/to/logs/yourapp.log</string>
<key>StandardErrorPath</key>
<string>/path/to/logs/yourapp.error.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>PYTHONPATH</key>
<string>/path/to/bin/python</string>
</dict>
</dict>
</plist>
cd backend
chmod 644 config/your.plist
launchctl load config/your.plist
launchctl start config/your.plist
launchctl list | grep your # Check Service Status
cat logs/yourapp.log
cat logs/yourapp.error.log
launchctl stop config/your.plist
launchctl unload config/your.plist