AnjuT
AnjuT

Reputation: 176

Run a python script as a background service in linux

I am currently using linux. I have a python script which I want to run as a background service such as the script should start to run when I start my machine. Currently I am using python 2.7 and the command 'python myscripy.py' to run the script.

Can anyone give an idea about how to do this.

Thank you.

Upvotes: 0

Views: 1455

Answers (2)

kangfend
kangfend

Reputation: 365

You can create init script in /etc/init/ directory Example:



    start on runlevel [2345]
    stop on runlevel [!2345]
    kill timeout 5
    respawn

    script
       exec /usr/bin/python /path/to/script.py
    end script


Save with .conf extension

Upvotes: 0

JawguyChooser
JawguyChooser

Reputation: 1936

It depends on where in the startup process you want your script to run. If you want your script to start up during the init process, then you can incorporate it into the init scripts in /etc/init.d/ The details will depend on what init system your system is running. You might be on a system V init (https://en.wikipedia.org/wiki/Init) or on systemd (https://wiki.debian.org/systemd), or possibly some other one. If you don't need your script to run at the system level, then you could kick the script off when you log in. To do that, you'd put it in ~/.profile if you log in using a terminal. Or, if you use a desktop environment, then you're going to be doing something in ~/.local/XSession (if I recall correctly). Different desktop environments are going to have different ways to specify what happens when a user logs in.

Hope this helps! Maybe clarify your needs if you want more detail.

Upvotes: 2

Related Questions