我开发了一个小型 python 应用程序,我正在尝试对其进行守护进程。我用过本指南并稍微修改了示例。我有以下脚本:
#!/bin/sh
### BEGIN INIT INFO
# Provides: my-config-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: My configuration website
# Description: Python Flask webserver with config views and API
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/home/pi/myserver/config-site
DAEMON=$DIR/run.py
DAEMON_NAME=my-config-server
LOGFILE=/var/log/myserver/config.log
# Add any command line options for your daemon here
DAEMON_OPTS="--public"
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=pi
# The process ID of the script when it runs is stored here:
PIDFILE=/tmp/$DAEMON_NAME.pid
. /lib/lsb/init-functions
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPTS > $LOGFILE 2>&1"
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0
应用程序读取/写入搁置文件以存储配置信息。当我正常从命令行运行脚本时,一切都很好。当我尝试启动服务时,在尝试读取搁置文件时收到“权限被拒绝”错误:
Traceback (most recent call last):
File "/home/pi/myserver/config-site/run.py", line 17, in <module>
from config import app
File "/home/pi/myserver/config-site/config/__init__.py", line 32, in <module>
alarm_brightness=100
File "/home/pi/myserver/config-site/config/persistent_config.py", line 52, in set_defaults
if self.get(k) is None:
File "/home/pi/myserver/config-site/config/persistent_config.py", line 14, in get
with shelve.open(self.filename) as shelf:
File "/usr/lib/python3.4/shelve.py", line 239, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/lib/python3.4/shelve.py", line 223, in __init__
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "/usr/lib/python3.4/dbm/__init__.py", line 94, in open
return mod.open(file, flag, mode)
_dbm.error: [Errno 13] Permission denied
搁置文件权限和父目录当前为 rwxr--r--。
如何正确运行守护程序/设置权限,以便可以读/写此文件(通常是任何具有读/写用户权限的文件?)
我在 Raspberry Pi Zero 上的 Raspbian 安装上运行此程序。