Run a Shell/Python Script as System Service

Objective

There are times when we need to keep Shell/Python Script continuously even user log-off from system. For this purpose, we can run the script as System Service. We can also enable it on system boot so that if system restarts, script will continue again.

Here are some of benefits of running scripts as System Service

  • Automatic Start: The service can be configured to start automatically when the system boots, ensuring that the script runs without manual intervention.
  • Graceful Stop: The service can also be configured to stop gracefully during system shutdown, ensuring proper cleanup and resource management.
  • Failure Detection: System services are often monitored by the system’s service manager (like systemd on Linux), which can detect if the script fails and automatically restart it.
  • Logging: Services usually have integrated logging mechanisms that make it easier to track the script’s activity and troubleshoot issues.

Environment Used

  • Ubuntu (VERSION 22.04.4 LTS) : This is used to store and run the python script.
  • Virtual Environment for Python3 (optional)

Pre-requisites

  • Since we are targeting the script as System Service, root escalation privilege (i.e. sudo) will be required.
  • Python/Shell Script to be run.
  • Python3 virtual Environment(optional) but recommended.

Steps

1. Create System Service for Script

Create a new file in the /etc/systemd/system/ directory with a name ending in .service, for example myscript.service. Use sudo option. Below is sample content for this file.

rakesh@Ubuntu-Lab150-DC1:/etc/systemd/system$ sudo vi fw-grafana-graph.service

rakesh@Ubuntu-Lab150-DC1:/etc/systemd/system$ cat fw-grafana-graph.service 
[Unit]
Description=Python Script to collect FW logs via vmanage API and insert data to Influx-db

[Service]
ExecStart=/home/rakesh/sdwan-integration-with-influxdb-grafana/venv/bin/python /home/rakesh/sdwan-integration-with-influxdb-grafana/code/firewall_statistics_api.py
Restart=always
User=rakesh

[Install]
WantedBy=multi-user.target

Here is breakdown of above sample service file.

  • Description: This provides a brief description of the service. Here, it indicates that the service runs a Python script that collects firewall logs via the vManage API and inserts the data into an InfluxDB database.
[Unit]
Description=Python Script to collect FW logs via vmanage API and insert data to Influx-db
  • ExecStart: Specifies the command to start the Python script. It uses a virtual environment located at /home/rakesh/sdwan-integration-with-influxdb-grafana/venv/bin/python to execute the script /home/rakesh/sdwan-integration-with-influxdb-grafana/code/firewall_statistics_api.py.
  • Restart: Indicates that the service should always restart if it stops or crashes. This helps ensure continuous operation of the script.
  • User: Specifies that the service should run as the user rakesh, providing appropriate user-level permissions and access.
[Service]
ExecStart=/home/rakesh/sdwan-integration-with-influxdb-grafana/venv/bin/python /home/rakesh/sdwan-integration-with-influxdb-grafana/code/firewall_statistics_api.py
Restart=always
User=rakesh
  • WantedBy: Specifies the target under which the service should be installed. multi-user.target is a standard systemd target for setting up a non-graphical multi-user system, meaning the service will start in the multi-user runlevel, which is typical for server environments.
[Install]
WantedBy=multi-user.target

This configuration ensures that the script will start automatically on boot, run continuously, and restart if it crashes, all while operating under the specified user account.

2. Start the service

a. Reload the systemd daemon with the command sudo systemctl daemon-reload

b. Start the service with the command sudo systemctl start fw-grafana-graph.service . This will run the Python script as a background process.

c. To make the service start automatically on system boot(optional), enable the service with the command sudo systemctl enable fw-grafana-graph.service

3. Monitoring

Use below commands for monitoring purpose of this service.

  • Check status sudo systemctl status fw-grafana-graph.service
  • Check logssudo journalctl -u fw-grafana-graph.service

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Share via
Copy link