gpt4 book ai didi

bash - 守护脚本无法启动(适用于 Google App Engine)

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:22 25 4
gpt4 key购买 nike

我正在尝试守护脚本。我遵循了几个教程并提出了以下脚本(以前从未做过,只是填写了一个模板,不确定 345 70 30 是什么意思):

#!/bin/bash

# parserservices Parser Services
#
# chkconfig: 345 70 30
# description: Parser Services
# processname: parserservices

# chkconfig --add parserservices

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

prog='ParserServices for CENTOS/UNIX'

start() {
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 1

echo -n $"Starting $prog: "
daemon --check parserservices nohup /home/centos/parserservices/start_dev_server_centos.sh &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/parserservices
return $RETVAL
}

stop() {
echo -n $"Shutting down $prog: "
killproc java
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/parserservices
return $RETVAL
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status parserservices
RETVAL=$?
;;
restart|reload)
stop
start
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
RETVAL=3
esac

exit $RETVAL

这只是一个测试,不会有更多的 java 进程在运行,所以杀死 java 应该不是问题(除非有人可以提供更好的替代方案,这旨在运行另一个脚本来执行一些初始化和使用 Google App Engine 开发服务器启动 java)。

我把它复制到/etc/rc.d/init.d/parserservices下面初始化启动了:

$ sudo chmod 755 /etc/rc.d/init.d/parserservices
$ sudo chkconfig --add parserservices
$ sudo systemctl daemon-reload
$ sudo service parserservices start
Starting parserservices (via systemctl): [ OK ]

但是什么都没有开始。当我自己运行脚本时,一切运行良好:

sudo nohup /home/centos/parserservices/start_dev_server_centos.sh &
root 21510 0.0 0.0 189372 2680 pts/1 S 21:18 0:00 sudo nohup /home/centos/parserservices/start_dev_server_centos.sh
root 21511 0.0 0.0 113116 1184 pts/1 S 21:18 0:00 /bin/bash /home/centos/parserservices/start_dev_server_centos.sh
root 21512 27.0 0.2 6619624 37596 pts/1 Sl 21:18 0:00 java -ea -cp /home/centos/gae/appengine-java-sdk-1.9.27/lib/appengine-tools-api.jar com.google.appengine.tools.KickStart com.google.appengine.tools.development.DevAppServ
root 21560 166 0.5 11041848 89188 pts/1 Sl 21:18 0:01 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/jre/bin/java -Xmx8184m -javaagent:/home/centos/gae/appengine-java-sdk-1.9.27/lib/agent/appengine-agent.jar -Xboo
centos 21578 0.0 0.0 123356 1384 pts/1 R+ 21:18 0:00 ps au

顺便说一句,脚本的权限:

-rwxrwxr-x.  1 centos centos  155 Jan 12 20:06 start_dev_server_centos.sh

这是 systemctl status parserservices 的输出:

$ sudo systemctl status parserservices
parserservices.service - SYSV: Parser Services
Loaded: loaded (/etc/rc.d/init.d/parserservices)
Active: active (exited) since Tue 2016-01-12 21:05:06 UTC; 18h ago

Jan 12 21:05:06 curator.novalocal parserservices[21266]: Starting ParserServices for CENTOS/UNIX:
Jan 12 21:05:06 curator.novalocal parserservices[21266]: /etc/rc.d/init.d/parserservices: Usage: daemon [+/-nicelevel] {program}
Jan 12 21:05:06 curator.novalocal systemd[1]: Started SYSV: Parser Services.
Jan 12 21:10:13 curator.novalocal systemd[1]: Started SYSV: Parser Services.

最佳答案

在 CentOS 7 中有更优雅的方式来创建守护进程。

我看到您正在混合使用经典 (SysV) 和新 (systemd) 方法来执行和管理守护进程。经典的方法是像您一样在/etc/init.d/中创建脚本,但我看到您使用的是 systemctl,它是 systemd 的一部分。我建议以 systemd 方式创建守护进程。

从/etc/init.d 中删除脚本并在/etc/systemd/system 目录中创建 parserservices.service 文件,如下所示:

[Unit]
Description=Parser Services
After=syslog.target

[Service]
ExecStart=/path/to/your/script/start_dev_server_centos.sh

[Install]
WantedBy=multi-user.target

开始于

systemctl start parserservices

检查它的状态

systemctl status parserservices

如果你想让它在系统启动时启动,那么启用它

systemctl enable parserservices

每次更改 .service 文件时执行 systemctl daemon-reload

阅读更多关于 systemd 守护进程的信息 here

关于bash - 守护脚本无法启动(适用于 Google App Engine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34754044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com