windows task scheduler: how trigger task then program has closed? - windows

I'm building a specific task.
I need to track when a program ends, make report and analyze it. So, I want to create a schedule for it.
For example, I have process called testing.exe, and I want to check for it's log after it finishes job. I have analyze.bat file. I just need something to run it, just after testing.exe finishes it's job and closes.
I can't change anything in program code, so I believe, that task scheduler is the only way.
Help me please

Task Scheduler is not even required for this! It can be done under just batch!
#echo off
set batdir=____________
:begin
tasklist /FI "IMAGENAME eq testing.exe" 2>NUL | find /I /N "testing.exe">NUL
if "%ERRORLEVEL%"=="0" goto start
goto begin
:start
tasklist /FI "IMAGENAME eq testing.exe" 2>NUL | find /I /N "testing.exe">NUL
if "%ERRORLEVEL%"=="0" goto start
start %batdir%\analyze.bat
Just replace _________ with the directory of analyze.bat and it should work. What it does is: it first waits for testing.exe to start. If it started or was already on, it switches and now waits for testing.exe to close. Once it closes, it starts analyze.bat which is exactly what you wanted without the need of Task Scheduler. Hope I helped!

Related

Windows Batch File - CLS command not working

Running on Windows 10 Pro 64-bit [Version 10.0.17763.1637];
I'm writing a simple batch file to show active processes:
cls
echo.
echo Showing all processes similar to %1*
tasklist /FO table /FI "IMAGENAME eq %1*"|more
However, I see that the screen is not being cleared as the script is launched (CLS works fine if I invoke it manually from my Command Line).
I know it's actually a quite silly issue, I'm just curious to know why that hppens and if there's any workaround.
Thanks in advance!
Massimo
i checked your script and cls works perfect(except for it isn't showing active processes) you just need to add #echo offand also validate encoding as ANSI if it isn't,if you want command line without prompt,here it is:
#echo off
cls
echo.
echo Showing all processes similar to %1*
tasklist
rem tasklist /FO table /FI "IMAGENAME eq %1*"|more

i want to run a batch script into my pc when it is locked

i want to run a batch file on my pc for an overnight activity. The problem is that i can't do that because the pc is locked.
I am using this code:
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" | find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
goto EXIT
:EXIT
and it works only the pc is unlocked.
Any help will matters.
Create a new Scheduled Task. Set the task to run when user is logged in or not. Then set the interval of your task time.
On Windows 8 etc you are able to set triggers on when the task should be kicked of by using either a set time or when PC goes on idle, when an event occurs etc.
There is also an option for On Workstation Lock
If this is not your intention to use scheduler. Then right a script that runs in a permanent loop by adding some sleep time and only rerun the taks every now and again, something like this (untested, just used yours as example)
:START
#echo off
tasklist /FI "IMAGENAME eq \\Desktop\notepad.exe" |find /i "\\Desktop\notepad.exe"
IF ERRORLEVEL 1 GOTO LOOP1
IF ERRORLEVEL 0 GOTO EXIT
:LOOP1
start notepad.exe
timeout 300
goto START
timeout 300 is basically sleeping the script for 300 seconds and will start from START again. We can then run the batch file before locking the PC and it wil lrun in a continuous loop. Even though it might not be the right way, it is an option. Perhaps some more detail around how often the batch file should run?

Run a scheduled windows task only if a specific program is running

I have Windows task scheduler set up to run a batch file that launches Firefox and runs a macro inside it.
The task is scheduled for weekdays at a specific time, and I want it to check if my messenger program is running before it activates. Kind of like a failsafe, if the messenger program isn't already running in the first place, I don't need the scheduler to launch Firefox.
Is this possible with some kind of trigger/argument I could add? Maybe even to the batch file itself? How would you suggest I do this?
you can do this to figure out if the firefox is on running
tasklist /FI "IMAGENAME eq firefox.exe" 2>NUL | find /I /N "firefox.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running

Monitor a start of process in windows, then execute something (stop another process/service)

can someone think of a solution for something like this? :
Program/script logic: It would constantly monitor the windows OS for a process starting within it (***1.exe) (I guess it could constantly run via task scheduler to do the constant monitoring?) , while it sees that ***1.exe is running, it would kill/end another process ***2.exe, and once ***1.exe would go away, it would no longer be stopping the ***2.exe process.
I think it could be either a bash script, powershell script, or a windows service?
Thanks!!!
You can use the Register-CimIndicationEvent cmdlet to register for events raised by Win32_ProcessStartTrace WMI class:
# Define which events to listen for
$NewProcessQuery = "SELECT ProcessId,ProcessName FROM Win32_ProcessStartTrace WHERE ProcessName LIKE '%1.exe'"
# Define the code to run every time a new process is created
$ProcessAction = {
# See if any instances of *2.exe processes are running
if(($TargetProcess = Get-CimInstance -ClassName Win32_Process -Filter "Name LIKE '%2.exe'"))
{
# Terminate them
$TargetProcess |Invoke-CimMethod -MethodName Terminate
}
}
# Register for the event
Register-CimIndicationEvent -Query $NewProcessQuery -SourceIdentifier ProcessCreated
So since the solution above was for only windows 2012 and up, I decided to try another solution. This should work for regular processes, but I'll have to try something else rather than %ERRORLEVEL% because the process I'm monitoring is originally an msi installer and seems like it returns and errorlevel of 1 all the time (running or not) while regular processes return 0 or 1 depending on the status. The process I'm ending starts back up automatically, that's the reason there's no start service command included in here, timeout was set to 62 seconds because the service starts back up automatically every 60 seconds, a /NOBREAK can be added if wanted to eliminate the possibility of user input starting it (if this would be ran without a task scheduler,etc.)
:loop_check
TIMEOUT /T 62
TASKLIST /FI "IMAGENAME eq process.exe" 2>NUL | find /I /N "process.exe">NUL
IF "%ERRORLEVEL%"=="0" (
GOTO stop_process2
) ELSE (
GOTO loop_check
)
:stop_process2
ECHO killing task
TASKKILL /F /IM process2.exe
GOTO loop_check
Read my previous reply/comment before this one for more clarity. This is the final solution that worked for me. A star(*) is included at the end of the 'BeginningOfApplicationName' because the installer/msi I'm detecting has sometimes different names based on it's version, so it finds/finishes the ending (wildcard). Since the name of the process I'm monitoring can have different names, I couldn't compare it to a static string, so I'm comparing it to INFO: , seems thats what windows (2008 and 2012!) both print out when a process is not found.
#ECHO OFF
SETLOCAL EnableExtensions
:loop_check
TIMEOUT /T 62
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq BeginningOfApplicationName*"') DO IF %%x == INFO: (
GOTO loop_check
) ELSE (
GOTO stop_process
)
:stop_process
TASKKILL /F /IM process.exe
GOTO loop_check

scripts to manage service start under windows

I'm currently looking for the best way to manage a service start/stop under windows.
I will use the task sheduler provided by windows to start a script at a specific time.
The goal of the script is to :
check if the process "X" is running
if not service Y can be started
if yes, check in one hour if the process "X" is still running
And also what is the best language to do it ? Is Python a good choice ?
Sebastien
Here is a batch script (save as .bat)
tasklist /nh /fi "imagename eq x.exe" | find /i "x.exe" >nul && goto :HOURCHECK || net start Y
exit >nul
:HOURCHECK
timeout /t 3600
tasklist /nh /fi "imagename eq x.exe" | find /i "x.exe" >nul && REM Running || REM Not Running
Just replace my REM comments to do what you want when it checks after an hour.

Resources