我有一台服务器线索发布管理器我已经从单独的用户(名为)将其安装在 Ubuntu 12.04 系统上pypi
,并且我希望能够在启动时启动该服务器。
我已经尝试使用一些命令创建一个简单的 bash 脚本(以用户身份登录pypi
,使用虚拟 python 环境,启动服务器),但这无法正常工作。终端崩溃,或者当我尝试询问服务状态时,它已启动,并且我以用户身份登录pypi
...?
因此,问题是:当服务由用户运行时,需要采取哪些步骤来确保 ClueReleaseManager 服务在启动时正确启动,并且我可以在运行时控制哪些步骤(启动/停止/..)pypi
?
附加信息和限制:
- 我想尽可能简单地做到这一点
- 无需安装任何其他软件包/程序
- 我不熟悉 Ubuntu 12.04 的 init 结构
- 我在网上找到的所有信息都非常稀疏、混乱、不正确或不适用于我以不同用户身份运行服务的情况
root
。
答案1
启动服务的基本脚本可能如下所示:
#!/bin/sh
# This is for the 'update-rc.d', so it knows when to start your service.
### BEGIN INIT INFO
# Provides: your-script
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: a short description
# Description: a long description
### END INIT INFO
COMMAND="echo -n 'I am: '; whoami"
log () {
echo "$@"
logger -p user.info -t "ClueReleaseManager [$$]" "$@"
}
main () {
case "$1" in
stop)
# stop your service
echo "Stopping"
;;
start)
# run your service as user pypi
# doesn't need password as root
echo "Starting"
# you probably want to start it in background
# (don't block the other services)
(su - pypi -c "$COMMAND") &
;;
restart|force-reload)
# restart your service
echo "Restarting"
;;
*)
# wrong command
echo "Unknown command: '$1'"
;;
esac
}
# If you want, you can even log the whole.
# Unless you changed it it should go to /var/log/syslog
main "$1" 2>&1 | log
# Better exit with success, not sure if this can screw up Ubuntus boot process.
exit 0
update-rc.d 如何操作:将其放入 /etc/init.d/your-script 中(或链接到它)。使其可执行,并添加到 init 服务中:
$ sudo chmod +x /etc/init.d/your-script
$ sudo update-rc.d your-script defaults
现在您可以启动和停止您的服务:
$ sudo your-script start
$ sudo your-script stop