1

Here is the python code, how can I start the application as a daemon using only python 1.py command?

import eventlet
from eventlet import wsgi



def hello_world(env, start_response):
    if env['PATH_INFO'] != '/':
        start_response('404 Not Found', [('Content-Type', 'text/plain')])
        return ['Not Found\r\n']
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello, World!\r\n']

wsgi.server(eventlet.listen(('', 8090)), hello_world)
4
  • This isn't so much a python problem, your OS is in charge of starting processes. Which OS are you using? Commented Nov 6, 2017 at 14:30
  • Centos,But i dont want this on startup of machine.when i run this application it has to start as background process. Commented Nov 6, 2017 at 14:33
  • 2
    Would a cronjob suit your needs? Info on it can be seen here. I know from personal experience running a python script in a cron job can be a pain. One approach to this is to create a bash script that calls your python script stating full paths to the file. Commented Nov 6, 2017 at 14:35
  • when i import daemon runner in python application will run the application as daemon ,but when i try with wsgi server it i not happening. Commented Nov 6, 2017 at 14:56

2 Answers 2

1

Supervisor is a great utility for managing long-running background processes.

Install supervisor, create a configuration file that specifies the command you want to run, user who should run it, log locations, etc.

Then you can start up the service with sudo supervisorctl start {{ name }} and stop it as needed with a similar command.

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

Comments

1

There are a number of different ways to start a process in CentOS, chose yourself which one solves the problem in the way you prefer.

  1. Open a terminal, run python 1.py and forget about it until you shut the machine down
  2. Open a terminal, run nohup python 1.py & and close the terminal. Nohup keeps the program alive even though the session ended. If you need to kill it, you need to find it in the process list with ps -ax and kill it with kill <process_id>
  3. Read the documentation on the deamon function and call your script with it
  4. I am sure there are many more

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.