能问个homebridge的问题么?
发表于 : 周四 6月 08, 2017 12:26
请问homebridge在安装并设定好之后,如何实现开机启动及后台运行?
我使用的是树莓派
谢谢
我使用的是树莓派
谢谢
代码: 全选
sudo nano /etc/init.d/homebridge
代码: 全选
#!/bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
dir="/home/pi" #!!!系统用户路径改成自己的!!!
cmd="DEBUG=* /usr/bin/homebridge"
user="pi" #!!!系统用户名改成自己的!!!
name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps -p `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd "$dir"
if [ -z "$user" ]; then
sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
else
sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
fi
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill `get_pid`
for i in 1 2 3 4 5 6 7 8 9 10
# for i in `seq 10`
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
代码: 全选
sudo chmod 755 /etc/init.d/homebridge
代码: 全选
sudo update-rc.d homebridge defaults
代码: 全选
sudo /etc/init.d/homebridge start
代码: 全选
tail -f /var/log/homebridge.log
tail -f /var/log/homebridge.err
DT27 写了: ↑周四 6月 08, 2017 13:35 这里讲了三种开机启动homebridge的方式:Running Homebridge on Bootup
建议用第二种方法[Running Homebridge on Bootup (init.d)],树莓派里的domoticz就是这么自动启动的:设置Domoticz开机自启动。
简单说下第二种方法的步骤
①新建homebridge守护进程文件文件内容如下:代码: 全选
sudo nano /etc/init.d/homebridge
②更改文件权限代码: 全选
#!/bin/sh ### BEGIN INIT INFO # Provides: # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start daemon at boot time # Description: Enable service provided by daemon. ### END INIT INFO dir="/home/pi" cmd="DEBUG=* /usr/local/bin/homebridge" user="pi" name=`basename $0` pid_file="/var/run/$name.pid" stdout_log="/var/log/$name.log" stderr_log="/var/log/$name.err" get_pid() { cat "$pid_file" } is_running() { [ -f "$pid_file" ] && ps -p `get_pid` > /dev/null 2>&1 } case "$1" in start) if is_running; then echo "Already started" else echo "Starting $name" cd "$dir" if [ -z "$user" ]; then sudo $cmd >> "$stdout_log" 2>> "$stderr_log" & else sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" & fi echo $! > "$pid_file" if ! is_running; then echo "Unable to start, see $stdout_log and $stderr_log" exit 1 fi fi ;; stop) if is_running; then echo -n "Stopping $name.." kill `get_pid` for i in 1 2 3 4 5 6 7 8 9 10 # for i in `seq 10` do if ! is_running; then break fi echo -n "." sleep 1 done echo if is_running; then echo "Not stopped; may still be shutting down or shutdown may have failed" exit 1 else echo "Stopped" if [ -f "$pid_file" ]; then rm "$pid_file" fi fi else echo "Not running" fi ;; restart) $0 stop if is_running; then echo "Unable to stop, will not attempt to start" exit 1 fi $0 start ;; status) if is_running; then echo "Running" else echo "Stopped" exit 1 fi ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit 0
③使文件生效代码: 全选
sudo chmod 755 /etc/init.d/homebridge
④这时,homebridge就会在系统重启后自动启动了,如果要现在手动启动,执行代码: 全选
sudo update-rc.d homebridge defaults
⑤如果要查看运行日志,可以检查log或者error文件代码: 全选
sudo /etc/init.d/homebridge start
代码: 全选
tail -f /var/log/homebridge.log tail -f /var/log/homebridge.err
第一行改为你的用户路径ylfzl 写了: ↑周五 8月 11, 2017 12:33DT27 写了: ↑周四 6月 08, 2017 13:35 这里讲了三种开机启动homebridge的方式:Running Homebridge on Bootup
建议用第二种方法[Running Homebridge on Bootup (init.d)],树莓派里的domoticz就是这么自动启动的:设置Domoticz开机自启动。
简单说下第二种方法的步骤
①新建homebridge守护进程文件文件内容如下:代码: 全选
sudo nano /etc/init.d/homebridge
②更改文件权限代码: 全选
#!/bin/sh ### BEGIN INIT INFO # Provides: # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start daemon at boot time # Description: Enable service provided by daemon. ### END INIT INFO dir="/home/pi" cmd="DEBUG=* /usr/local/bin/homebridge" user="pi" name=`basename $0` pid_file="/var/run/$name.pid" stdout_log="/var/log/$name.log" stderr_log="/var/log/$name.err" get_pid() { cat "$pid_file" } is_running() { [ -f "$pid_file" ] && ps -p `get_pid` > /dev/null 2>&1 } case "$1" in start) if is_running; then echo "Already started" else echo "Starting $name" cd "$dir" if [ -z "$user" ]; then sudo $cmd >> "$stdout_log" 2>> "$stderr_log" & else sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" & fi echo $! > "$pid_file" if ! is_running; then echo "Unable to start, see $stdout_log and $stderr_log" exit 1 fi fi ;; stop) if is_running; then echo -n "Stopping $name.." kill `get_pid` for i in 1 2 3 4 5 6 7 8 9 10 # for i in `seq 10` do if ! is_running; then break fi echo -n "." sleep 1 done echo if is_running; then echo "Not stopped; may still be shutting down or shutdown may have failed" exit 1 else echo "Stopped" if [ -f "$pid_file" ]; then rm "$pid_file" fi fi else echo "Not running" fi ;; restart) $0 stop if is_running; then echo "Unable to stop, will not attempt to start" exit 1 fi $0 start ;; status) if is_running; then echo "Running" else echo "Stopped" exit 1 fi ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit 0
③使文件生效代码: 全选
sudo chmod 755 /etc/init.d/homebridge
④这时,homebridge就会在系统重启后自动启动了,如果要现在手动启动,执行代码: 全选
sudo update-rc.d homebridge defaults
⑤如果要查看运行日志,可以检查log或者error文件代码: 全选
sudo /etc/init.d/homebridge start
代码: 全选
tail -f /var/log/homebridge.log tail -f /var/log/homebridge.err
我的运行环境是,ubuntu 16.04 server 在线默认装的domoticz和homebridge
domoticz目录是:/home/ylf/domoticz/
我把domoticz自启动用sudo service domoticz restart 放在了/etc/rc.local 内了,可以成功自启动。
但在设置homebridge时不能正常启动。只有在任意路径下手动输homebridge可以正常启动
dir="/home/pi"
cmd="DEBUG=* /usr/local/bin/homebridge"
user="pi"
cmd="DEBUG=* /usr/local/bin/homebridge"这个路径下我有没有 homebridge,dir="/home/pi"这个路径是否是domoticz的?想请教是否这段有关系?正确的路径我要如何写,感谢
代码: 全选
dir="/home/ylf"
cmd="DEBUG=* homebridge文件路径"
user="ylf"