3

I'm trying to create a batch script or scheduled task that can run this:

pythonw manage.py runserver >nul

This will run if I wrote it from a command prompt, and return me the prompt, but if I try to run it using a batch file or an scheduled task it will open a window and lock itself in that command. The window I will close adding a final "exit" but it will be locked in the first command unless I kill the process in the task manager.

How can I do this? if it works in the normal cmd why don't work the same way when in a .bat or .cmd file?

3
  • 1
    I don't know python, but pythonw is a GUI program therefore has no access to the console that starts it. Therefore the >nul is meaningless. However your specific problem is something else - in a batch CMD waits for GUI programs to exit (interactively CMD only waits for console programs). See start /? for help on this (it explains starting programs with and without using start - new behaviour is from Windows 2000 compared with Windows NT4). You probably should be using python, however see start /w if you want to use the GUI python in a console it's not designed for. Commented May 31, 2016 at 11:51
  • The part with >nul is because manage.py is a django script that is not meant to run in this way and uses the stdout in every action you perform with the webserver it creates. This is a workaround to make the Django dev server start on startup w/o console and no log just to hardcore testing. Commented May 31, 2016 at 11:55
  • 1
    I meant start without the /w. Commented May 31, 2016 at 11:57

1 Answer 1

2

Ok, after testing more and more I found something that works. Sorry for making the Question without searching and testing more.

This is what has worked:

CMD /c start "" /B path\to\pythonw.exe path\to\manage.py runserver > nul ^& exit

Inside a .cmd file. Executing the file starts the Django dev webserver properly and a scheduled task that runs that .cmd file also start the server.

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

2 Comments

What happens if you just use start "" /B path\to\pythonw.exe path\to\manage.py runserver > nul? The /B is required to force cmd.exe to set the stdout handle, but why would you need cmd /c ... ^& exit? pythonw.exe doesn't use the console, so there shouldn't be a problem; the batch should just continue and exit. start "" /B pythonw.exe -c "import time;time.sleep(60)" >nul works fine for me. And if I attach a debugger I can see that @$peb->ProcessParameters->StandardOutput is the nul device, while StandardInput and StandardError are unset as NULL.
Well, I don't really know why I need that, but what I know is that without it when I launched this it would open a command window and not close it unless I do it by hand. Also, de problem is because the way manage.py works

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.