python-3.xpython-daemon

How can i run a python file as service python3


I have a python script and that I have to run as a service/background process, I have tried python-daemon but it gives me errors is there any good way to run it as a service so that the script can be run in the background to accept messages and process files?


Solution

  • I have made a sytemctl service file in /lib/systemed/system The file content is

    [Unit]
    Description=RPC SERVER
    After=network.target
    
    [Service]
    User=ubuntu
    WorkingDirectory=/home/ubuntu/
    Type=simple
    ExecStart=/usr/bin/ python3 /home/ubuntu/rpc.py
    
    
    [Install]
    WantedBy=multi-user.target
    

    /home/ubuntu/rpc.py is my python script path and i saved the file as rpcservertest.service so i can start the service using sudo systemctl start rpcservertest.service to stop sudo systemctl stop rpcservertest.service and to get status sudo systemctl status rpcservertest.service