Windows cmd: How to find and kill .bat file that was run and hidden with wscript? - windows

I was playing with running a Turntable.fm bot, and I wanted to run it on my local machine's Nodejs installation, but not show the cmd window. So I ended up using a vbs script to hide the window:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
And then my .bat file looks like this:
.\node.exe .\Turntable-API\bots\carlin.js
That's it. Pretty simple. I created a shortcut file in Windows that then ran this:
C:\Windows\System32\wscript.exe "C:\ttbot\invisible.vbs" "C:\ttbot\ttbot.bat"
The issue now is that I have no idea how to find or kill the process that was started. When I try to look in my task manager, there's no wscript.exe, or cmd.exe, or node.exe, or invisible.vbs, or ttbot.bat running process of any sort. When I try running the .bat file in cmd, it doesn't close immediately, so I assume it needs manual shutdown of some sort. What should I do?

Try using Process Explorer, search for any of the processes that you mentioned above incase the processes are running under an existing process, find that, then you can kill yours.
If it doesn't show up in Process Explorer the likelyhood is the script isn't running or it ended after execution, possibly because of an error. Try running without the vbscript to make sure everything is ok.

Related

How do I tell a windows batch script to execute the next line without waiting for the previous line to finish executing?

I'm setting up a .cmd script to be run on startup for a pseudo-server (It's actually a laptop, but that's irrelevant) to automatically launch pageant, load an SSH key, connect to an SSH server using Putty (Pageant would automatically authenticate with the key), then launch mIRC which in turn has a series of scripts setup to operate as an IRC bot and automatically connect to networks using putty as an SSH tunnel.
With that in mind, I have the below code in a startup.cmd file:
"C:\Program Files (x86)\PuTTY\pageant.exe" c:\Path\To\Private\Key.ppk
"C:\Program Files (x86)\PuTTY\putty.exe" -ssh user#host
"C:\Program Files (x86)\mIRC\mirc.exe"
EXIT
When I test run this file, the command prompt runs the first line, launches pageant, and then sits there and does nothing until I close pageant completely. I believe I have an idea on what the issue here is, but I can't find any information on how to resolve this in a batch file.
I do know on linux systems, if I were running a bash script to do something similar, I would want to have a & symbol at the end of each line to tell it to run the next command without waiting for the previous command to finish executing. I did try that in the batch script in the off chance that would work (It didn't).
For those who may ask, this is on Windows 8.1 64 bit. The user running this script is not an administrator.
I can't comment to expand on Squashman's suggestion, so let me answer here.
In your case, if you only want to have Pageant running in the background, without interacting with it, I think it's best to run:
START "" /B <your command>
The /B parameter will spawn the process without launching a new window for it, which seems like something you'd like to avoid (anyway, it's probably closest to the behaviour you can obtain in Linux with &).
Please note that if you close the window from which you spawned this process, it will terminate as well.

Windows: start/stop (toggle) portable XAMPP via single batch file. Switch between multiple XAMPPs

I have multiple portable XAMPP Servers on my local windows machine (to simulate each production Server I work on).
In order to start one of the local XAMPP Servers, I have to make sure no other XAMPP is running, otherwise it won't start. So I have to find the correct xampp_stop.exe each time I want to switch servers and get the correct xampp_start.exe for the one I want to switch to.
I think an ideal setup would look like this:
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Shortcuts\
|__"xammp serverName1 toggle.bat"
|__"xammp serverName2 toggle.bat"
|__"xammp serverName3 toggle.bat"
|__"xammp kill.bat"
...
The toggle batch files would kill any previous running server and start the one respectively.
The kill batch file would kill any running server.
All placed inside the Shortcuts directory to have fast excess via WindowsKey+Search.
Does anyone know an approach on this?
Ok, I found a solution. It works well with all XAMPP Servers I have installed.
This script shuts down any server before it starts the one it belongs to.
The script just uses the existing apache/mysql start/stop batch files which are located inside XAMPP root.
In order to call the batches hidden (without any permanent console window) they are called by a virtual basic script. Here is how that works: https://superuser.com/a/62646/431390
It is quite simple.
And this is the Batch to start the server it belongs to. It goes into each XAMPP root.
XAMPP_root/XAMPP_Start.bat
#echo off
wscript.exe "C:\someDir\invisible.vbs" %~dp0"apache_stop.bat"
wscript.exe "C:\someDir\invisible.vbs" %~dp0"mysql_stop.bat"
wscript.exe "C:\someDir\invisible.vbs" %~dp0"apache_start.bat"
wscript.exe "C:\someDir\invisible.vbs" %~dp0"mysql_start.bat"
exit
The next file is the batch file to shut down any XAMPP server. It's just the first part of the other one...
XAMPP_root/XAMPP_Stop.bat
#echo off
wscript.exe "C:\someDir\invisible.vbs" %~dp0"apache_stop.bat"
wscript.exe "C:\someDir\invisible.vbs" %~dp0"mysql_stop.bat"
exit
The next file is the vbs script, that starts the batch files hidden. So that the whole thing won't occupy any console windows.
C:/someDir/invisible.vbs*
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Running batch file at Windows 7 UNLOCK

