![守护进程 /etc/init.d/ 作为守护进程运行时出现错误,但脚本在命令行中运行良好](https://linux22.com/image/1489004/%E5%AE%88%E6%8A%A4%E8%BF%9B%E7%A8%8B%20%2Fetc%2Finit.d%2F%20%E4%BD%9C%E4%B8%BA%E5%AE%88%E6%8A%A4%E8%BF%9B%E7%A8%8B%E8%BF%90%E8%A1%8C%E6%97%B6%E5%87%BA%E7%8E%B0%E9%94%99%E8%AF%AF%EF%BC%8C%E4%BD%86%E8%84%9A%E6%9C%AC%E5%9C%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E4%B8%AD%E8%BF%90%E8%A1%8C%E8%89%AF%E5%A5%BD.png)
从 /etc/init.d 运行时出错
sudo /etc/init.d/openconnect start
* Starting open connect xxx.somedomain.com openconnect
start-stop-daemon: unable to start /usr/local/bin/op_connect.sh (Exec format error)
...fail!
直接执行脚本就可以了
该脚本是 /etc/init.d 中“skeleton”示例的副本(仅更改了以下部分)
#!/bin/sh
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Author: Foo Bar <[email protected]>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="open connect XXXXXXX"
NAME=op_connect.sh
DAEMON=/usr/local/bin/$NAME
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if
下面的脚本
#!/bin/sh
echo '<passwd>' | sudo openconnect [email protected] --passwd-on-stdin https://xxxx.somedomain.com
答案1
文件开头的 ( ) 实际上是魔术数字的人类可读版本,它允许函数确定shebang
文件的性质,#!
exec
IE该文件是可执行二进制文件还是纯脚本。
因此,错误消息Exec format error
意味着exec
函数无法确定您的脚本op_connect.sh
是脚本还是二进制文件。这通常是因为 拼写shebang
错误,包括存在不可打印的字符。对您来说,最简单的方法是重新输入脚本,并确保它是可执行的,如果不是这样,这就可以解释该exec
功能的难解之处。