Skip to main content

πŸ“˜ ** Exit Status Code = 143 in Bash**


🎯 What You Will Learn​

  1. Understand what exit status code = 143 means in Bash.
  2. Learn how SIGTERM (signal 15) works β€” the graceful termination signal.
  3. Differentiate 143 from similar codes: 130 (SIGINT) and 137 (SIGKILL).
  4. Detect when processes are ended normally by system shutdown, Docker stop, or admin command.
  5. Handle and trap SIGTERM safely inside scripts.
  6. Apply best practices in WordPress VPS automation, ensuring clean exits during restarts, backups, or container stops.

1. 5W + 1H Framework​

ElementDescription
WhatExit code 143 means the process was terminated gracefully via SIGTERM (signal 15).
WhySIGTERM is sent to stop a process politely, giving it a chance to clean up before exiting.
WhoUsed by systemd, Docker, Kubernetes, and admins when stopping services or automation scripts.
WhenWhen a process receives SIGTERM (kill, docker stop, systemctl stop) instead of abrupt SIGKILL.
WhereCommon in background jobs, cron, Docker containers, or managed WordPress VPS tasks.
HowBash computes 128 + signal_number β†’ 128 + 15 = 143.

2. Prerequisites​

Before starting, you should:

  • Understand signals in Bash (SIGINT, SIGTERM, SIGKILL).
  • Know how to use commands like kill, ps, and trap.
  • Be familiar with background processes (&) and service management (systemctl, docker stop).
  • Have access to a Linux VPS environment.

3. Core Syntax and Examples​

🧩 Definition​

Exit code 143 = process terminated by SIGTERM (signal 15) β†’ graceful shutdown signal.​

Example 1 – Sending SIGTERM with kill​

sleep 60 &
kill -15 $!
wait $!
echo $?

Expected Output:

143

Explanation: Process was found and asked to terminate (SIGTERM), not forced. Bash reports exit code 143 = 128 + 15.​

Example 2 – Graceful Docker Stop​

docker run --name test -d ubuntu sleep 300
docker stop test
docker inspect test --format='{{.State.ExitCode}}'

Expected Output:

143

Explanation: Docker sends SIGTERM to the container before SIGKILL (after timeout). Container exits gracefully β†’ exit code 143. Use Case (WordPress VPS): Gracefully stopping a running PHP-FPM or Redis container.​

Example 3 – systemd Service Stop​

systemctl stop php8.3-fpm
echo $?

Expected Output (in logs):

php8.3-fpm[PID]: received SIGTERM, shutting down

Explanation: systemd uses SIGTERM to stop services like PHP-FPM or MySQL gracefully. The exit code is reported as 143. Use Case: When rebooting a VPS or restarting the stack with systemctl restart lsws.​

Example 4 – Handling SIGTERM in Script​

#!/bin/bash
trap "echo 'Graceful stop detected'; exit 143" SIGTERM
echo "Running backup..."
sleep 60
echo "Backup completed."

Run it:

bash safe_exit.sh &
kill -15 $!
wait $!
echo $?

Expected Output:

Running backup...
Graceful stop detected
143

Explanation: The script traps SIGTERM, performs cleanup, and exits with 143 intentionally. Use Case: Gracefully stop WordPress backup jobs or WP-CLI updates during system shutdown.​

Example 5 – System Sending SIGTERM Before Reboot​

sudo reboot

Expected in logs:

systemd[1]: Stopping php8.3-fpm.service...
php-fpm[PID]: received SIGTERM, exiting gracefully

Explanation: During reboot, systemd sends SIGTERM to running processes. They log exit 143 as part of controlled shutdown.​

4. Common Scenarios Triggering Exit Code 143​

ScenarioExampleDescription
Manual stopkill PID or kill -15 PIDUser requests graceful stop
System service stopsystemctl stop mysqlsystemd sends SIGTERM
Docker container stoppeddocker stopDocker sends SIGTERM before kill
VPS rebootreboot or shutdownOS sends SIGTERM to all
Cron cleanupScript terminates itself cleanlyUses exit 143
Monitoring agent shutdownservice telegraf stopSIGTERM issued to daemon

5. WordPress VPS Use Cases​

Use CaseExampleExplanation
Graceful PHP-FPM stopsystemctl stop php8.3-fpmEnds requests safely
WP container shutdowndocker stop wp-siteGraceful exit of web app
Cron job interruptedAdmin sends killScript exits 143
Redis cache stopsystemctl stop redis-serverSIGTERM sent for safe data flush
WP backup abortkill -15 PIDBackup stops safely, not corrupted
LiteSpeed reloadsystemctl reload lswsWorkers gracefully end connections

6. Troubleshooting Matrix​

SymptomLikely CauseDiagnostic CommandFix
Exit 143 loggedGraceful termination (SIGTERM)journalctl -xeExpected normal behavior
Process ends mid-taskAdmin stop or rebootlast -xRe-run job after reboot
Docker container exits 143Container stop issueddocker logs <container>Normal; verify graceful shutdown logs
Script halts unexpectedlyReceived SIGTERMtrap handling missingAdd cleanup trap
WP tasks stop on cron overlapDuplicate executionAdd lockfile mechanism

7. Best Practices​

  1. Handle SIGTERM gracefully:
trap "echo 'SIGTERM received, exiting cleanly...'; exit 143" SIGTERM

  1. Use traps for cleanup:
  • Delete temp files.
  • Close connections.
  • Log event before exit.
  1. Avoid force-killing processes: Use kill or systemctl stop instead of kill -9.
  2. For containers: Always let Docker send SIGTERM before SIGKILL (default 10s timeout). Extend timeout if needed:
docker stop -t 30 wp-site

  1. Automate logging of exits:
echo "$(date): Process exited with code $?" >> /var/log/exit_status.log

  1. Use lock files in scripts to prevent multiple overlapping runs.

8. Quick Lab – Simulate Exit Code 143​

Step 1: Create a test script​

nano /root/exit143_test.sh

Step 2: Insert​

#!/bin/bash
trap "echo 'SIGTERM received, cleaning up...'; exit 143" SIGTERM
echo "Starting task..."
sleep 60
echo "Finished task."

Step 3: Run in background​

bash /root/exit143_test.sh &
pid=$!

Step 4: Send SIGTERM​

kill -15 $pid
wait $pid
echo $?

Expected Output:

Starting task...
SIGTERM received, cleaning up...
143

Explanation: Script exited gracefully when SIGTERM was received β€” exit code 143 confirms safe shutdown.​

9. Cheat Sheet​

Exit CodeMeaningDescription
0SuccessNormal completion
1General errorNon-specific failure
2Misuse of shell builtinsSyntax or structure error
126Command cannot executePermission or binary issue
127Command not foundMissing binary or path
130Terminated by SIGINTManual interrupt (Ctrl+C)
137Killed by SIGKILLForceful kill or OOM
139Segmentation faultMemory access violation
143Terminated by SIGTERMGraceful termination

10. Mini-Quiz​

  1. What signal number corresponds to exit code 143?
  2. What’s the difference between SIGTERM and SIGKILL?
  3. Which system command sends SIGTERM to stop services gracefully?
  4. How can you handle SIGTERM in a Bash script?
  5. Why is exit code 143 considered a β€œnormal” stop rather than an error?