Batch script that checks windows services and creates .csv output works when manually initiated, but not when initiated via Windows Task Scheduler? - windows

My goal is to have a batch script that checks windows services to see if they're running and output a .csv report. I also want this batch script to run hourly via Windows Task Scheduler.
Here is a script I was given to and am using for the example:
type null > servicecheckupreport.csv
sc query "AdobeARMservice" >> servicecheckupreport.csv
sc query "aspnet_state" >> servicecheckupreport.csv
sc query "AdobeFlashPlayerUpdateSvc" >> servicecheckupreport.csv
I am checking three (example) services, AdobeARMservice, aspnet_state and AdobeFlashPlayerUpdateSvc.
The batch works fine when you manually execute it, however when I successfully connect it to Windows Task Scheduler using the "Create Basic Task" feature, the batch script runs, but doesn't generate the .csv report. I can see the command prompt come up for a little bit, goes away and then that's it.

Related

Unable to run a windows task that makes https request under SYSTEM account

I have created a windows cmd file that calls three independent bat files. I want to create a windows task that calls this cmd file and runs every 5 minutes. The problem is that this task runs perfectly fine only when I'm logged into the system. But I'm unable to make this task continue to run "whether I'm logged in or not".
I even asked my colleague to login to that machine and run this task under his account - it worked. I created a local admin user on that machine, logged in as that user, tried to run this task - it did not work - the script waits forever while post_results.bat. I even tried to schedule a jenkins job that basically does the same thing - it did not work - the jenkins job waits forever while post_results.bat (I killed the jenkins job after waiting for ~20 min).
Here is a summary of what these tasks are doing:
run_all.cmd
call "run_test.bat"
call "post_results.bat"
call "clean.bat"
run_test.bat - executes a jmeter script
C:\Users\Administrator\LS2\apache-jmeter-4.0\bin\jmeter -n -t api_strategy_synthetic_tests.jmx -JTestEnv=amer1 -l Result_log.jtl
post_results.bat - calls a python script that posts the jmeter test results to datadog
python post_jmeter_results_to_datadog.py Result_log.jtl
post_jmeter_results_to_datadog.py - uses the datadog python api to post metrics to datadog
#!/usr/bin/env python3
import sys
import pandas as pd
from datadog import initialize, api
options = {
'api_key': <API_KEY>,
'app_key': <APPLICATION_KEY>
}
initialize(**options)
jtl_file = sys.argv[1]
df = pd.read_csv(jtl_file)
for index, row in df.iterrows():
tag = "success:" + str(row['success'])
api.Metric.send(
metric=row['label'],
points=[(row['timeStamp']/1000,row['elapsed'])],
tags=[tag]
)
clean.bat - deletes the jmeter test result files
rmdir /s /q "errors"
del "jmeter.log"
del "Result_log.jtl"
All I need is to be able to run this task every 5 minutes. If anyone is able to see what I'm doing wrong and points that out... I'd be really grateful.
You can create one PowerShell script to execute your Batch scripts remotely.
And Even you can schedule your PowerShell script using Windows Task Scheduler which will run as per your settings.

Task scheduler is not executing Batch call to VBS script but it works fine when executed manually

We are running a Batch script call "C:\My Scripts\Script.vbs" which when run manually, executes the vbscript perfectly. However when setting up a scheduled task, it works some times, but most times it never completes and we have set the task to forcibly stop after 4 hours - which does not seem to work either. The task is set to run with administrator privileges and we can see in the task history that the task is running when it is set to run but it does not seem to ever execute the script properly. Any recommendations and help you can provide are much appreciated.
Update: It will run when set to run when user is logged on but will not run when set to run where user is logged on or not

Windows Task Scheduler Return Code 2147942511

I've defined something to run on Task Scheduler and I'm always getting the return code 2147942511 on the History log. It just gives me an empty log of the batch script I defined to be executed.
When I try to run the batch script manually, it works perfectly well.
Scheduled Task Info:
OS is Windows 2012 R2. I'm using an account with logon as batch job, task is set to execute with highest privileges, execute even user is not logged on, do not store password is ticked.
2147942511 is 0x8007006f or ERROR_BUFFER_OVERFLOW which according to MSDN has message "The file name is too long." Have you tried shortening the path of your batch script file?
Source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

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.

Calling cscript.exe and passing a vbscript to call

I have scheduled a task in windows server 2008R2 ..I want to run a VBScript so when I setup the task I call the cscript at C:\Windows\System32\cscript.exe and in the arguments section I am passing //nologo //B d:\main\programs\copy.vbs /targets:contents but it is not executing my script ..If I call my script directly in the start program section it works fine but it's not working if I call CSrcipt and pass in arguments the status changes to queued but nothing happens after that..Can someone tell me what I am doing wrong here.
Also another question I have is that can we run 2 programs one after another in one task like when one script is finished I would like to start another script .
Thanks
Put this line at the top of your script and try again:
CreateObject("WScript.Shell").LogEvent 4, "Script running"
Unless you get an Information event with source WSH and event-ID 4, your script isn't running at all. Check the eventlog and the task's History tab for clues as to why that is. Also check the permissions of the script. Is the runas account of the task able to access/run the file? You can check that by starting a CMD instance as that user
runas /user:DOM\USER cmd
and then trying to run the script in that CMD instance.
Also double-check the task settings. As which user is it configured to run? With the user logged on or not logged on? Is "Run with highest privileges" enabled (in case UAC is enabled on your server)?
If the script does produce the abovementioned event that means it's running in principle, but something is going wrong in the process. You need to debug your script.

Resources