Wednesday, July 15, 2015

Monitor active time of a process in windows OS

I wrote a simple script that will run on any windows operating system silently, then log if the specified application is running (eg. notepad.exe), if it is not running it will not update the log and the reason is to avoid large generated log file.

<<< logger.bat >>>
@echo off

set processname=notepad.exe                    
set MYPATH=c:\a
set LOGFILE=%processname%_%date:~4,2%%date:~7,2%%date:~10,4%.log
set PATHLOG=%MYPATH%\%LOGFILE%
set _my_datetime=%date%_%time%


echo %_my_datetime%, Searching for %processname%... >> %PATHLOG%
:loop
set _my_datetime=%date%_%time%
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /I /C:"%processname%"') do call :searchprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop


:searchprocess   
echo %_my_datetime%, %success% >>  %PATHLOG%&goto :eof

<<< monitor_notepad.vbs >>

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "c:\a\logger.bat" & Chr(34), 0
Set WshShell = Nothing


You just have to create shortcut to startup folder.