Launch a Python script as a service

Almost [Getting a Python script to run in the background (as a service) on boot](http://blog.scphillips.com/2013/07/getting-a-python-script-to-run-in-the -remains background-as-a-service-on-boot /).

Creating a python script

For example, suppose you started /usr/bin/local/test/test.py as a service here.

Creating a Daemon Script

Model

Create the following script as myservice.sh in / usr / local / bin / test.

myservice.sh


#!/bin/sh

### BEGIN INIT INFO
# Provides:          myservice
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Put a short description of the service here
# Description:       Put a long description of the service here
### END INIT INFO

# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin/myservice
DAEMON=$DIR/myservice.py
DAEMON_NAME=myservice

# Add any command line options for your daemon here
DAEMON_OPTS=""

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
    log_daemon_msg "Starting system $DAEMON_NAME daemon"
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
    log_end_msg $?
}
do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME daemon"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    log_end_msg $?
}

case "$1" in

    start|stop)
        do_${1}
        ;;

    restart|reload|force-reload)
        do_stop
        do_start
        ;;

    status)
        status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
        ;;
    *)
        echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
        exit 1
        ;;

esac
exit 0

Script editing

Edit the following contents according to the save location of the python script.

DIR=/usr/local/bin/myservice
DAEMON=$DIR/myservice.py
DAEMON_NAME=myservice

For example, if you want to daemonize /usr/local/bin/test/test.py, edit it as follows:

DIR=/usr/local/bin/test
DAEMON=$DIR/test.py
DAEMON_NAME=test

Copy to /etc/init.d

After changing permissions etc., copy to /etc/init.d

cd /usr/local/bin/test
sudo chown root.root myservice.sh
sudo chmod 755 myservice.sh
sudo cp myservice.sh /etc/init.d/

Change permissions for Python scripts

Change the permissions along with the python script

cd /usr/local/bin/test
sudo chmod 755 test.py

Registration to Service

Checking the operation of the daemon script

Check the operation with the following command

sudo /etc/init.d/myservice.sh start
sudo /etc/init.d/myservice.sh status
sudo /etc/init.d/myservice.sh stop

Register for service

Register to the service with the following command

sudo update-rc.d myservice.sh defaults

You can check if you have registered for the service with the following command.

ls -l /etc/rc?.d/*myservice.sh

Recommended Posts

Launch a Python script as a service
Use pymol as a python library
Python service
Use blender as a python module
python launch
Write a batch script with Python3.5 ~
Install Python as a Framework with pyenv
Run the Python interpreter in a script
[Python] Sorting collection types as a reference
Launch a Flask app in Python Anywhere
Python as a strongly, dynamically typed language
Use Django from a local Python script
How to run a Maya Python script
Script python file
python script skeleton
Python script profiling
Import python script
Launch a simple password-protected search service in 5 minutes
python memo: Treat lists as a set type
"Python Kit" that calls a Python script from Swift
Launch a web server with Python and Flask
Call a Python script from Embedded Python in C ++ / C ++
Run a python script from excel (using xlwings)
How to manage arguments when implementing a Python script as a command line tool
[Grasshopper] When creating a data tree on Python script
A * algorithm (Python edition)
A python script that imports a dated csv file into BigQuery as a time partition table
[Python] Take a screenshot
(Python) Treat integer values as a set of flags
Specify a subcommand as a command line argument in Python
A python lambda expression ...
A memorandum to run a python script in a bat file
Daemonize a Python process
Launch a Python web application with Nginx + Gunicorn with Docker
What's in that variable (when running a Python script)
DynamoDB Script Memo (Python)
Create a Python environment
Python script to create a JSON file from a CSV file
Python3> round (a --b, 7)
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
I replaced the Windows PowerShell cookbook with a python script.
Displaying DICOM images in rudimentary Python as a medical professional
A Python script that saves a clipboard (GTK) image to a file.
How to run a Python program from within a shell script
[Python, shell script, team development] Create a nifty Git repository
How to launch AWS Batch from a python client app
Let's create a script that registers with Ideone.com in Python.
Creating a Python script that supports the e-Stat API (ver.2)
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
[Python] Use JSON format data as a dictionary type object
A set of script files that do wordcloud in Python3
A python script that converts Oracle Database data to csv
A Python script that compares the contents of two directories
AtCoder ABC 177 Python (A ~ E)
Take a screenshot in Python
Create a Wox plugin (Python)
Create a function in Python
Create a dictionary in Python
AtCoder ABC 178 Python (A ~ E)
A road to intermediate Python