0

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.

2
  • 1
    please be more specific, CentOS or Ubuntu or Debian, which version? Commented Feb 28, 2017 at 4:36
  • Probably better asked over at ServerFault since it's not a programming question. Commented Feb 28, 2017 at 4:54

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.