Solved Can't Figure Out Why .bat File Not Working Properly

October 2, 2018 at 12:54:39
Specs: Windows 10, i7-7700K@4.2GHz, 32 GB
I created a batch file to start NordVPN and then start Firefox, and planned that when Firefox closes, NordVPN should close and nordrvpn-service should also stop. When I run the batch file, NordVPN starts as desired, Firefox then starts as desired, and IPLeak.net confirms that I am connected to the VPN.

But when I close Firefox, NordVPN does not close, but the nordvpn-service does.

Here is the batch file:
@echo off
net start "nordvpn-service"
cd "C:\Program Files (x86)\NordVPN\"
start "" "nordvpn.exe"
cd "C:\Program Files\Mozilla Firefox"
start "" /W "firefox.exe"
cd\
wmic process where name="nordvpn.exe" call terminate
net stop "nordvpn-service"

However, if I put the last 2 commands in a batch file, that file will close nordvpn and stop nordvpn-service.

If anyone can help solve this problem, I would appreciate it very much.


See More: Cant Figure Out Why .bat File Not Working Properly

Reply ↓  Report •

✔ Best Answer
October 3, 2018 at 10:28:04
I downloaded & installed nordvpn, ran nordvpn.bat as admin:

@echo off
net start nordvpn-service
cd "C:\Program Files (x86)\nordvpn"
start "" "nordvpn.exe"
Timeout 5 /NOBREAK >nul
::above line is here to allow nordvpn to startup
start /Wait firefox.exe
taskkill /F /IM nordvpn.exe
net stop nordvpn-service
exit/b

i run nordvpn.bat as admin(right click the .bat file and select run as administrator):

the service starts
nordvpn.exe starts ( it autoconnects to the vpn in about 3seconds)
meanwhile firefox starts

then i close firefox:

nordvpn.exe terminates
cmd.exe outputs that the service was succesfully stopped

however, the icon remains visible in the windows taskbar as an icon, but if i hover my mouse over the icon it dissapears.

I'm using windows 10 and everything seems to work on my side

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded



#1
October 3, 2018 at 01:06:45
wmic process where name="nordvpn.exe" call terminate

should be:

taskkill /F /IM "nordvpn.exe"

also what is this "cd\", what are you trying to do here? move back to C:\?

and for net stop I'm not sure if you can stop non-native windows services with net stop, also i have no way of checking if the name of the service is correct

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded

message edited by hidde663


Reply ↓  Report •

#2
October 3, 2018 at 06:21:08
I probably should have noted that I am not a programmer...I'm an orthodontist with a keen interest in how my system operates, and enjoy making the PC do what I want.

Specifically, I added the "cd\" because I thought maybe the file was not working because I needed to be back in the root of "C:". And the "wmic" command does work when in it's own short batch file. I had tried "taskkill" but that would not kill the process. Additionally, "net stop" is able to stop the service (and "nordvpn-service" is the correct name) if it is in that two-line batch file.

I have figured out a way to make all of the commands work, but it doesn't seem as simple as it should be. If I join the short file with the first, longer, list of commands, I get the result I want. The VPN service starts, the VPN process starts, and Firefox starts behind the VPN. Then when I close Firefox, the process terminates and the service stops.

Here is the batch file that works (Run as Administrator):

@echo off
net file 1>nul 2>nul && goto :run || powershell -ex unrestricted -Command "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~fnx0 %*'"
goto :eof
:run
net start "nordvpn-service"
cd "C:\Program Files (x86)\NordVPN\"
start "" "nordvpn.exe"
cd "C:\Program Files\Mozilla Firefox"
start "" /W "firefox.exe"
cd "C:\Users\Jon\desktop
start "" "kill nord.bat"

message edited by IndigoJon


Reply ↓  Report •

#3
October 3, 2018 at 06:51:17
Running this as admin does not work?

the cmd.exe window will wait for firefox to be closed (by pressing the firefox close button), only then it will continue executing code, thus killing nordvpn.exe&the accompanying service.

p.s. I can get the free 3-day trial of nordvpn myself so i can test it on my desktop at home in a few hours, I can even make the cmd.exe window dissapear by running it inside a hidden windowsscript file(wscript.exe)

