Skip to Content
Author's profile photo Karl-Heinz Hochmuth

How to use Windows Task Scheduler with SAP instances?

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.

Prerequisites

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

Procedure

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

  1. Open Windows Task Scheduler (you find it in “Administrative Tools”) and create a new task:
  2. Enter a name for the task and add a description:
  3. Set the task to trigger weekly:
  4. Select the day and time:
  5. Select “Start a program”:
  6. Enter the full path to the batch script:
  7. 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.
  8. 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:
  9. 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!
  10. If you want to test this task, select the Task and “Run”:
  11. Inspect your log in C:\SAPbatchjob\StopInstance.log. You can also scroll to the left to “Last Run Result” tab:
  12. 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:

→ Finished!

 

Everything should run smoothly now. However, if you are still having some problems, have a look at the following Questions & Answers.

Troubleshooting

Question:
My instance didn’t start! There is no data written to C:\SAPbatchjob\StartInstance.log.

Answer:
Check error message in Task Scheduler tool. Check credentials used to run this task. Maybe somebody changed the password for this user?

 

Question:
Can I use SAPService<SID> user to start or stop an SAP instance?

Answer:
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!

 

Question:
My domain user <SID>adm get’s locked out all the time after I have changed its password. What is the reason for this?

Answer:
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!

 

Question:
Can I create, start or stop Windows tasks in an command-prompt?

Answer:
Yes, the command “schtasks” contains all necessary functions.

Example:

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”.

 

 

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kavi Gunness
      Kavi Gunness

      Is it recommended to use cmd to stop/start SAP or PowerShell is more convenient?

       

      Do you have any solution to rotate the stop/start sap logs?

      For ex, every time the log files reach 1MB

      Author's profile photo Karl-Heinz Hochmuth
      Karl-Heinz Hochmuth
      Blog Post Author

      Hello Kavi,

      you can use either cmd.exe or powershell.exe to start a SAP instance using sapcontrol.exe. I usually use PowerShell.

       

      What do you mean with "rotate" the stop/start sap logs? Do you mean the dev_trace files in \work folder of a SAP instance? They will only overwritten if you stop/start an instance again (the current files will be kept with .old extension).

      Best regards,

      Kalle