locked
schedule a ping program RRS feed

  • Question

  • I just would like to set the schedule job to run the below ping job .

    just would like the ping can be run in every 30 sec , would advise the windows schedule can only set daily / weekly / monthly but not run in 30 sec , how to set it ? thanks a lot .

    ping Server1 >>D:\ServerPing.txt
    ping Server2 >>D:\ServerPing.txt
    net time >> ServerPing.txt

    Thursday, July 16, 2020 1:44 PM

All replies

  • You can't. As you have found, the task scheduler will not allow that. The shortest interval that I have found is one minute. For just a ping status, that should be good enough. The problem as I see it is that you are going to continuously write to the same D:\ServerPing.tct file. If you don't periodically clear out that file, it is just going to grow and grow and grow and trying to read it to determine when a machine was down is going to be challenging. It is just going to be filled with ping output. 

    A better choice would be to use Powershell to monitor the ping status and only write to the log when the status changes. And write to a log file whose name contains the day/week/month so that you can purge old files, and if you are interested in a given timeframe,you can find the info.

    Here is a Powershell script that I had that does just that. Add in your server names, Adjust the log file location, and save it a a .ps1 file. Run it interactively for a while to see what it does. Check the log file. Monitor a test server that you can shut down to see what gets logged. Then on your monitor server create a task that runs at system startup and launched the script. It will run continuously.   

    $names = 'server1','server2'              # Put your server names here. 
    $logFile = 'C:\Logs\Monitor' +  (Get-Date -Format 'yyyy-MM') + '.log'        # your monthly log name
    $servers = @()                          # collection of servers and their status
    foreach ($name in $names){              # populate the initial collection  
        $servers +=     [PSCustomObject]@{
            ComputerName = $name
            Online       = $null 
        }
    }
    "{0} ServerMonitor Starting" -f (get-date)                                  # write to the screen if interactive
    "{0} Logging to {1}" -f (get-date), $logFile                                # write to the screen if interactive
    "{0} ServerMonitor Starting" -f (get-date) | Out-File $logFile -Append      # add to the log file           
    while ($true) {                                                             # run forever, assume that we have been launched at system startup
        foreach ($server in $servers) {
            $ping = Test-NetConnection -ComputerName $server.Computername   -ErrorAction SilentlyContinue
            if (($ping.pingsucceeded -eq $true) -and ($server.online -eq $true)) {
                # No need to do anything, the server is up and it was up during the last execution.
                # Uncomment the next statement to write this event to the log file.
                # "{0} {1} is still up." -f (get-date), $server.computername | Out-File $logFile -Append         
                "{0} {1} is still up." -f (get-date), $server.computername         
            }
            elseif (($ping.pingsucceeded -eq $false)  -and ($server.online -eq $false)) {
                # During our last check, the server did not ping, but now it does.
                # Log the event and change the status.
                #"{0} {1} is still down." -f (get-date), $server.computername  | Out-File $logFile -Append
                "{0} {1} is still down." -f (get-date), $server.computername  
            }
            elseif (($ping.pingsucceeded -eq $true) -and ($server.online -eq $false)) {
                # During our last check, the server did not ping, but now it does.
                # Log the event and change the status.
                "{0} {1} is now up." -f (get-date), $server.computername  | Out-File $logFile -Append
                "{0} {1} is now up." -f (get-date), $server.computername  
                $server.online = $true                        # update our status 
            }
            elseif ($ping.pingsucceeded -eq $true) {
                $server.online = $true
                "{0} {1} is now up." -f (get-date), $server.computername  | Out-File $logFile -Append
                "{0} {1} is now up." -f (get-date), $server.computername  
                $server.online = $true                       # update our status 
            }
            elseif ($ping.pingsucceeded -eq $false) {
                "{0} {1} is now down." -f (get-date), $server.computername  | Out-File $logFile -Append
                "{0} {1} is now down" -f (get-date), $server.computername  
                $server.online = $false                      # update our status 
            }
            else {
                "{0} Testing {1}, but something is wrong. We should not get to this statement. " -f (get-date), $server.computername
                "{0} Testing {1}, but something is wrong. We should not get to this statement. " -f (get-date), $server.computername  | Out-File $logFile -Append  
            }  
        } 
        start-sleep -Seconds 30 
    } 

     
    • Edited by MotoX80 Friday, July 17, 2020 1:03 AM
    Friday, July 17, 2020 1:02 AM
  • Hi ,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,

    Candy


    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com   

    Monday, July 20, 2020 1:59 AM
  • Hi ,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,

    Candy


    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com   

    thanks reply ,

    It seems too complicate , I just want a simple non-stop job , like a ping command is running , is there other easy way to do that ?

    thanks

    Thursday, July 30, 2020 1:40 PM
  • So just schedule the task to run every minute. 

    Add a timeout if you want 2 tests each minute.

    ping Server1 >>D:\ServerPing.txt
    ping Server2 >>D:\ServerPing.txt
    net time >> ServerPing.txt
    
    timeout /t 30
    
    ping Server1 >>D:\ServerPing.txt
    ping Server2 >>D:\ServerPing.txt
    net time >> ServerPing.txt

    • Edited by MotoX80 Thursday, July 30, 2020 10:59 PM
    Thursday, July 30, 2020 10:53 PM
  • So just schedule the task to run every minute. 

    Add a timeout if you want 2 tests each minute.

    ping Server1 >>D:\ServerPing.txt
    ping Server2 >>D:\ServerPing.txt
    net time >> ServerPing.txt
    
    timeout /t 30
    
    ping Server1 >>D:\ServerPing.txt
    ping Server2 >>D:\ServerPing.txt
    net time >> ServerPing.txt

    windows scheduler can be setup a task to run every minute ? I found the least time interval is not in hourly base?

    how to setup a task in every minute ?

    thanks

    Friday, July 31, 2020 1:03 AM