Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active June 3, 2022 03:27
Show Gist options
  • Save gilangvperdana/ea45401a05c3d15bcf56c792612c0f96 to your computer and use it in GitHub Desktop.
Save gilangvperdana/ea45401a05c3d15bcf56c792612c0f96 to your computer and use it in GitHub Desktop.
Run Script to SystemD

Run Script to SystemD

You can run your script to SystemD on Linux

Configuration

  • Make script for run your apps (for example run Flask) For example my apps directory on /root/Flask/
touch run.sh
chmod +x run.sh
nano /root/run.sh
#!/bin/bash

while :
do
  sleep 1
  sh -c "python3 /root/Flask/app.py"
done
  • Create SystemD Service
sudo nano /lib/systemd/system/flask.service 
[Unit]
Description=My Shell Script

[Service]
ExecStart=/root/run.sh

[Install]
WantedBy=multi-user.target
  • Apply & Restart SystemD Service
sudo systemctl daemon-reload
sudo systemctl enable flask.service 
sudo systemctl start flask.service 
sudo systemctl status flask.service 

Add

You can configure your SystemD to your needs.

  • For example you want to auto restart on Failure State, you can add this :
sudo nano /lib/systemd/system/flask.service 
[Service]
Restart=on-failure
RestartSec=5s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment