gpt4 book ai didi

bash - Centos 7 自定义 bash 脚本服务不工作

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

我在 centos 7 中创建了一个守护进程来从电子邮件队列发送电子邮件。

邮件队列是在 Yii1 中实现的。这工作正常但是当我试图在服务器中创建和运行守护进程但它失败并显示错误时:

echo "Error! Could not start MyStaging!"

我正在按照我找到的说明进行操作 here .

在检查中我发现 PID 获得值 1232 而 pgrep -u $RUNAS -f $NAME >/dev/null 命令返回空。

下面是我的脚本:

#!/bin/bash
### BEGIN INIT INFO
# Provides: MyStaging
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: This service having purpose to send email from email queue where relevant email push by particular module operation in queue.
### END INIT INFO

SCRIPT="/usr/bin/php5 /home/my/public_html/staging/protected/yiic mailqueue run"
RUNAS=root
NAME=MyStagingMailQueue

PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log

start() {
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service ' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
# Try with this command line instead of above if not workable
su -c "$CMD" $RUNAS > "$PIDFILE"
sleep 2
PID=$(cat $PIDFILE)
if pgrep -u $RUNAS -f $NAME > /dev/null
then
echo "$NAME is now running, the PID is $PID"
else
echo "Error! Could not start $NAME!"
fi
}

stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service ' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}

status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac

不知道是什么问题。请帮我解决一下。

最佳答案

由于您使用的是 CentOS 7,因此最好创建一个 systemd 服务

第一步:

为您的服务创建服务文件 yiicmail.service。做:

sudo touch /etc/systemd/system/yiicmail.service


第二步:

用你喜欢的编辑器打开上面的文件,并在其中放入以下内容:

[Unit]
Description=yiic service
After=network.target

[Service]
Type=simple
User=root
ExecStart="/usr/bin/php5 /home/whizbite/public_html/staging/protected/yiic mailqueue run"
#If there are spaces, I would strings within quotes like above
Restart=on-abort


[Install]
WantedBy=multi-user.target


第三步:

现在是体验该服务的时候了。要启动它,请执行以下操作:

sudo systemctl start yiicmail # You don't have to type full name, that is, yiicmail.service

要检查状态,请执行:

sudo systemctl status yiicmail

要停止它,请执行:

sudo systemctl stop yiicmail

要在启动时启动服务,请执行:

sudo systemctl enable yiicmail

要在启动时禁用该服务,请执行:

sudo systemctl disable yiicmail

希望这对您有所帮助。

关于bash - Centos 7 自定义 bash 脚本服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34461451/

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