How to run window-less scheduled task

2

I've got the following command running as scheduled task in Windows 8

cmd /C copy /Y nul B:\keepalive.txt

However, when it runs it opens the command prompt window for fraction of second, and that's enough to make it annoying.

How can I run the same command but without opening the command prompt windows?

TheBlueSky

Posted 2013-09-06T08:48:31.407

Reputation: 217

1

Wrap your code in a VBS script and point your task to this file. VBS scripts can run CMD commands in a hidden window. Read this answer to see how it works. Remember: In your case, you can put the CMD command copy /Y nul B:\keepalive.txt directly into that VBS, and no additional CMD file is needed like I explained over there. Currently at work I can't write a full answer, I hope you get the idea.

– nixda – 2013-09-06T09:42:25.730

@nixda, why is your excellent comment not posted as an answer? – Dave – 2013-09-06T11:07:07.640

@nixda, this doesn't work as advertised :) there as two issues I faced trying to make this work. First one, which isn't a big deal, when I used the command copy /Y nul B:\keepalive.txt in the VBS file it returns an error, something like the script can't file the specified file, so I put the entire cmd /C copy /Y nul B:\keepalive.txt command. The second issue, the scheduled task cannot run VBS files and always displays "How do you want to open this file?" dialog. I'm on Windows 8, as previously said, and double-clicking the file works; i.e. the file is executed. – TheBlueSky – 2013-09-06T11:43:58.153

Answers

2

Wrap your code in a VBS script and point your task to this file

VBscript's run method can execute batch commands in a hidden window, if you set "intWindowStyle" to 0.

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn]) 

Put this code into your HiddenTask.vbs file.

Set wShell = CreateObject ("Wscript.Shell") 
wShell.Run "cmd /c copy /Y nul D:\keepalive.txt", 0

I successfully tested your example. It creates a new empty file just like it should. Of course without displaying any window.


Read this SO question regarding your issue that it's not working when used as scheduled task. Grant your job administrator rights and set the "start in" folder. Also, check the event log for this task.

Additionally I made some screenshots. Maybe you are able to find the difference to your setup.

enter image description here enter image description here enter image description here

IMPORTANT If you're facing an issue after following the instructions here, please read the comments below, the issue may be the file name or path.

nixda

Posted 2013-09-06T08:48:31.407

Reputation: 23 233

Thanks for the detailed answer. This is what I've already done and my issue is with "How do you want to open this file?" dialog displayed instead of the script running. It isn't a permission issue (I'm running it under my user) and the referenced Stackoverflow question doesn't say a thing about the "How do you want to open this file?" – TheBlueSky – 2013-09-08T17:49:23.317

have you tested the "start in" folder setting like mentioned here?.

– nixda – 2013-09-08T17:54:13.950

sorry for the late reply. Yes, I tried the "Start in" and the result is the task doesn't even "see" the script file, it's as if the "Start in" doesn't have any effect. Just to iterate on that, the task obviously reaches the file, however, it cannot run it as if it was double-clicked. – TheBlueSky – 2013-09-13T08:19:50.857

You know what's even more frustrating, all this issue is because the schedule tasks doesn't like spaces in the path as well even if I used quotation mark around it (actually the quotation mark makes the run fail); it also doesn't like anything other than letters and numbers. When I moved my script to a space-less path, it worked. Who's stupid idea was this? Where I put my files and what I call them is none of your business, Microsoft. – TheBlueSky – 2013-09-13T08:20:15.547