I have developed a Django website and hosted the application in an Amazon EC2 instance.
AMI name: ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20231207
Instance type: t2.micro
I run the project following these steps:
- SSH into the EC2 instance from my terminal.
- I locate into the proper django project folder.
- I run this command: python3 manage.py runserver 0.0.0.0:8000
Now, following these steps, I’m able to run the project and it works fine. But when I close the cmd opened in my local PC that I used to SSH into the EC2 instance and to run the application, then the application doesn’t work anymore.
My aim would be to simply run the django project from my cmd once (of course after having SSH to the EC2) and then close my laptop and having the application still alive.
Do you know how can I do this?
3
Answers
I guess that’s because when closing the ssh connection, you stop the program because it’s run in foreground.
With the following command it should work:
PS: you should not start the Django application with a development server, see the deployment page of the Django documentation.
edit you could also create a systemd service like in this answer.
You can use nohup for running it even after you terminate processes on your terminal/cmd.
You can also log the output of above command by writing it to a file for debugging purpose.
Note: Not recommended for production environment.
Please use Nohup
It ignores the hangup signal, allowing the process to continue running after the terminal is closed.
Steps :-
nohup python3 manage.py runserver 0.0.0.0:8000 &
Run: nohup python3 manage.py runserver 0.0.0.0:8000 &.
The & at the end puts the process in the background.
Exit the SSH session; the process will continue to run.