4

I can't seem to figure this out.

I am creating a live site and deploying it online

batch file:

START cmd /K "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"


START /wait "" http://localhost:4000/

The thing is running the command hexo generate & hexo serve --draft takes about 5-10 seconds, time varies. Ideally I want to wait for this to occur before going to live site at http://localhost:4000

Some reason this windows batch command just automatically opens up localhost:4000 right away though

Vincent Tang
  • 3,758
  • 6
  • 45
  • 63

2 Answers2

7

I would use /B to stay in the same process and /wait tot wait until the first command is finished. You don't need the /wait in the second line unless there are more commands to follow. If this doens't work, experiment with leaving the cmd /K away. Since runas is an executable and the batch waits until it is finished it's possible you can let away the start command all together.

If all this doesn't work insert 5 ping commands, that is the classic way to wait for ± one second.

START /B /wait "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"


START /B "" http://localhost:4000/
peter
  • 41,770
  • 5
  • 64
  • 108
  • I tried running this but my command prompt simply just sits there and doesn't do anything. No error codes or anything – Vincent Tang Oct 22 '16 at 15:31
  • what exactly dit you try, I sugges here a number of things.. first try the runas part on itself without start in a console, only thereafter try it in a batch – peter Oct 22 '16 at 15:34
  • I copy pasted your code inthe first instance. Then in my original file I removed the /wait command. I also tried /B, but for some reason it would always run the command in the same folder as my batch file. – Vincent Tang Oct 22 '16 at 15:51
  • /K was the only thing that worked for me opening up a new cmd prompt – Vincent Tang Oct 22 '16 at 15:52
  • opening up another prompt makes for not being able to wait until it is finished, you would have to go with the pings to time the second command to start – peter Oct 22 '16 at 16:13
2

I'll take a stab at this, (completely untested).

@Echo Off

(Set SrcDir=%UserProfile%\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io)

If Not Exist "%SrcDir%\" Exit/B
Start "" /D"%SrcDir%" /Wait /B RunAs /User:administrator^
 "Cmd /C Start /Wait hexo generate & Start hexo serve --draft"

Start http://localhost:4000/
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I tried running this but it requested me for admin password, even though I'm the admin :s – Vincent Tang Oct 24 '16 at 17:22
  • You used the command, I just copied it from your opening post, _(apart from my inadvertent use of a capital **A** instead of **a**, now changed)_. – Compo Oct 24 '16 at 18:26