Automated SAP Datasphere TaskChains with SAP CLI, Batch and Windows Scheduler
Share

Introduction

In large SAP landscapes, Task Chains often run repeatedly and require monitoring. Manual supervision can therefore be time- and cost-consuming. By combining the SAP CLI, batch scripting, and Windows Task Scheduler, you can fully automate Task Chain execution, monitoring, and error handling.

Use Case: This automation approach is particularly suitable for Task Chains that may fail due to missing responses from source systems or because of long runtime/response times. In these cases, a simple automatic restart can resolve the issue. This method is not recommended for all Task Chains, as persistent errors may indicate a deeper problem that cannot be resolved by a restart.

Before continuing with the step-by-step guide on how to set up your own automated SAP Datasphere Task Chain, I will briefly describe the architecture of the automated SAP Datasphere Task Chain process.

Archtiecture Overview

genevieve_27_0-1773830587647.png

The Windows Server hosts the Task Scheduler and the Batch Script. We need the the Task Scheduler to execute the Batch Script at configured intervals. The Batch Script contains logic to check the Task Chain status, writes logs and restart the TaskChain if needed. With the SAP Datasphere Command Line Interface (CLI) we have the possibility to acces our TaskChains via OAuth authentifaction. SAP Datasphere CLI acts as an interface between the Batch Script and SAP Datasphere Task Chain.

With this approach the TaskChains in SAP Datasphere are executed and monitored automatically.

Step-by-Step Guide

Install SAP Datasphere CLI

To control your Task Chain over the CLI the following prerequisites should be fulfilled:

  1. Install Node.js on Windows  ( https://nodejs.org/en/download/ )
  2. Install SAP Datapshere CLI globally with the following command

                            npm install -g @sap/datasphere-cli

      3. To check if the installation was sucessful, run:

                            datasphere –version

Make sure, that Node.js is correctly in your System Path , so the CLI Command is recognized.

Connect CLI to Datapshere Tenant

Configure CLI and log on to your SAP Datasphere Tenant  

datasphere config host set https://yourdataspheretenant.eu10.hcs.cloud.sap

datasphere login

 

To log into the Command Line Interface a OAuth2.0 Client with an interactive usage purpose is required. If you have an administrator role you can create OAuth2.0 Client by yourself. Follow the instructions in the SAP Help Portal “Create an OAuth2.0 Client with an Interactive Usage Purpose | SAP Help Portal”

After creating the client, log in with your Client ID and Secret. If the login is successful a confirmation window will appear. Afterwards you won’t need to repeat the log in for 720 hours.

genevieve_27_1-1773830587648.jpeg

 

 

 

Test Task Chain Commands

Below are some commands you can test. Make sure they return the excpected results before proceeding with the automation of the Task Chain.

  1. Call the Stauts of a Task Chain:

              datasphere tasks logs get –space TRAINING_SPACE_1 –log-id 10183

       2. List all Logs of a Task Chain:

            datasphere tasks logs list –space TRAINING_SPACE_1 –objectname DEMO_TASK_CHAIN_CLI

      3. Execute a Task Chain manually:

            datasphere tasks chains run –space TRAINING_SPACE_1 –object DEMO_TASK_CHAIN_CLI

genevieve_27_2-1773830587649.jpeg

      4. You can list all available commands with the follwing command

            datasphere –h

genevieve_27_3-1773830587651.png

Create a Batch File

After setting up the conection to SAP Datasphere through SAP CLI we will create a Batch Script not only to automate and monitor our task chains but also to restart task chains if the status is FAILED.

You can use the following script as a template:

@echooff

setlocal enabledelayedexpansion

set SPACE_ID=technical_name_of_your_space_ID

set TASKCHAIN_NAME=DEMO_TASK_CHAIN_CLI

set LOGFILE=C:Users…Logstaskchain_watch.log

 

if not exist “C:Users…Logs” (

    mkdir “C:Users…Logs”

)

 

REM determine last log ID

for /f “tokens=2 delims=:” %%a in (‘

  datasphere tasks logs list –space %SPACE_ID% –objectname %TASKCHAIN_NAME% ^| findstr /i “logId”

‘) do (

  set LOG_ID=%%a

  set LOG_ID=!LOG_ID:,=!

  goto GET_STATUS

)

 

REM Check Status of Task Chain

:GET_STATUS

for /f %%a in (‘

  datasphere tasks logs get –space %SPACE_ID% –log-id %LOG_ID%

‘) do (

  set STATUS=%%a

)

 

set STATUS=!STATUS:”=!

 

REM Neustart bei FAILED

if /i “!STATUS!”==”FAILED” (

    datasphere tasks chains run –space %SPACE_ID% –object %TASKCHAIN_NAME%

    echo %date% %time% FAILED erkannt – Task Chain neu gestartet >> “%LOGFILE%”

    goto :EOF

)

 

echo %date% %time% Status OK (!STATUS!) >> “%LOGFILE%”

 

Explanation:

  • @echooff  – suppresses display of commands
  • set –  definition of variables
  • for –  processing outputs in loops
  • if –  performs conditional checks
  • goto –  jumps to a defined script marker

Automate with Windows Task Scheduler

Now we move on to the automation part.

  1. Open Task Scheduler and click on “Create Task”
  2. On the General Tab give a name and Description  and also check “Only run, if usesr is logged” button

genevieve_27_4-1773830587653.jpeg

      3. On the Trigger Tab set up your individual Schedule (daily, weekly, monthly, etc.)

      4. On the Actions tab select your batch script 

After setting up your scheduled task make sure that logs are generated and the Task Chain restart by the Status FAILED.

genevieve_27_5-1773830587654.png

 

 

Conclusion/Summary

By combining SAP CLI, Batch Script, and Windows Task Scheduler, you can now execute and monitor Task Chains automatically. Failures are detected and resolved by restarting the Task Chain, and logs are generated for traceability.

Optional enhancements:

  • Add email notifications for FAILED Task Chains
  • Monitor multiple Task Chains simultaneously
  • Centralize logs for team access
  • Limit the number of automatic restarts (e.g., max 3 retries) to identify persistent issues that require manual intervention

 

  Read More Technology Blog Posts by Members articles 

#abap

By ali