07 giugno 2013

Using Django runserver in background with Screen

I was poking around today looking for an easier way to use the Django runserver without having to stop and start it in the same terminal window each time I needed to test.

I could just simply start and run the server in a separate terminal session, but I still wanted to check out some alternatives. One interesting method I came across is using the “screen” command in any Unix type OS such as Linux or Mac OS X.

Screen is a window manager program for the terminal that allows you to start, run, and send commands to separate screens running within the same session.

From your terminal session, start a new screen:
screen

Once you see the program information screen, just hit enter or space to move on to the new window. You can then detach the screen and return to your main terminal session using the -d option.
screen -d

You should now back in the main session. To retrieve the screen, use the option -r any time you want to reattach.
screen -r

That’s the basic flow for using screen, so let’s go ahead and start the Django server in your new screen. From your screen session, use the normal command for starting the Django server.
python manage.py runserver

The only problem from here is that we can’t use the command “screen -d” from the command line when the server is actively running. To detach the screen in this situation, simply use the shortcut
CTRL-A-D

This will take you back to your main session while keeping the screen actively running in the background with your Django server. Any time you want to check the status, just repeat the steps above by reattaching with “screen -r”.

When you are done with your work for the day, you can terminate the screen by stopping the Django server with CTRL-C and then using the exit command from the screen window prompt.
exit

That’s it. Screen is a simple tool that can help you manage multiple screens within the same terminal session.

If you work on multiple projects at the same time, you can optionally use the -S option to name your screen. You can then start, list, and reattach multiple screens by name.
screen -S fooScreen

To show a list of the current screens, use:
screen -ls

To reattach to a specific screen:

screen -r fooScreen
If you want to read more about screen and more of it’s capabilities, check out the post Linux 101 Part 1 – How to use screen

From: http://bitsoul.com/2012/07/05/using-django-runserver-in-background-with-screen/

Tondo è bello...

Tondo è bello...