Best way to do it by making the .bat file run inside a hidden wscript.exe, which is opened by a shortcut on the desktop, tho I will really need to install nordvpn myself by then to check if everything is working properly.

Let me know what you think :)

nordvpn.bat:


@echo off
net start nordvpn-service
start "C:\Program Files (x86)\NordVPN\nordvpn.exe"
start /Wait firefox.exe
taskkill /F /IM nordvpn.exe
net stop nordvpn-service
exit/b

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded

message edited by hidde663


Reply ↓  Report •

Related Solutions

#4
October 3, 2018 at 07:23:16
Copied your file exactly and ran it as admin. VPN service starts, but NordVPN program does not. Firefox does start. CMD window prompt is at: C:\Windows\system32>

When I exit Firefox, the VPN service remains running.


Reply ↓  Report •

#5
October 3, 2018 at 07:30:24
and if you replace this line:
start "C:\Program Files (x86)\NordVPN\nordvpn.exe"
with:
start "C:\Program Files (x86)\NordVPN\" "nordvpn.exe"

if it doesnt work, I will install nordvpn myself in around 2 hours and try it again

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded


Reply ↓  Report •

#6
October 3, 2018 at 08:03:10
Starts the service, and then CMD window says: "The system cannot find the file nordvpn.exe."

Reply ↓  Report •

#7
October 3, 2018 at 08:05:12
cd "C:\Program Files (x86)\NordVPN\"
start "" "nordvpn.exe"

instead of start "C:\Program Files (x86)\NordVPN\nordvpn.exe" also doesnt work?

hmmmmm

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded


Reply ↓  Report •

#8
October 3, 2018 at 09:38:09
Your batch file:

@echo off
net start nordvpn-service
cd "C:\Program Files (x86)\nordvpn"
start "" "nordvpn.exe"
start /Wait firefox.exe
taskkill /F /IM nordvpn.exe
net stop nordvpn-service
exit/b

will start the vpn service, start the vpn program and start Firefox. But when Firefox closes, the vpn program remains running and the service remains running. I can kill those both with this file:

wmic process where name="nordvpn.exe" call terminate
net stop nordvpn-service

if I run it as admin.

But even when Firefox starts under the first file above, I am not able to double-click URL's in my password manager (Keepass) and have them open in Firefox. ????


Reply ↓  Report •

#9
October 3, 2018 at 10:28:04
✔ Best Answer
I downloaded & installed nordvpn, ran nordvpn.bat as admin:

@echo off
net start nordvpn-service
cd "C:\Program Files (x86)\nordvpn"
start "" "nordvpn.exe"
Timeout 5 /NOBREAK >nul
::above line is here to allow nordvpn to startup
start /Wait firefox.exe
taskkill /F /IM nordvpn.exe
net stop nordvpn-service
exit/b

i run nordvpn.bat as admin(right click the .bat file and select run as administrator):

the service starts
nordvpn.exe starts ( it autoconnects to the vpn in about 3seconds)
meanwhile firefox starts

then i close firefox:

nordvpn.exe terminates
cmd.exe outputs that the service was succesfully stopped

however, the icon remains visible in the windows taskbar as an icon, but if i hover my mouse over the icon it dissapears.

I'm using windows 10 and everything seems to work on my side

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded


Reply ↓  Report •

#10
October 3, 2018 at 10:31:30
I am not entirely sure how keepas works(I am familiar what it is for), I might have to install that as well, but I won't be doing that today :)

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded


Reply ↓  Report •

#11
October 3, 2018 at 11:04:49
So, yes, your file starts the service, starts NordVPN, and starts Firefox. When I close Firefox, NordVPN terminates, and the service stops.

CMD.exe window remains on desktop until exit from Firefox.

Only other issue is why double-click URL in Keepass does not open URL in Firefox if Firefox was opened from batch file as above.

Definitely making progress, and thanks very much!


Reply ↓  Report •

#12
October 4, 2018 at 00:37:27
So the url does not start in firefox, but does it start in a different browser, lets say the edge browser?
Or does nothing happen at all?

