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

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.

Related

Opening Cygwin with Windows bat file and running script file

I require a tunnel between my windows machine to a UNIX server and wish to automate the process so that on startup the tunnel will be generated for me.
I installed Cygwin with ssh and autossh to connect to the remote server, built up the connection manually, and have confirmed that the connection works. The process involves 3 commands, which isn't a lot but something that would be great to have automated.
After creating a .sh script file, which includes my autossh connection commands, and saving it using Notepad ++ as a UNIX document (to avoid any potential conflicts regarding the file ending), I can navigate to this script in Cygwin and call bash script.sh. After which the connection is made and I can work on my server.
My problem comes when creating my bat file:
start /d "C:\cygwin\bin\" mintty.exe "C:\Users\user\Documents\Dev\" script.sh
The first part up to and including the .exe file works to open the Cygwin window, but I have been unsuccessful in feeding the script into it. I even tried including a --bash command before referencing the script file as follows, but I received an error that the command is unknown:
start /d "C:\cygwin\bin\" mintty.exe --bash "C:\Users\andrew\Documents\Development\" tunnel.sh
Does anyone know if and how it is possible to open a Cygwin window and call a script file within this window? This is my first time creating a bat file, so I hope this is perhaps a newbie problem that no one even bothers to post a solution online for...
you don't need start.
assuming your Cygwin is in C:\cygwin
you need just:
chdir c:\cygwin\bin
mintty /usr/bin/bash -l -c /cygdrive/c/Users/user/Documents/Dev/script.sh

Why Autoit script doesn't run over SSH (W10)?

a simple command on AutoIT:
ControlClick("Sighthound Video","","[CLASS:wxWindowNR; INSTANCE:46]","Primary")
ControlClick("Sighthound Video","","[CLASS:wxWindowNR; INSTANCE:49]","Primary")
to click a button in an application.
I create a script called "Toggle.au3".
If I click double click, it works.
If I launch it from the command line, it works.
If I launch from a remote computer with Putty (through SSH) it doesn't works; no error appears but doesn't perform the tasks.
Why? I have also tried to compile in a exe file, but nothing changes.
Windows 10
Bitvise SSH Server
AutoIT 3.3.14.2
THX.
It is because you don't have rights from a remote computer.
If the au3.exe gets the remote's privilege then it is definitely not going to interact.
Edit: However ALL autoit commands are windows based commands, which means C++ commands. If you can call those commands through putty or CMD then it might work

Open Excel on Jenkins CI

I am working on Windows 7 (logged in as session no.1), my Jenkins CI is running as windows service in session 0.
My problem is.. I want to open an Excel file through Jenkins CI in session 0, but want to display its GUI on session 1.
I know that session 0 is isolated in Windows 7, but is it possible to run a process in session 0 and then output in another session? please help.
Edit:
Took a little trial and error, but this is what finally worked for me (Windows 7 64-bit).
Download PsTools from Microsoft site
We only need psexec.exe, but you can extract everything. Extract to some location accessible by Jenkins, preferably without spaces in the path.
Open elevated command prompt: type cmd into Start's quicksearch, right click cmd.exe, select Run as Administrator.
Type C:\path\to\psexec.exe -accepteula and press enter.
Type C:\path\to\psexec.exe -i 1 cmd and press enter. (If you see a command prompt appear, all is good, close it now)
In Job configuration, configure Execute Windows Batch command step
Write the following:
C:\path\to\psexec.exe -accepteula && C:\path\to\psexec.exe -i 1 cmd /c start C:\PROGRA~2\MICROSO~1\path\to\excel.exe
Where:
C:\path\to is your full path to psexec.exe, unless it is in your %path%
-i 1 is the session ID that you want to launch in.
C:\PROGRA~2\MICROSO~1\path\to is your full path to excel.exe without spaces. Since most Office installations are going to be under paths with spaces, like "Program Files (x86), you have to figure out the short path, or place it somewhere without spaces.
Having excel.exe under %path% and working from regular command line was not enough.
A little explanation for those that care:
psexec needs to install a services first. For that, it needs to be run from elevated command prompt for the first time. This is a one-time installation step.
To make psexec work, you need to accept the EULA prompt. This is done per session/user. So even if you run psexec -accepteula in your command prompt, it doesn't help when Jenkins service (running as Local System in session 0) tries to use it. Therefore, you have to place that into the Jenkins job, along with the command. Technically, it only needs to be there once, and can be removed afterwards, but it definitely doesn't hurt to keep it there.
I've used cmd /k and running this command from my local cmd prompt to debug. This is what made me realize I couldn't find a way to escape the spaces (tried various quoting), so had to resort to short file names. Note that short file names are not required, this is just to escape spaces.
no its not-
plus any UI interactions requires you to run Jenkins as Java web start rather than a service or you can not interact with UI elements.

Running programs via shell

I am running Windows 7x64 and Excel 2010x32. I call 32bit dos programs (written in Fortran) via vba using ExecCmd (a Microsoft function that waits for a command prompt process to finish). I send a command line to this function that explicitly contains the program path and the paths of the input file and output file.
This runs fine on my PC and also on a company PC running the same software (OS and Office) and for which I have general access to the C: drive.
On other company PCs, where there is not general access to the C: drive, this does not work - i.e. the dos programs do not produce an output file. On these PCs, I can still run the program at the command prompt manually. It is just that calling this command prompt does not work via Excel VBA.
Now the strange thing is that I can successfully run one of these programs by adding "cmd.exe /c" at the beginning of the command line. That would appear to be running a command prompt within a command prompt (!). The other program (which, incidentally, is quite a bit bigger) does not work at all via vba on these PCs. I need to be able to provide other employees with something that works.
Can anyone shed some light on what is happening here and suggest a work around? I could past some code, but I think the above should be self explanatory.
You're confusing the command shell with the console window. In this context, the distinction is critical.
Console-mode programs (aka "command-line programs") require a console window to provide input and output. When a console-mode program is launched from a GUI program, Windows automatically creates a console window for it (unless instructed otherwise).
The command shell (aka "Command Prompt") is cmd.exe, a console-mode program.
The important point here is that not every console window has an instance of cmd.exe running in it. When a console-mode program is launched from a GUI program, Windows automatically creates a console window but does not automatically create an instance of cmd.exe. If you want to pass a command to cmd.exe you have to do so yourself, or use a run-time library routine that does it for you.
ExecCmd does not do this; it runs the program directly. So passing cmd /c <command> to ExecCmd is not "running a command prompt within a command prompt" at all. Without the cmd /c you aren't running a command shell command, you're just launching an executable.
There are any number of reasons why the command you're passing might need to be given to the command shell. For example:
it might be a built-in command like dir or type which only exists within the command shell;
it might include redirection or pipelining operators, or environment variable substitution;
it might be a script rather than an executable.
There are other cases. If you show us the command line being passed to ExecCmd we may be able to provide more specific advice. (The fact that the same command line is apparently working on some machines is puzzling, but can't be addressed without more information.)

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

Resources