几周前我买了一个 Raspberry Pi 3,玩了好久。我正在运行 Raspbian Lite 无头版,我想做的一件事就是在启动时运行一些东西。没什么特别的,只是几个命令,但我发现这很难做到。我将使用我的脚本的一个简化示例,但这是我已经完成的,这是我无法做到的。以下是我到目前为止所做的:
创建了一个文件 /etc/init.d/sanity
#!/bin/sh
# /etc/init.d/sanity
# If you want a command to always run, put it here
echo "sanity script is running"
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "START START START"
echo "START START START" > /root/START.txt
;;
stop)
echo "STOP STOP STOP"
echo "STOP STOP STOP" > /root/STOP.txt
;;
*)
echo "Usage: /etc/init.d/sanity {start|stop}"
echo "FAIL FAIL FAIL" > /root/FAIL.txt
exit 1
;;
esac
exit 0
然后使用以下命令修改权限:
chmod 755 /etc/init.d/sanity
这似乎有效:
root@raspberrypi:/etc/init.d|⇒ ll
total 304
...blah blah blah...
-rwxr-xr-x 1 root root 493 Aug 13 23:09 sanity
...blah blah blah...
并且,手动运行,这似乎有效:
root@raspberrypi:/etc/init.d|⇒ sh sanity start
sanity script is running
START START START
root@raspberrypi:/etc/init.d|⇒ sh sanity stop
sanity script is running
STOP STOP STOP
root@raspberrypi:/etc/init.d|⇒ sh sanity
sanity script is running
Usage: /etc/init.d/sanity {start|stop}
root@raspberrypi:/etc/init.d|⇒ cat /root/STOP.txt
STOP STOP STOP
root@raspberrypi:/etc/init.d|⇒ cat /root/START.txt
START START START
root@raspberrypi:/etc/init.d|⇒ cat /root/FAIL.txt
FAIL FAIL FAIL
看起来不错。现在,我将其设置为在启动时运行,然后尝试像服务一样启动它...但什么也没发生。
root@raspberrypi:/etc/init.d|⇒ update-rc.d sanity defaults
insserv: warning: script 'K01sanity' missing LSB tags and overrides
insserv: warning: script 'sanity' missing LSB tags and overrides
root@raspberrypi:/etc/init.d|⇒ service sanity start
root@raspberrypi:/etc/init.d|⇒ ls /root
configurations
root@raspberrypi:/etc/init.d|⇒
那么,我在这里做错了什么?我是否错过了 Raspberry Pi 的某些特别之处?我确信我以前在 Ubuntu 和 Debian 上也遇到过这种情况,但从未遇到过这么多麻烦。这让我发疯了。
答案1
(对不起@Gogeta70)
我最终还是使用了 systemd。下面是我运行的 dropbox 服务的示例,它依赖于以下两个因素:
[Unit]
Description=Dropbox as a system service
After=local-fs.target network.target
[Service]
User=me
Restart=always
ExecStart=/opt/dropbox/dropboxd
Restart=on-failure
RestartSec=1
[Install]
WantedBy=default.target
我命名了该文件dropbox.service
并将其放入/etc/systemd/system/
答案2
使用systemd用于服务管理。