Also do you want me to hide the cmd.exe window in the background or not?(that does requite another process to be started whihc you can see in the taskmanager as wscript.exe)
Wscript.exe can be found here: C:\Windows\System32\wscript.exe

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded


Reply ↓  Report •

#13
October 4, 2018 at 01:17:56
I might be able to do a workaround for the URL, if the link is located on disk in plan text.

I can make firefox start with a given URL, but I need to be able to read it from a file somehow.

But, there is also a firefox add-on called keefox which might do the trick:
https://addons.mozilla.org/nl/firef...

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded

message edited by hidde663


Reply ↓  Report •

#14
October 4, 2018 at 06:27:38
It would be better if the cmd.exe window were hidden.

I have troubleshot the URL issue and the behavior is somewhat strange. If no browser is open, then any URL can be opened from Keepass, either with or without the VPN being enabled. And if the browser has been opened in this way, by clicking a Keepass URL, then clicking a different URL in Keepass will open that one without incident.

But if a browser (tested Chrome, Firefox, IE) is already open, then URL's can't be opened by clicking in Keepass, regardless of whether or not the VPN is enabled. I must not have noticed this behavior in the past.
In any event, it it not a problem caused by the way the batch file works.

Thanks for working on this!


Reply ↓  Report •

#15
October 4, 2018 at 07:22:07
Make a shortcut of nordvpnstartup.bat and place it on the desktop, then right click the shortcut --> properties;
under tab shortcut - set option run: to Minimized
under tab shortcut - under button advanced - set run as administrator to true

You can ignore the text below if you wish to only minimize the cmd.exe window instead of hiding it from view compleetly

explanation of nordvpnstartup.bat:

nordvpnstartup.bat check if 2 files exist in the current directory (the directory where the file is executed from)
if invisible.vbs and/or nordvpnMAIN.vbs do not exist, it will make the 2 files in the current directory.
it will then start wscript.exe and run the (newly created invisible.vbs, meanwhile nordvpnstartup.bat closes)
invisible.vbs will run (the newly created) nordvpnMAIN.bat invisibly in the background& then closes.
nordMAIN.bat, now running in the background runs the code in post #8
However, I have not checked if nordvpnMAIN.bat inherrits nordvpnstartup.bat's admin rights.
I will test this at home in about 3 hours.

::nordvpnstartup.bat code:

@echo off
if not exist "invisible.vbs" (
echo CreateObject^("Wscript.Shell"^).Run "C:\REPLACE_THIS_PATH\nordMAIN.bat", 0, False >invisible.vbs
)
if not exist "nordvpnMAIN.bat" (
echo @echo off>nordvpnMAIN.bat
echo net start nordvpn-service>>nordvpnMAIN.bat
echo cd "C:\Program Files (x86)\nordvpn">>nordvpnMAIN.bat
echo start "" "nordvpn.exe">>nordvpnMAIN.bat
echo start /Wait firefox.exe>>nordvpnMAIN.bat
echo taskkill /F /IM nordvpn.exe>>nordvpnMAIN.bat
echo net stop nordvpn-service>>nordvpnMAIN.bat
echo exit/b>>nordvpnMAIN.bat
)
wscript.exe "invisible.vbs"
exit/b

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.35v
MSI Armor RX 570 4GB@1340Mhz core@1.110v/1865MHz BiosModded

message edited by hidde663


Reply ↓  Report •

#16
October 9, 2018 at 01:36:37
I did actually test it at home and it did work, but i forgot to mention it here :)

you may want to replace this line:
echo start "" "nordvpn.exe">>nordvpnMAIN.bat

with these 2:
echo start "" "nordvpn.exe">>nordvpnMAIN.bat
echo timeout 5 /NOBREAK >nul >>nordvpnMAIN.bat

this is to allow nordvpn.exe to fully start up in time before firefox connects to the internet.

i5-6600K[delid]@4.7GHz/4.3GHz@1.376v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2700MHz@1.35v
MSI Armor RX 570 4GB@1260Mhz core@1.000v/2000MHz


Reply ↓  Report •

Ask Question