All Products
Search
Document Center

Microservices Engine:Graceful shutdown for scheduled jobs

Last Updated:Feb 28, 2026

When you restart or redeploy an application that uses SchedulerX, running scheduled jobs can be interrupted, which may cause incomplete data processing. Graceful shutdown lets running jobs finish before the application process exits.

Graceful shutdown diagram

image

Prerequisites

The SchedulerX client version must be 1.10.8 or later.

Configure graceful shutdown

SchedulerX supports graceful shutdown for jobs in standalone mode and distributed mode. Unlike real-time online services, offline scheduled jobs may take a long time to complete. Set a timeout to prevent application processes from waiting indefinitely.

Add the following properties to your Spring Boot configuration:

# The graceful shutdown mode. WAIT_ALL: Wait for all jobs to complete. WAIT_RUNNING: Wait for running jobs to complete.
# If you do not configure this parameter, the graceful shutdown feature is disabled. By default, the graceful shutdown feature is disabled.
spring.schedulerx2.graceShutdownMode=WAIT_ALL
# The timeout period for graceful shutdown. Unit: seconds. By default, no timeout period for graceful shutdown is specified.
spring.schedulerx2.graceShutdownTimeout=10

# Specifies whether to enable the port required by the HTTP service. Default value: false.
spring.schedulerx2.httpServerEnable=true
# The port over which you can call the shutdown-purposed HTTP interface. Default port: 51886.
spring.schedulerx2.httpServerPort=52333

Shutdown modes

Mode

Behavior

WAIT_ALL (recommended)

The application exits only after all jobs and tasks in the application complete.

WAIT_RUNNING

The application exits after running jobs and tasks to which threads are allocated complete. Jobs and tasks in queue are dropped.

Trigger graceful shutdown

Use one of the following three methods to trigger graceful shutdown.

Method 1: Send a SIGTERM signal (kill -15)

SchedulerX SDKs register a JVM shutdown hook that triggers graceful shutdown automatically. Send a SIGTERM signal to the application process:

kill -15 <Process ID>

The process waits for running jobs to complete based on the configured shutdown mode and timeout, then exits.

Important

Do not run kill -9 directly. This signal forces immediate termination and bypasses graceful shutdown. Instead, run kill -15 first, monitor the application for a period, and then run kill -9 only if the process has not exited. Set an appropriate graceful shutdown timeout based on your job characteristics to prevent an extremely slow release process.

Method 2: Use the Spring Boot shutdown event

If your application uses Spring Boot with SchedulerX, use the Spring Boot Actuator to trigger graceful shutdown through the Spring container shutdown event.

  1. Add the Actuator dependency to your pom.xml file:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. Enable the shutdown endpoint:

management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=shutdown
  1. Call the shutdown endpoint:

curl -X GET http://NodeIP:Port/actuator/shutdown

Method 3: Use the SchedulerX HTTP interface

Enable the SchedulerX built-in HTTP server to accept shutdown requests from external systems.

  1. Add the following configuration:

# Enable the HTTP service interface. Default: false.
spring.schedulerx2.httpServerEnable=true
# HTTP shutdown interface port. Default: 51886.
spring.schedulerx2.httpServerPort=51886

Call the following HTTP endpoint to trigger a graceful shutdown:

curl -X GET http://NodeIP:51886/schedulerx/worker/shutdown

Integrate graceful shutdown into deployment workflows

Integrate graceful shutdown into your operations and maintenance (O&M) release processes to prevent scheduled jobs from being interrupted during application restarts.

Integrate into a self-built continuous delivery (CD) process

A typical CD pipeline includes a step to stop the application process. Include the graceful shutdown logic in your stop script. Use any of the three trigger methods based on your requirements.

Sample CD process:

image

The following script demonstrates how to stop an application process with graceful shutdown:

# After you start the application, the process ID is written into the app.pid file.
PID="{Application deployment path}/app.pid"
FORCE=1
if [ -f ${PID} ]; then
  TARGET_PID=`cat ${PID}`
  kill -15 ${TARGET_PID}
  loop=1
  while(( $loop<=5 ))
  do
    # health: Specifies whether the current application process ends. You can configure the health field based on your business requirements.
    health
    if [ $? == 0 ]; then
      echo "check $loop times, current app has not stop yet."
      sleep 5s
      let "loop++"
    else
      FORCE=0
      break
    fi
  done
  if [ $FORCE -eq 1 ]; then
  	echo "App(pid:${TARGET_PID}) stop timeout, forced termination."
    kill -9 ${TARGET_PID}
  if
  rm -rf ${PID}
  echo "App(pid:${TARGET_PID}) stopped successful."
fi

Integrate into a Kubernetes PreStop hook

Use the Kubernetes PreStop lifecycle hook to trigger graceful shutdown before a pod is terminated. Two approaches are available: exec scripts and HTTP requests.

Exec script approach

Run a stop script or send a SIGTERM signal directly:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        lifecycle:
          preStop:
            exec:
              # command: ["/bin/sh", "-c", "kill -15 PID && sleep 30"]
              command: ["/bin/sh", "-c", "Script path /stop.sh"]

HTTP request approach

Call the shutdown endpoint through an HTTP GET request:

  • For Spring Boot applications with Actuator enabled, set the path to /actuator/shutdown.

  • For non-Spring applications, enable the SchedulerX HTTP server and set the path to /schedulerx/worker/shutdown.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest
        lifecycle:
          preStop:
            httpGet:
              path: /schedulerx/worker/shutdown
              port: 51886
              scheme: HTTP

Integrate with Alibaba Cloud deployment platforms

If you deploy through Enterprise Distributed Application Service (EDAS) or through Microservices Engine (MSE) with Container Service for Kubernetes (ACK), the platform automatically integrates SchedulerX graceful shutdown after you enable the feature. For more information, see Gracefully shut down Spring Cloud applications and Graceful shutdown.