Starting independent batch files through ssh and process ownership - windows

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

Related

How to work around ebook-convert.exe (in calibre portable) halting batch file execution?

I use ebook-convert.exe from a batch file, and on one of my machines (old Windows vista laptop), any calls ebook-convert.exe prevent further commands in a batch file from running (in that .bat file, or in that cmd shell instance for instance). The conversion to .mobi actually succeeds, prints no errors even in -v -v full verbose mode. I tried versions 1.20.0 and 0.9.6 of calibre portable. It seems to be related to the executable itself, and not to the specific conversion job (just running it with -h flag has same effect). I tried running cmd in administrator mode, wrapping it with 'call' command, nothing helps. Assuming the calibre issue is a black hole that can't be solved, is there a DOS trick I could possibly try to keep batch commands running no matter what odd thing happens with ebook-convert.exe on that machine?
Using call in a batch file should work on normal console programs but it is possible for programs to "escape" if they really want to.
Using Start instead might work but it does not wait by default and its syntax is not sane:
Start /B /WAIT c:\path\without\spaces.exe optionalParameter
or
Start "" /B /WAIT "c:\path\w i t h\spaces.exe" optionalParameter
Remove /B to run the application in a new console.
Another option is to call ping localhost and For (to get the file size with %%~zA) in a loop until the size of the destination file stops increasing. This is a bit of a hack and might not work, it depends on how the converter opens the file and how often it flushes the writes.

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.

How do I schedule an scp job on Windows?

First of all, I am not an expert programmer, and I don't know much of the programmer's lingo. So please bear with me.
I am using Cygwin on windows, to copy a file from home directory to a remote server (which uses Linux) using SCP. I need to do this every day and so I want to automate it. I know how to schedule tasks in task scheduler, but I don't know what kind of file to save an scp command as. Please help? Oh and I don't have admin access, so I cannot install or use third party applications
First add Cygwin to your Windows Environment Variables. You can find directions on how to do that here (the directions you need are almost all the way at the bottom of the page). Now you should be able to run Linux commands from the command prompt. Simply make a .bat file in notepad with the commands you need to run. It should look something like this:
scp /cygdrive/d/test.txt <linux ip>:/etc/var/test/test.txt
Then use task scheduler to run the .bat file.

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.

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

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.

Resources