Running a bat file in background in windows - windows

I need to run a bat file in background in windows.
I have tried using theSTART command.
The problem is, I connect to this machine remotely & if I log off, then the process gets killed.
Is there any way to keep a bat file running even after I log off from the remote machine?
Thanks in advance

Srvany: http://support.microsoft.com/kb/137890
RunAsService: http://runasservice.sourceforge.net/
Nssm: http://nssm.cc/

If you execute the bat file from a Windows service (using a service account) you will be able to log off and have the bat file execute in the background.
This walkthrough might be handy
http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

Related

Execute WIndows bat file in Jenkins

I am using Jenkins 2.46.1
I have a build pipeline plugin installed and I want to execute a Windows batch file in it. The batch file should execute in a new command window and not on jenkins console output. I give the below Jenkins pipeline groovy script:
node {
stage 'Init'
bat '''
call C:\\myprj\\mybat.bat stop
EXIT /B 0
'''
stage 'Deploy'
bat '''call C:\\myprj\\mybat.bat'''
}
In the init stage, I want to kill the process if it is already open and in stage deploy it should open a new command window and run my batch file.
The problem is that the above does not work. The build is succesful but no command window opens up. Pls suggest
Technically, to do what you're asking you should be able to run
bat 'start cmd.exe /c C:\\myprj\\mybat.bat'
This will launch a new command windows (cmd.exe) and run the batch file given. Depending how your Jenkins slave is running you may not see anything. (eg if it's running as a windows service or different user, you won't see anything)
Alternative solution if the agent is running in a service and you would like to get output:
bat(readFile("mybat.bat"))
Note: The bat file will need to be in your workspace.
Additional Note: You are no longer running the bat file from its original location. Instead it is being run from a temp location created by the underlying durable task system. This means things like %~dp0 in your script are not going to return the paths you might expect.

Windows login script to add line in configuration file

I am new to this website, and fairly inexperienced with scripting, so be easy on me.
For this particular issue, my environment consists of roughly 1,000 Windows 7 PCs. Every PC has a Solarwinds Log and Event Manager agent running in the background that writes to a LEM server for each PC. Each PC has a configuration file titled spop.conf located in C:\Windows\SysWOW64\ContegoSPOP (I am assuming all PCs are 64 bit). I need to add the line "UseLocalEnvironmentVariableForLocalHost=true" to that file on all PCs in the environment. To do this, I also need to stop the service "Contego_Spop" and restart it after editing. Would it be best to create a new spop.conf file with this line in it and push that out via script?
Does anyone have some words of wisdom for a newbie? Thank you for any help you can give.
I assume you're a domain admin.
Write a .bat file fix.bat that accepts computer name, edits the config file and restarts the service.
sc \\%1 stop Contego_Spop
echo UseLocalEnvironmentVariableForLocalHost=true >>\\%1\c$\Windows\SysWOW64\ContegoSPOP\spop.conf
sc \\%1 start Contego_Spop
run this bat file for each computer name
C:\> call fix.bat host1
C:\> call fix.bat host2
C:\> call fix.bat host3
...

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.

Batch file is not running through Windows 7 Task Scheduler

I have a batch file that executes a php script which fetches files(pdfs) from the backend and saves locally. I have tried executing the batch file through command prompt and it works perfectly.
But when I setup a Windows Task Scheduler to run this batch, it is not working. The command prompt window pop ups and displays alot of unreadable characters and hangs there.
I have also chosen the option for the bat to run with highest privileges but no difference.
Any idea what's wrong?
My mistake. Managed to solve it. The path to php was not set properly.
Now it is fine.

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