解决 Systemd 报错 Failed with result 'exit-code' status=203/EXEC 修复指南
排查 systemd 服务单元启动失败 status 203/EXEC 错误,解决执行权限缺失、脚本解释器缺失及 SELinux 拦截。
问题现象
配置好 systemd 服务单元(如 /etc/systemd/system/myapp.service)后,执行 systemctl start myapp 提示失败:
Job for myapp.service failed because the control process exited with error code.
See "systemctl status myapp.service" and "journalctl -xeu myapp.service" for details.
查看 systemctl status myapp.service,输出核心报错:
Process: 4125 ExecStart=/opt/myapp/start.sh (code=exited, status=203/EXEC)
Main PID: 4125 (code=exited, status=203/EXEC)
触发根因
Systemd 状态码 status=203/EXEC 表示 systemd 在调用底层的 execve() 系统调用派生新进程时失败。具体原因通常包含以下几类:
- 二进制文件或脚本缺乏执行权限:
start.sh没有添加chmod +x。 - Shebang 行错误或解释器不存在:脚本头部写了
#!/bin/bash但实际解释器位于/usr/bin/bash,或者 Windows CRLF 换行符导致解释器识别为/bin/bash\r。 - ExecStart 路径错误或非绝对路径:Systemd 严格要求所有执行指令必须使用绝对路径。
- SELinux 上下文阻断:二进制文件存放在非常规目录(如
/home或/root),被 SELinux 策略拦截。
解决步骤
1. 验证目标脚本权限与绝对路径
检查 ExecStart 中指定的目标路径是否存在并具有执行权限:
ls -la /opt/myapp/start.sh
如果缺少 x 权限,执行赋权:
sudo chmod +x /opt/myapp/start.sh
2. 检查脚本 Shebang 与换行符
检查脚本第一行以及换行符格式:
file /opt/myapp/start.sh
head -n 1 /opt/myapp/start.sh
如果输出包含 with CRLF line terminators,使用 dos2unix 转换:
sudo sed -i 's/\r$//' /opt/myapp/start.sh
验证头部指定的解释器是否存在:
# 检查 /bin/bash 是否真实存在
which bash
推荐在脚本头部统一采用兼容性最好的标准写法:
#!/usr/bin/env bash
3. 检查 SELinux 上下文限制
如果系统开启了 SELinux(如 RHEL、CentOS、Rocky Linux),检查安全上下文:
ls -Z /opt/myapp/start.sh
若上下文为 unlabeled_t 或 user_home_t,systemd 启动将被直接阻断。执行上下文修复:
sudo restorecon -v /opt/myapp/start.sh
或将可执行文件类型设置为 bin_t:
sudo chcon -t bin_t /opt/myapp/start.sh
4. 重载配置并重启服务
修改完成后,通知 systemd 重新扫描单元文件:
sudo systemctl daemon-reload
sudo systemctl restart myapp.service
验证确认
执行状态查询:
sudo systemctl status myapp.service
输出显示:
● myapp.service - My Application Service
Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2026-11-15 14:25:10 CST; 5s ago
Main PID: 4210 (start.sh)
Tasks: 12 (limit: 4915)
Memory: 45.2M
服务正常运行,203/EXEC 报错彻底消除。


