问题
在启动一个自定义的systemd服务时,启动失败,查看日志,Failed at step EXEC spawning /some/path/my_sh.sh: Permission denied
查看日志
journalctl -u unit_name
Apr 19 14:30:36 my_host_name systemd[21484]: Failed at step EXEC spawning /some/path/my_sh.sh: Permission denied
Apr 19 14:30:36 my_host_name systemd[1]: myService.service: main process exited, code=exited, status=203/EXEC
Apr 19 14:30:36 my_host_name systemd[1]: Unit myService.service entered failed state.
Apr 19 14:30:36 my_host_name systemd[1]: myService.service failed.
Apr 19 14:30:46 my_host_name systemd[1]: myService.service holdoff time over, scheduling restart.
shell文件
my_sh.sh
case "${1}" in
'start')
if [ -n "${PID}" ];
then
echo "${0}: ${PNAME} is already running: ${PID}"
exit 0
fi
//这里产生了子进程
nohup java -jar ${JAR} --spring.profiles.active=dev 2>&1 1>/dev/null &
service文件
service文件 cat /etc/systemd/system/my_service.service
[Service]
User=root
#当服务崩溃时自动重启
Restart=on-failure
#重启前等待10秒
RestartSec=10s
# 启动命令
ExecStart=/some/path/my_sh.sh start
# 停止命令
ExecStop=/some/path/my_sh.sh stop
操作系统版本
[root@my_host_name ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
selinux状态
查看selinux状态,是关闭的,
[root@my_host_name opt]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
解决
selinux是关闭的,shell也有x权限,再次看了一遍报错的信息,Failed at step EXEC spawning
,似乎是因为创建子进程导致的,联想到service默认类型是simple,simple不能以子进程方式启动,所以很有可能是shell文件中的nohup或&导致,去掉nohup及&后启动成功。
修改前
nohup java -jar ${JAR} --spring.profiles.active=dev 2>&1 1>/dev/null &
修改后
java -jar ${JAR} --spring.profiles.active=dev 2>&1 1>/dev/null
照17.3.2 systemctl 設定檔的設定項目簡介所说,如果想要以nohup启动,需要设置type为forking,默认的是simple