Task Sheduler: How to run batch file through cmd instead of taskeng.exe? - windows

I wrote a simple script:
cd C:\TESTS\example
call git pull
cd C:\TESTS\example\AutoApp\bin\debug
start AutoApp.exe
I created daily task in sheduler and when task running it open taskeng.exe. That "command line" do not have any git/cmd command wich I use in script. And my application and git pull do not work.
If I open batch file by click on it, it works fine(git pull done and app run done) and run through cmd.
After firs option of anwser task is running all time.
After second option of anwser.
How to solve this problem?

Change the command line used in Task Scheduler to call cmd.exe to launch the batch file instead:
cmd /c "YourBatchFile.bat"
Or
%comspec% /c "YourBatchFile.bat"

Related

Windows "Start" command doesn't return from within "Execute shell script" step

Within a kettle job we need to call a program that doesn't return until it is stopped. From a command line this can be done with the Start command of Windows:
Start "some title" /b "C:\windows-style\path with spaces\program.exe" unqoted_param -i -s "quoted param"
This works well by starting the program in another shell while the shell calling it returns and can continue. From within a kettle job this should be possible too, I think, by simply running the above command in a Execute a shell script step with the Insert script option.
However instead of returning from running the program in a new shell, the execution waits for the program to finish. This is not what we want because while the program is running (it's a VPN connection) we need to perform some other steps before the program is stopped again.
I suspect this might have something to do with how kettle performs the script, namely by putting the commands in a temporary batch file, then running that one. At least that's how it is presented in the job log:
2019/09/17 09:40:24 - Step Name - Executing command : cmd.exe /C "C:\Users\username\AppData\Local\Temp\kettle_69458258-d91e-11e9-b9fb-5f418528413ashell.bat"
2019/09/17 09:40:25 - Step Name - (stdout)
2019/09/17 09:40:25 - Step Name - (stdout) C:\pentaho_path>Start "some title" /b "C:\windows-style\path with spaces\program.exe" unqoted_param -i -s "quoted param"```
For a quick solution, you can use parallel execution in the job.
From the Start step (or whichever step precedes the need for the VPN), activate the option to run the subsequent steps in parallel. Then you can put the shell script step in its own branch while the rest of the job can continue (with a wait step on the other branch to allow the VPN to start).
From the question, you are probably running jobs from the Pentaho server. If you happen to run them from a scheduler with kitchen.bat, you could start the VPN before calling kitchen of course.

Command prompt started from batch file closes when operation is stopped

I am using a batch file to open a number of command prompts while I do some development work, however, I often need to restart 1 or more of the programs, while I'm debugging.
Is it possible to change the code to keep the window open to allow me to restart the application?
C:\
cd /d "C:\Users\me\mydir"
start redis-server
start celery worker -A celery_worker.celery --loglevel=info
start python manage.py runserver
I'd like to be able to kill and restart the celery worker / webserver whenever I make a change to the code.
START attempts to start the program directly; if the program uses stdin and stdout, this will invoke the console host to handle it. If you want a window that will remain open, instead of using START, try CMD /K - for the celery command line specifically, change start celery ... to CMD /K celery .... This starts a command prompt and runs the specified command; if the command terminates, the command prompt will remain, waiting for input (and will remain open until you exit it). Look at the output of CMD /?, or the page on cmd at SS64 for more information.

Using a batch file to start pm2 when windows starts?

I can't get pm2 to start my apps on Windows start. I'm running Windows Server 2012 R2 Standard and pm2 2.4.2.
I have a pm2 process file in JSON format which I use to start all my apps.
c:\pm2\process.json
{
"apps": [
{
"name" : "my-app",
"script" : "c:\\node\\myapp\index.js"
}
]
}
I have a batch file which uses the JSON file:
c:\pm2\pm2-startup.bat
#echo off
set HOMEDRIVE=C:
set PM2_HOME=C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2
cd C:\pm2 & pm2 start process.json
I have a Windows task scheduled to run the batch file:
Trigger: At startup
Run under: An administrator account
Run whether user is logged in or not: Yes
Run with highest privileges: Yes
Action: Start a program
Script: C:\pm2\pm2-startup.bat
Start in: C:\pm2
If I run the batch file manually (double clicking it), it works. If I run the scheduled task manually (right-click, run), it works.
When I restart the server and check the scheduled task, it has run, no errors, however the apps are not running. Doing pm2 list shows no apps in the table.
I don't want to use pm2-windows-service because I don't want to run pm2 as a service (tried it and it was flaky).
I don't want to use pm2-windows-startup either as it doesn't seem to work with a pm2 process file, it just tries to remember what was running before.
I do want to use a plain batch file on startup.
What am I doing wrong with the batch file..? Why does the scheduled task run ok, but the pm2 list is empty..?
I know this is an old question, but I'll leave an answer for future searches.
You can put a shortcut to the .bat file in the Windows Startup folder.
Assuming the PM2 cmd is in path, your bat file could look like this.
#echo off
SET PM2_HOME=C:\Users\pstart\.pm2
pm2 start C:\path\to\run.js
That'll start PM2 and run the program. Now you just take the bat file and save it somewhere and create a shortcut in the Windows Startup folder by clicking the Start button -> Run -> shell:startup and pasting it in there.

Command failed when redirection failed

I'm running java program with bat script using command line:
start "AppName" /B %LOCAL_JAVA% -jar Starter.jar %* 1>out.txt 2>err.txt
I want to run bat script for a second time, java application will connect using TCP socket to first running and will pass script args. But instead of this I get error:
The process cannot access the file because it is being used by another process.
This is because files out.txt and err.txt are used by first running application.
How I can ignore failed stream redirection and run my command line?
start "AppName" /B %LOCAL_JAVA% -jar Starter.jar %* 1>%random%out.txt 2>%random%err.txt
like that? what do you mean it runs twice? does the command run twice or the program?

What's the nohup on Windows?

I want to run a Java jar file like this:
java -jar spider.jar
How to run it on the background on Windows?
Like this on Linux:
nohup java -jar spider.jar > /var/tmp/spider.log 2>&1 &
You could use the Windows start command:
start /min java -jar spider.jar
This command is not really the same as nohup; but it might be suitable if you're happy with the Java process running in a separate minimised window. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx
On Windows it's not normal that a process terminates once its parent was killed (like Unix-likes do it normally). Therefore there is no direct necessity for something like nohup. If you want to avoid the console window associated with it, you can use javaw but redirection won't work, then.
The only way to get the nohup behavior, where the process still runs after logging off (like for micro-services, analytic tools, server batch jobs etc.), is to run the .bat file with the start javaw -jar ... contents as either a service or a scheduled task.
save the following code to nohup.vbs, and add the directory to PATH.
Set args=Wscript.Arguments
Set ws=CreateObject("wscript.shell")
ws.Run args(0),0,true
then, run:
nohup "java -jar spider.jar > /var/tmp/spider.log"
Note that you should quote the full commands and the redirects.
For windows run following mentioned command in Command Prompt or in Terminal
nohup (file need to run) > (File you need to save the log) 2>&1
Ex:
nohup ./bin/windows/kafka-server-start.bat config/server.properties > ./MyKafka.log 2>&1

Resources