Browse By

Start Django automatically after restarting development system/server in Linux

Start Django automatically after restarting system/server in Linux

Are you tired of writing script manually whenever you restart your systems to start Django? :-(

Don’t worry about it. Today we are going to learn how to start Django automatically after restarting development or local systems/server in Linux.

Please don’t use this script on the live server, this script is for only development or local system.

Start Django automatically after restarting development or local system/server in Linux

  1. First open gedit in your Linux system.
    Run the following command in terminal.$gedit
  2. Now we create script for start Django server In gedit
#!/bin/bash
clear
cd /path/to your/virtual environment  #path to your virtual environment
. bin/activate  #Activate your virtual environment
cd /path/to your/project directory  #After that go to your project directory
python manage.py runserver  #run django server

After that save this file with .sh extension. I have saved this file as script.sh in the following example.

Start Django automatically after restarting system/server in Linux

Script for start Django server In gedit script.sh

3.  Run following command in terminal for giving execution permission to your script
$ chmod u+x script.sh

4. To test your script works perfectly, add following command in terminal
$ ./ script.sh

5. After that, you have to create cron job on your machine for run script automatically on reboot give following command in terminal
$ crontab -e

Above command will open crontab window, then add the following line in it:

@reboot path/to/script.sh

Start Django automatically after restarting system/server in Linux

Crontab Window

6.  After pressing crtl+x, it will ask you for saving changes

Start Django automatically after restarting system/server in Linux
7.  Now, press Y to save your changes

Bravo!!! We have done it. Whenever you restart your machine, Django will run automatically. 😀
I hope you like this article. Share your views to to improve content. Happy Learning !!!

Subscribe for more articles !!

 

  • 廖家輝

    Thanks,that is simply and useful .

  • http://google.com/+Nkansahrexford Nkansah Rexford

    That is short and sweet. Thank you

  • Hubert Kanyamahanga

    I have been struggling to find how to start a django server automatically!

    Thank u for this guide!