次に、このスクリプトを使用してプロセスを再起動します。
#!/usr/bin/env bash
set -u
set -e
set -x
#
# const define area
#
PROGRAM_NAME="schedulespider.py"
CELERY_PROGRAM_NAME="celery"
PYTHON_BIN_PATH="/usr/bin/python3"
#
# stop celery process
#
ps auxww | grep celery | awk '{print $2}' | xargs kill -9
sleep 5s
ps auxww | grep schedulespider | awk '{print $2}' | xargs kill -9
#
# stop python process
#
sleep 5s
#
# start process
#
count=`ps -ef | grep ${PROGRAM_NAME} | grep -v "grep" | wc -l`
if [[ ${count} -lt 1 ]]; then
# /usr/bin/python3 -m pip install --upgrade pip
# pip3 install -r requirements.txt
export PYTHONIOENCODING=utf-8
nohup ${PYTHON_BIN_PATH} schedulespider.py > /dev/null &
#https://stackoverflow.com/questions/36651680/click-will-abort-further-execution-because-python-3-was-configured-to-use-ascii
export LC_ALL=en_US.utf-8 \
&& export LANG=en_US.utf-8 \
&& nohup celery -A dolphin.tasks.tasks worker \
--loglevel=INFO -n worker1 -Q editor_pick_and_diff_pull --concurrency 1 &
export LC_ALL=en_US.utf-8 \
&& export LANG=en_US.utf-8 \
&& nohup celery -A dolphin.tasks.tasks worker \
--loglevel=INFO -n worker2 -Q non_editor_pick_and_diff_pull --concurrency 1 &
export LC_ALL=en_US.utf-8 \
&& export LANG=en_US.utf-8 \
&& nohup celery -A dolphin.tasks.tasks worker \
--loglevel=INFO -n worker3 -Q non_editor_pick_and_non_diff_pull --concurrency 1 &
sleep 3
else
echo "unable to start app, process already exists!"
fi
このスクリプトは古いプロセスを終了し、新しいプロセスを開始します。ただし、GitHub Actionsでリモートでスクリプトを実行すると、次のログが表示されます。
cd /opt/apps/pydolphin
. /opt/apps/pydolphin/restart.sh
======END======
err: +/opt/apps/pydolphin/restart.sh:16> PROGRAM_NAME=schedulespider.py
err: +/opt/apps/pydolphin/restart.sh:17> CELERY_PROGRAM_NAME=celery
err: +/opt/apps/pydolphin/restart.sh:18> PYTHON_BIN_PATH=/usr/bin/python3
err: +/opt/apps/pydolphin/restart.sh:23> ps auxww
err: +/opt/apps/pydolphin/restart.sh:23> grep celery
err: +/opt/apps/pydolphin/restart.sh:23> awk '{print $2}'
err: +/opt/apps/pydolphin/restart.sh:23> xargs kill -9
err: +/opt/apps/pydolphin/restart.sh:25> sleep 5s
err: +/opt/apps/pydolphin/restart.sh:31> PID=+/opt/apps/pydolphin/restart.sh:31> ps -ef
err: +/opt/apps/pydolphin/restart.sh:31> PID=+/opt/apps/pydolphin/restart.sh:31> grep -w schedulespider.py
err: +/opt/apps/pydolphin/restart.sh:31> PID=+/opt/apps/pydolphin/restart.sh:31> grep -v grep
err: +/opt/apps/pydolphin/restart.sh:31> PID=+/opt/apps/pydolphin/restart.sh:31> cut -c 9-15
err: +/opt/apps/pydolphin/restart.sh:31> PID=' 9772 '
err: +/opt/apps/pydolphin/restart.sh:32> [ -z ' 9772 ' ']'
err: +/opt/apps/pydolphin/restart.sh:35> array=( ' 9772 ' )
err: +/opt/apps/pydolphin/restart.sh:36> var= 9772
err: +/opt/apps/pydolphin/restart.sh:38> single_pid=+/opt/apps/pydolphin/restart.sh:38> echo ' 9772 '
err: +/opt/apps/pydolphin/restart.sh:38> single_pid=+/opt/apps/pydolphin/restart.sh:38> awk 'gsub(/^ *| *$/,"")'
err: +/opt/apps/pydolphin/restart.sh:38> single_pid=9772
err: +/opt/apps/pydolphin/restart.sh:39> [[ 9772 -gt 1 ]]
err: +/opt/apps/pydolphin/restart.sh:40> kill -15 9772
err: +/opt/apps/pydolphin/restart.sh:47> sleep 5s
err: +/opt/apps/pydolphin/restart.sh:52> count=+/opt/apps/pydolphin/restart.sh:52> ps -ef
err: +/opt/apps/pydolphin/restart.sh:52> count=+/opt/apps/pydolphin/restart.sh:52> grep schedulespider.py
err: +/opt/apps/pydolphin/restart.sh:52> count=+/opt/apps/pydolphin/restart.sh:52> grep -v grep
err: +/opt/apps/pydolphin/restart.sh:52> count=+/opt/apps/pydolphin/restart.sh:52> wc -l
err: +/opt/apps/pydolphin/restart.sh:52> count=0
err: +/opt/apps/pydolphin/restart.sh:53> [[ 0 -lt 1 ]]
err: +/opt/apps/pydolphin/restart.sh:57> export 'PYTHONIOENCODING=utf-8'
err: +/opt/apps/pydolphin/restart.sh:61> export 'LC_ALL=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:62> export 'LANG=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:59> nohup /usr/bin/python3 schedulespider.py
err: +/opt/apps/pydolphin/restart.sh:66> export 'LC_ALL=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:67> export 'LANG=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:63> nohup celery -A dolphin.tasks.tasks worker '--loglevel=INFO' -n worker1 -Q editor_pick_and_diff_pull --concurrency 1
err: +/opt/apps/pydolphin/restart.sh:71> export 'LC_ALL=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:72> export 'LANG=en_US.utf-8'
err: +/opt/apps/pydolphin/restart.sh:68> nohup celery -A dolphin.tasks.tasks worker '--loglevel=INFO' -n worker2 -Q non_editor_pick_and_diff_pull --concurrency 1
err: +/opt/apps/pydolphin/restart.sh:73> nohup celery -A dolphin.tasks.tasks worker '--loglevel=INFO' -n worker3 -Q non_editor_pick_and_non_diff_pull --concurrency 1
err: +/opt/apps/pydolphin/restart.sh:81> sleep 3
out:
out: -------------- celery@worker3 v5.1.2 (sun-harmonics)
out: --- ***** -----
out: -- ******* ---- Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core 2021-07-19 21:12:01
out: - *** --- * ---
out: - ** ---------- [config]
out: - ** ---------- .> app: tasks:0x7fa484116518
out: - ** ---------- .> transport: redis://:**@121.196.199.223:6379/5
out: - ** ---------- .> results: redis://:**@121.196.199.223:6379/5
out: - *** --- * --- .> concurrency: 1 (prefork)
out: -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
out: --- ***** -----
out: -------------- [queues]
out: .> non_editor_pick_and_non_diff_pull exchange=non_editor_pick_and_non_diff_pull(direct) key=non_editor_pick_and_non_diff_pull
out:
out:
out: [tasks]
out: . pydolphin.dolphin.tasks.tasks
out:
out:
out: -------------- celery@worker1 v5.1.2 (sun-harmonics)
out: --- ***** -----
out: -- ******* ---- Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core 2021-07-19 21:12:01
out: - *** --- * ---
out: - ** ---------- [config]
out: - ** ---------- .> app: tasks:0x7f4f1efa40b8
out: - ** ---------- .> transport: redis://:**@121.196.199.223:6379/5
out: - ** ---------- .> results: redis://:**@121.196.199.223:6379/5
out: - *** --- * --- .> concurrency: 1 (prefork)
out: -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
out: --- ***** -----
out: -------------- [queues]
out: .> editor_pick_and_diff_pull exchange=editor_pick_and_diff_pull(direct) key=editor_pick_and_diff_pull
out:
out:
out: [tasks]
out: . pydolphin.dolphin.tasks.tasks
out:
out:
out: -------------- celery@worker2 v5.1.2 (sun-harmonics)
out: --- ***** -----
out: -- ******* ---- Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core 2021-07-19 21:12:01
out: - *** --- * ---
out: - ** ---------- [config]
out: - ** ---------- .> app: tasks:0x7fec552640b8
out: - ** ---------- .> transport: redis://:**@121.196.199.223:6379/5
out: - ** ---------- .> results: redis://:**@121.196.199.223:6379/5
out: - *** --- * --- .> concurrency: 1 (prefork)
out: -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
out: --- ***** -----
out: -------------- [queues]
out: .> non_editor_pick_and_diff_pull exchange=non_editor_pick_and_diff_pull(direct) key=non_editor_pick_and_diff_pull
out:
out:
out: [tasks]
out: . pydolphin.dolphin.tasks.tasks
out:
2021/07/19 13:21:45 Error: command timeout
err: Run Command Timeout!
ついにスクリプトがタイムアウトしました! ! !スクリプトが自動的に終了しないのはなぜですか?バックグラウンドでプロセスが進行中です。この問題を回避するにはどうすればよいですか?
答え1
同様の問題があります。出力だけでなくdev \ nullに入力を送信することで問題を解決しました。あなたの場合:
nohup ${PYTHON_BIN_PATH} schedulespider.py < /dev/null & > /dev/null &