ruby 优雅地关闭 sidekiq 进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12143350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gracefully shutting down sidekiq processes
提问by user1627827
Does anyone know how to find sidekiq's pidfile to gracefully shut it down?
Running ps ax | grep sidekiqand then running sidekiqctl stop <pid from grep>consistently gives a no such pidfileerror?
Cntl-C and Cntl-D also seem to have no effect.
有谁知道如何找到 sidekiq 的 pidfile 来优雅地关闭它?运行ps ax | grep sidekiq然后sidekiqctl stop <pid from grep>一直运行会no such pidfile出错吗?Cntl-C 和 Cntl-D 似乎也没有效果。
Closing the process window and reopening a new window doesn't kill the process as it appears to be running as a daemon.
关闭进程窗口并重新打开一个新窗口不会终止进程,因为它似乎作为守护进程运行。
The only consistent fix I've found is rebooting.
我发现的唯一一致的修复方法是重新启动。
回答by Paritosh Piplewar
Use this to kill sidekiq forcefully.
用它来强行杀死 sidekiq。
ps -ef | grep sidekiq | grep -v grep | awk '{print }' | xargs kill -9
回答by eebbesen
Sidekiq provides the ability to specify a pidfile at start time or, as shown below, to create the pidfile after the process has been started. In either case you can then use the pidfile at stop time.
Sidekiq 提供了在启动时指定 pidfile 的功能,或者如下所示,在进程启动后创建 pidfile。无论哪种情况,您都可以在停止时间使用 pidfile。
- Use
ps -ef | grep sidekiqto find the pid - Create a file (e.g.,
sidekiq.pid) with the only contents being the pid you just found sidekiqctl stop <pidfile_name>- Use
-P <pidfile_name>or--pidfile <pidfile_name>when starting sidekiq in the future
- 使用
ps -ef | grep sidekiq查找PID - 创建一个文件(例如,
sidekiq.pid),其中唯一的内容是您刚刚找到的 pid sidekiqctl stop <pidfile_name>- 使用
-P <pidfile_name>或--pidfile <pidfile_name>将来启动 sidekiq 时
回答by dazoakley
Just been looking into this one myself...
我自己也在研究这个...
Seems like newer versions of Sidekiq have this built in:
似乎新版本的 Sidekiq 内置了这个:
https://github.com/mperham/sidekiq/wiki/Signals
https://github.com/mperham/sidekiq/wiki/Signals
kill -USR1 [PROCESS_ID]
kill -USR1 [PROCESS_ID]
Worked great for me. The workers stopped picking up new jobs, but finished the ones they were on, then I finally killed the process when it was done.
对我很有用。工人们不再接新的工作,但完成了他们所从事的工作,然后我终于在完成后终止了这个过程。
kill -TERM [PROCESS_ID]
kill -TERM [PROCESS_ID]
回答by DaniG2k
I've written a little handler that can start or stop sidekiq.
我编写了一个可以启动或停止 sidekiq 的小处理程序。
start_stop_sidekiq.sh
start_stop_sidekiq.sh
#!/bin/bash
cmd=
PROJECT_DIR=
PIDFILE=$PROJECT_DIR/tmp/pids/sidekiq.pid
cd $PROJECT_DIR
start_function(){
LOGFILE=$PROJECT_DIR/log/sidekiq.log
echo "Starting sidekiq..."
bundle exec sidekiq -d -L $LOGFILE -P $PIDFILE -q mailer,5 -q default -e production
}
stop_function(){
if [ ! -f $PIDFILE ]; then
ps -ef | grep sidekiq | grep busy | grep -v grep | awk '{print }' > $PIDFILE
fi
bundle exec sidekiqctl stop $PIDFILE
}
case "$cmd" in
start)
start_function
;;
stop)
stop_function
;;
restart)
stop_function && start_function;
;;
*)
echo $"Usage: #!/bin/bash
DIR="$( cd "$( dirname "##代码##" )" && pwd )"
PROJECT_DIR=$DIR/../ # EDIT HERE: rel path to your project form this file location (my scripts are in ./scripts/)
SIDEKIQ_PID_FILE=$PROJECT_DIR/tmp/pids/sidekiq.pid # EDIT HERE: pid file location
if [ ! -f $SIDEKIQ_PID_FILE ]; then
# if no pid file, retrieve pid and create file
ps -ef | grep sidekiq | grep busy | grep -v grep | awk '{print }' > $SIDEKIQ_PID_FILE
fi
(cd $PROJECT_DIR && bundle exec sidekiqctl stop $SIDEKIQ_PID_FILE)
{start|stop|restart} /path/to/rails/app"
esac
Save it, type chmod +x start_stop_sidekiq.sh.
Then just run it with:
保存,输入chmod +x start_stop_sidekiq.sh。然后只需运行它:
bash start_stop_sidekiq.sh start /path/to/your/rails/app
bash start_stop_sidekiq.sh start /path/to/your/rails/app
or
或者
bash start_stop_sidekiq.sh stop /path/to/your/rails/app
bash start_stop_sidekiq.sh stop /path/to/your/rails/app
If you only have one Rails app, you can also set the $PROJECT_DIR variable statically so that you don't need to specify the path each time. Hope this helps!
如果你只有一个 Rails 应用,你也可以静态设置 $PROJECT_DIR 变量,这样你就不需要每次都指定路径。希望这可以帮助!
回答by laffuste
If you like bashes...
如果你喜欢 bash ...
scripts/stop_sidekiq.sh
脚本/stop_sidekiq.sh
##代码##Notes:
笔记:
- will work even if sidekiq started without pid file argument
- assumes this script is in a folder inside the project and pid files are stored in ./tmp/pids/
- 即使 sidekiq 在没有 pid 文件参数的情况下启动也能工作
- 假设此脚本位于项目内的文件夹中,pid 文件存储在 ./tmp/pids/
回答by Henley Chiu
Try using god to monitor sidekiq.
尝试使用 God 来监控 sidekiq。
Then all you need to do is bundle exec god stop
然后你需要做的就是 bundle exec God stop
Alternatively, you can use: sidekiqctl stop 60
或者,您可以使用:sidekiqctl stop 60
回答by Kamil Bednarz
Sharing a bash script that checks if sidekiq is running, sends it TSTP to ask it to not pick up any new jobs, waits until any running jobs are finished and then stops the process by sending a TERM signal to it.
共享一个 bash 脚本来检查 sidekiq 是否正在运行,向它发送 TSTP 以要求它不接收任何新作业,等待任何正在运行的作业完成,然后通过向它发送 TERM 信号来停止进程。
https://gist.github.com/kamilbednarz/5ea6398af2a7537aa8feb5a63f3acf2f
https://gist.github.com/kamilbednarz/5ea6398af2a7537aa8feb5a63f3acf2f

