π ** Exit Status Code = 255 in Bash**
π― What You Will Learnβ
- Understand what exit status code = 255 means in Bash.
- Learn how it represents abnormal termination or out-of-range exit codes.
- Identify when Bash or other programs (SSH, rsync, etc.) return
255. - Differentiate between script-level errors, network/SSH failures, and restricted exit values.
- Apply 255 handling in WordPress VPS automation, remote backups, and deployment pipelines.
- Learn to prevent and debug
exit 255in real-world VPS contexts.
1. 5W + 1H Frameworkβ
| Element | Description |
| What | Exit code 255 indicates abnormal termination β often caused by invalid exit values, failed SSH connections, permission issues, or missing targets in remote commands. |
| Why | Bash restricts exit codes to 0β255. If a script calls exit with a value outside that range, it wraps around or becomes 255. SSH and network tools also use 255 for unreachable hosts or authentication failures. |
| Who | System admins, Bash scripters, and DevOps engineers managing remote tasks (SSH, rsync, scp, WP automation). |
| When | When a command, SSH session, or script fails to execute properly or terminates unexpectedly. |
| Where | Common in cron jobs, deployment pipelines, backup transfers, and cross-server WordPress management scripts. |
| How | Bash and many other tools (OpenSSH, rsync, scp) internally use 255 to signal fatal errors or unreachable destinations. |
2. Prerequisitesβ
You should already understand:
- Exit codes and their ranges (0β255).
- SSH, rsync, and remote Bash command execution.
- Common VPS tools like
scp,rsync, andssh. - How to use
$?to inspect command return values.
3. Core Syntax and Examplesβ
π§© Definitionβ
Exit code 255 = βAbnormal terminationβ β used for unreachable SSH connections, invalid exit values, or unrecoverable execution errors.β
Example 1 β SSH Connection Failureβ
ssh root@unknown-host
echo $?
Expected Output:
ssh: Could not resolve hostname unknown-host: Name or service not known
255
Explanation: The SSH client couldnβt connect (DNS or network error). Exit 255 signals a fatal SSH failure. Use Case (WordPress VPS): Remote deployment script tries to connect to an offline backup server.β
Example 2 β SSH Permission Deniedβ
echo $?
Expected Output:
Permission denied (publickey,password).
255
Explanation: Authentication failed. SSH client exits with code 255, signaling fatal access error. Fix:
ssh-copy-id [email protected]
Example 3 β Script Exiting with Invalid Codeβ
#!/bin/bash
exit 999
echo $?
Expected Output:
255
Explanation:
Exit code limit is 0β255.
Anything above 255 wraps or is reported as 255 (out-of-range exit).
Use Case:
Bad exit control in complex WP maintenance scripts (e.g., exit $status where $status > 255).β
Example 4 β rsync or scp Failureβ
echo $?
Expected Output (if network unreachable):
ssh: connect to host 10.0.0.5 port 22: Connection timed out
255
Explanation: Remote server unreachable β rsync/ssh fails β exit 255. Use Case: Automated WordPress backup via rsync fails when remote VPS is offline.β
Example 5 β Command Not Executable via SSHβ
ssh user@remote '/root/unknown_script.sh'
echo $?
Expected Output:
bash: /root/unknown_script.sh: No such file or directory
255
Explanation: SSH connects, but the remote script is missing or non-executable. SSH returns 255 to indicate remote execution failure.β
Example 6 β Script Exiting Manually with 255β
#!/bin/bash
echo "Fatal error occurred!"
exit 255
Expected Output:
Fatal error occurred!
255
Explanation:
You can explicitly set exit 255 in Bash to mark fatal failure or stop signal in automation pipelines.β
4. Common Scenarios Triggering Exit Code 255β
| Scenario | Example | Description |
| SSH host unreachable | ssh user@offlinehost | Network or DNS issue |
| SSH permission denied | ssh root@vps | Authentication failed |
| Script out-of-range exit | exit 999 | Invalid code (above 255) |
| Remote command not found | ssh root@server 'nonexistent.sh' | Remote file missing |
| rsync/scp timeout | rsync /home remote:/backup | Connection dropped |
| Broken cron job | cron: ssh connection refused | Remote call failed silently |
5. WordPress VPS Use Casesβ
| Use Case | Example | Explanation |
| Remote backup via rsync fails | rsync /var/www/html root@backup:/wp/ | Destination unreachable β 255 |
| SSH automation from script | ssh root@$remote "wp plugin update --all" | Remote not accessible |
| Deployment via CI/CD | ssh deploy@server | SSH key missing |
| Cron job pushing updates | ssh -i /root/key [email protected] | Network failure |
| Remote database sync | `mysqldump ... | ssh remote mysql ...` |
| Monitoring script aborts | exit 255 | Custom fatal exit condition |
6. Troubleshooting Matrixβ
| Symptom | Possible Cause | Diagnostic Command | Fix |
ssh: connect to host ... timed out | Network issue | ping <host> | Check connectivity |
Permission denied | Missing SSH key | ssh-copy-id root@host | Add key |
Connection refused | SSH daemon off | systemctl status ssh | Start SSH service |
| Script exit 255 | Out-of-range value | grep exit in script | Keep exit codes β€255 |
| Cron job fails silently | SSH unreachable | Add set -x or log output | Log SSH errors |
| rsync stops mid-transfer | Network drop | dmesg / ping | Stabilize network or retry |
7. Best Practicesβ
- Validate SSH connectivity before automation:
ssh -o BatchMode=yes -o ConnectTimeout=5 root@host "echo connected"
- Always check exit status after remote commands:
ssh root@server "wp plugin update --all"
if [ $? -eq 255 ]; then
echo "SSH connection failed!"
fi
- Avoid invalid exit codes in scripts:
exit 1 # not exit 999
- Log every remote command:
ssh root@backup "wp cron event run --due-now" || echo "$(date): exit 255" >> /var/log/ssh_error.log
- Set connection timeouts:
ssh -o ConnectTimeout=10 root@vps
- Use retry loops for network resilience:
for i in {1..3}; do ssh root@host "echo test" && break || sleep 5; done
8. Quick Lab β Simulate Exit Code 255β
Step 1: Connect to invalid hostβ
ssh root@notarealhost
echo $?
Expected Output:
ssh: Could not resolve hostname notarealhost: Name or service not known
255
Step 2: Simulate fatal error in scriptβ
cat > /root/fatal_exit.sh <<'EOF'
#!/bin/bash
echo "Unrecoverable error occurred!"
exit 255
EOF
bash /root/fatal_exit.sh
Expected Output:
Unrecoverable error occurred!
255
Explanation: The script explicitly signals a fatal condition.β
9. Cheat Sheetβ
| Exit Code | Meaning | Description |
0 | Success | Command executed successfully |
1 | General error | Non-specific failure |
2 | Misuse of shell builtins | Syntax or structure issue |
126 | Cannot execute | Permission or binary issue |
127 | Command not found | Missing binary or invalid path |
130 | Terminated by SIGINT | Manual interrupt (Ctrl+C) |
137 | Killed by SIGKILL | Forceful termination (OOM) |
139 | Segmentation fault | Memory access violation |
143 | Terminated by SIGTERM | Graceful shutdown |
255 | Abnormal termination | Fatal or unreachable error |
10. Mini-Quizβ
- What does exit code
255mean in Bash? - Why does
ssh root@offline-hostreturn 255? - What happens when you use
exit 999inside a script? - How can you prevent
rsyncorscpreturning 255? - Why is 255 used instead of 256 or higher numbers in Bash?
Would you like me to continue next with Exit Status Code = 126 + 127 Combination Handling (Scripted Multi-Command Diagnostics) β which explains how Bash scripts handle mixed error codes (e.g., detecting which command failed in pipelines or chained sequences)?