1

I use python 2.7 and Django 1.11.5.

I want to check that my process manage.py, running in screen. If check is false I want to start process manage.py.

I use three files: check.py, start_screen.sh, start_server.sh.

I check the process in the first file(check.py):

import subprocess
from settings import BASE_DIR, SERVER

process_list = subprocess.check_output(['/bin/ps', 'ax'])
my_server = re.search('SCREEN .* server', process_list)
if my_server is None:
    s = subprocess.check_output(['/bin/bash', BASE_DIR + '/start_screen.sh'])

I start the screen in the second file(start_screen.sh) :

#!/usr/bin/env bash
screen -dmS server /bin/bash /home/user/Desktop/python_server/start_server.sh

I start the server in the third file(start_server.sh):

#!/bin/bash
cd /home/user/Desktop/python_server/
/usr/bin/python manage.py server 0.0.0.0:8000

I want to reduce the number of files to one python file.

Also I want to pass parameters like SERVER.PORT, SERVER.ADDRESS and BASE_DIR to the request.

Can anybody help with this?

6
  • Can you please explain what you want to obtain? From what I see I suspect that this is a script to start / stop Django but it doesn't seem something that you would like using for production. Commented Aug 13, 2019 at 12:49
  • I want to restart process in screen when(if) it falls. If you know other way to do this, it may be interesting for me. I want to make current code better and more pythonic. Commented Aug 13, 2019 at 13:38
  • @ДмитрийСиденко can't you use something like if ! pgrep -f "screen.*start_server.sh"; then screen -dmS server /bin/bash /home/user/Desktop/python_shome/user/Desktop/python_server/start_server.sh; fi? This way you don't have to invoke python script. Commented Aug 13, 2019 at 14:14
  • I have a variables like SERVER.PORT, SERVER.ADDRESS and BASE_DIR. I can get it only from my python script, so the best way is to call it from python as far as I see. In addition, I need to run multiple server instances and I need to change this variables in this script. But I am open to the opinions of others. Commented Aug 13, 2019 at 14:24
  • 1
    Sure thing... There are countless tutorials on the topic, I just link one here, I'd go - personal preference - with the Gunicorn part: developer.mozilla.org/en-US/docs/Learn/Server-side/Django/… Commented Aug 13, 2019 at 14:31

1 Answer 1

1

I thank the participant Pitto for help in improving the server.

I find the next way to solve the current problem.

import subprocess
from settings import BASE_DIR, PYTHON_DIR, SERVER_PORT

process_list = subprocess.check_output(['/bin/ps', 'ax'])
my_server = re.search('SCREEN .* server', process_list)
if my_server is None:
    s = subprocess.check_output([
        "screen -dmS server && "
        "screen -S server -X -p 0 stuff 'cd {0}/\n' && "
        "screen -S server -X -p 0 stuff '{1} manage.py runserver 0.0.0.0:{2}\n'"
        .format(BASE_DIR, PYTHON_DIR, SERVER_PORT)], shell=True)

If you want to start this screen from root you need to start python from root or make crontab with root. To configure crontab use:

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

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.