Running a background script in Linux is fairly easy. Just create a cron job and it will be executed at the given time in the background. On Windows however running background scripts in most cases a terminal window will pop-up, even if it’s just for a second while the script is running. As this is quite annoying and will result in lost focus within applications (especially on fullscreen applications like games) I was in search for an alternative.
As far as I can tell there are 2 viable options here. Using wscript
to run programs in the background or just using pythonw
from Python. I moved from wscript to the Python way, as this just feels more robust and I’m using Python for a few things anyway, no need to use shell on Windows or batch files.
wscript
This approach needs a .vbs file containing a few lines. In line 3 the script to execute is entered. Be aware that passing arguments to a script may have a different syntax. Save this file as .vbs
.
Dim WinScriptHost Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "C:\my\cool\script.sh" & Chr(34), 0 Set WinScriptHost = Nothing
In the task scheduler in Windows use the “run program” action and enter wscript
in the input field of the program to run. The argument field receives the full qualified path + filename to your .vbs script. The wscript wrapper should execute the script without popping up a terminal window.
Python
In python the approach is far more easy. Just create a Python script and save it. In the task scheduler in Windows use the “run program” action again, but enter the full qualified path to the pythonw
executable (mind the w
– within the Python installation there is python
and pythonw
) and as an argument enter the full qualified location of your Python script to run. That’s all. In the background the Python script should run withouth Windows creating a pop-up.
flupe
Thank you so much for this trick of using vbscript! I’m been using syncthing launched with windows task manager on log in for a year, and i’ve always been bothered by the leftover terminal window. This fixed it.