I have a very simple .bat file. It only has one line:
powercfg -h off
What this does is turn off the computer's ability to hibernate. It works fine when I double click the file. I can confirm this by going into the command prompt and typing "powercfg /a", which shows me that hibernate is indeed off. The problem is that this batch file doesn't seem to be running at logon even though it's in my Startup folder.
I've tested it by turning hibernate on using the command prompt ("powercfg -h on") and then actually putting my computer into hibernation. Then I turn it back on and log in to Windows. But when I open a command prompt and type "powercfg /a" it shows me that hibernate is still on. It seems the batch file doesn't run at logon even though it is in my Startup folder.
I've also tried making it run from a task scheduled to run at logon, but that didn't work either.
Some ideas:
Make sure you set the Start in and Program/script options of the batch file correctly.
If (1) doesn't work then try moving the .bat file to a directory with basic permissions.
Try to schedule the execution of the batch file like this cmd /c "c:\path\batch.bat"
Also take a look at this: Batch runs manually but not in scheduled task.
I got it to work using Task Scheduler. The problem was that I was using the trigger "At log on," when I should have chosen "On workstation unlock."
It's obvious to me now, but I didn't think of it at the time: hybernating didn't actually log me off, it only locked me out.

Starting independent batch files through ssh and process ownership

I've used batch files for many things in the past... but I've always had this problem. I'm sorry if this is a repeat question, I'm not entirely sure I know how to phrase it for searching purposes. The problem is this:
1) Batch file starts some process.
2) command window closed by user.
3) process started by batch file ends.
I imagine this is due to the fact that the started process is "called" by the batch file, and is thus it's child. Specifically what I'm trying to do is login to a server through ssh, run a batch file located on that server which then starts a java program. I need the batch to either stay open, or allow the java program to own itself somehow. That way, when I leave the SSH session, the program will continue to run. Any ideas how I can do this?
I'm running a windows XP x64 server with MobaSSH.
You could try using the psexec tools from sysinternals.
Some possible helpful commands:
at
schtasks
sc
wmic
I'm not sure that any of the above commands will be of any help, but I think they're worth checking out.
Question is not clear, but looks like what you are looking for is a way to "detach" the script from the terminal so that it will continue to run even when the terminal is closed.
You can do:
nohup <your-script> &
Or:
<your-script> &
disown

Voodoo with DOS Batch files

I've created a simple batch file that kicks off my *.msi installer within our company, creating a log file of the process, then displays the log file after the installer has completed.
installAndLog.bat:
msiexec.exe /i "\\FileServer2\setup.msi" /l*v "C:\setupLog.txt"
"C:\setupLog.txt"
It works, but there are two (2) glitches that annoy me:
The black console box shows in the background the whole time the installer is running and the log file is being displayed. Q1: How do I hide that?
and
The console box will not close until the log file is no longer being viewed (i.e. notepad.exe is closed). Q2: Can I call the text file in a new process and simply exit?
I was a DOS lover back in the day, but that was too many years ago.
I don't think you can hide the console window when running a batch file. However you can use vbscript instead which will by default not create a console window.
Take the below and put it in a file with a .vbs extension:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "msiexec.exe /i ""\\FileServer2\setup.msi"" /l*v ""C:\setupLog.txt""", 1, true
wshShell.Run "C:\setupLog.txt"
All the double double quotes are there because the entire command must be surrounded by "'s and doubling them escapes them. The the documentation for WshShell.Run for more info.
Q1 - AFAIK you can't really hide the console window.
Q2 - Use the start command. This will launch the specified program (notepad) outside of the shell. It will also prevent the shell from waiting until the application closes to continue processing the batch script.
You might be better off changing the batch script to launch the MSI installer using the start command and having the installer launch notepad to view the log file once installation is complete.
If you really want to get these batch windows away, you'll have to switch over to something else. One simple alternative could be one of the scripting languages supported by the windows scripting host.
Or you try HTA (HTML applications) see here and here.
Run the dos script as a different user by scheduled task or as a service.

Resources