1

I have a python script that needs to be run from cmd, but should also stay running once the cmd window has been closed. I've tried various combinations of START and CALL but I've hit a mental blank (and google isn't helping at all).

Here's what I have:

@echo off
start /b python server.py
start python client-sendmessage.py
exit

server.py is a singleton, and should continue running after exit is reached. client-sendmessage.py should halt the script until it returns.

Ryan Cole
  • 100
  • 1
  • 10
  • Changing the script to `.pyw` doesn't create a console window. And check http://stackoverflow.com/q/21031171/236871 – KurzedMetal Jul 22 '15 at 12:58
  • or http://stackoverflow.com/questions/3382082/whats-the-nohup-on-windows – Igor Jul 22 '15 at 14:15
  • Continue to use `start /b` and have server.py run `import ctypes; if ctypes.windll.kernel32.GetConsoleWindow(): ctypes.windll.kernel32.FreeConsole()`. server.py will continue to run until the user's session ends. If you need it to continue running after the user logs off, then implement server.py as a service that runs in session 0. – Eryk Sun Jul 22 '15 at 20:52
  • thank you @eryksun, that worked a charm! for what it's worth, freeing the console will cause `print()` to throw a `OSError: [Errno 9] Bad file descriptor` error. i literally spent 2 hours debugging before i finally commented my print statements out.. – Ryan Cole Jul 23 '15 at 01:07
  • Yes, if the process detaches from the console the existing stdin, stdout, and stderr file descriptors (i.e. descriptors 0, 1, 2) are invalid because the OS handles for `CONIN$` and `CONOUT$` are no longer valid. You can rebind `sys.stdout` and `sys.stderr` to log files, but it would be better to just use the [logging](https://docs.python.org/2/library/logging.html) module. You can also use a command line `--debug` argument to have the server remain attached to the console for printing debug messages. – Eryk Sun Jul 23 '15 at 01:56
  • thanks for the information. this is my first python project and your wisdom has been invaluable. i ended up using the logging module which saved me plenty of hair follicles.. [here's](https://github.com/MGinshe/ftp-push-changes.py) my project if you're interested, it's a batch ftp uploader that only pushes files that have been modified. – Ryan Cole Jul 23 '15 at 06:18

0 Answers0