In this blog, I want to describe how to start or stop an SAP instance via a scheduled Windows task. The Windows Task Scheduler is a mighty tool to trigger (start) a task based on a timestamp or a special event. Special events can be the point in time, when Windows starts up, when a certain user logs on or if Windows creates a specific event in Event Viewer.
The following use case is just an example how to use Windows scheduled tasks in conjunction to SAP applications.
The use case
We want to start an ABAP application server instance on a specific Windows host to cover the weekly batch job processing load during weekend. This additional application server should be only online from Friday, 22:00 to Monday, 6:00.
We already have a single ABAP instance installed (SAP SID = BER, System No. is 0) and running on a single Windows host (wsiv1046):
This instance can be started or stopped via SAP MMC or via sapcontrol.exe.
Start SAP instance:
sapcontrol -prot PIPE -nr 00 -function StartWait 300 10
Stop SAP instance:
sapcontrol -prot PIPE -nr 00 -function StopWait 300 10
Let’s create two batch scripts which contain the two commands above plus a simple log writer to get trace information for error tracking.
- Batchscript to start SAP instance:
@echo off echo. >> C:\SAPbatchjob\StopInstance.log echo Script started: >> C:\SAPbatchjob\StopInstance.log echo %date% %time% >> C:\SAPbatchjob\StopInstance.log echo hostname is: >> C:\SAPbatchjob\StopInstance.log echo %computername% >> C:\SAPbatchjob\StopInstance.log echo. >> C:\SAPbatchjob\StopInstance.log C:\usr\sap\BER\D00\exe\sapcontrol.exe -prot PIPE -nr 00 -function StopWait 300 10 >> C:\SAPbatchjob\StopInstance.log if %errorlevel%==0 goto end echo Error in processing ... check Windows Application log for additional information. >> C:\SAPbatchjob\StopInstance.log :end echo. >> C:\SAPbatchjob\StopInstance.log echo Script end. >> C:\SAPbatchjob\StopInstance.log
Batchscript to stop SAP instance: @echo off echo. >> C:\SAPbatchjob\StartInstance.log echo Script started: >> C:\SAPbatchjob\StartInstance.log echo %date% %time% >> C:\SAPbatchjob\StartInstance.log echo hostname is: >> C:\SAPbatchjob\StartInstance.log echo %computername% >> C:\SAPbatchjob\StartInstance.log echo. >> C:\SAPbatchjob\StartInstance.log C:\usr\sap\BER\D00\exe\sapcontrol.exe -prot PIPE -nr 00 -function StartWait 300 10 >> C:\SAPbatchjob\StartInstance.log if %errorlevel%==0 goto end echo Error in processing ... check Windows Application log for additional information. >> C:\SAPbatchjob\StartInstance.log :end echo. >> C:\SAPbatchjob\StartInstance.log echo Script end. >> C:\SAPbatchjob\StartInstance.log
Now we’re ready to create two Windows tasks.
Create the Task to start the instance
- Open Windows Task Scheduler (you find it in “Administrative Tools”) and create a new task:
- Enter a name for the task and add a description:
- Set the task to trigger weekly:
- Select the day and time:
- Select “Start a program”:
- Enter the full path to the batch script:
- Click “Finish” on the summary screen:→ You have successfully created a Windows task:But the task runs under the wrong credentials. We must change the credentials to use <SID>adm user.
- Select “Properties” and click the “Change User or Group…” button.
In this example, the batch script should run under the credentials of <domain>\<SID>adm, here it is nt5\beradm:
- Make sure to select “Run whether user is logged on or not”.
Only then you will be prompted to enter the password for <SID>adm user!
- If you want to test this task, select the Task and “Run”:
- Inspect your log in C:\SAPbatchjob\StopInstance.log. You can also scroll to the left to “Last Run Result” tab:
- In case the task cannot run, for example because of wrong user credentials, then you see an OS error code here.→ You have successfully created the start task for an ABAP application server instance.
Create the Task to stop the instance
Create the second Task to stop the instance each Monday, at 6:00 AM. Use the steps above but this time, select the StopInstance.cmd script.
→ You should have two tasks in Windows Task Scheduler:
Everything should run smoothly now. However, if you are still having some problems, have a look at the following Questions & Answers.
My instance didn’t start! There is no data written to C:\SAPbatchjob\StartInstance.log.
Check error message in Task Scheduler tool. Check credentials used to run this task. Maybe somebody changed the password for this user?
Can I use SAPService<SID> user to start or stop an SAP instance?
No, because SAPService<SID> user has no rights to start or stop Windows tasks. I do not recomend to add the required permissions to this user, because you would change the default security settings of SAPService<SID> user.
Keep in mind, that an SAP instance runs under the credentials of SAPService<SID>, by default!
My domain user <SID>adm get’s locked out all the time after I have changed its password. What is the reason for this?
You have a Windows task somewhere on another Windows host running, which still uses the old password! Be very careful with password changes of an SAP system or single instances!
Can I create, start or stop Windows tasks in an command-prompt?
Yes, the command “schtasks” contains all necessary functions.
C:\SAPbatchjob>schtasks /run /tn “stop sap instance BER each monday at 6 am”
SUCCESS: Attempted to run the scheduled task “stop sap instance BER each monday at 6 am”.