Open a task working in background

Nikhilesh T S 21 Reputation points
2022-09-30T09:26:15.423+00:00

I'm scheduling a bat file to run on windows startup irrespective of user login or not. This is run on a windows server so that after a patch is done the system restarts and the app starts automatically not waiting for a user to be involved every time the patch is done. The task is running as expected but there is a problem with monitoring the bat file and see what's happening in the file.

Is there a way to look at what is happening with the bat file while it's running?

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,264 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,091 Reputation points
    2022-09-30T12:46:51.897+00:00

    Capture stdout and stderr. Set the scheduled task to execute program "cmd.exe".

    In the arguments field set it like this example:

    /c C:\Scripts\MyScript.bat 1>>"C:\Scripts\Logs\MyScript-%date:~10,4%-%date:~4,2%%date:~7,2%.log" 2>&1

    That will create a daily log file of all executions of the task. That will capture stdout and stderr for programs that get called.

    In the bat file display the date and time and the return code from programs that it executes.

    @echo %date% - %time% MyScript is starting.  
    SomeProgram.exe    
    @echo %date% - %time% MyScript is ending. SomeProgram RC=%errorlevel%    
     
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 43,996 Reputation points
    2022-09-30T15:38:32.707+00:00

    Hello there,

    When you are writing your script, %errorlevel% can be used to retrieve the return value of a program or script. You can place %errorlevel% in a variable and use it to determine the next branch of execution in the script.

    If your needs end up being more complex, then you may want to consider Powershell or VBScript.

    To understand the backend you can use the Microsoft tools.
    Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity. You can get the tool from here https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    System Monitor (Sysmon) is a Windows system service and device driver that, once installed on a system, remains resident across system reboots to monitor and log system activity to the Windows event log.You can get the tool from here https://learn.microsoft.com/en-us/sysinternals/downloads/sysmon

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    1 person found this answer helpful.
    0 comments No comments