调试过时的 Ruby 进程
我在 Rails 项目中有一个 ruby 脚本。该脚本包含environment.rb,以便Rails 被加载等。然后它侦听另一台服务器的TCP 套接字上的数据,解析它以保持mysql 数据库最新。
我使用守护进程 gem 来轻松启动和停止该进程。 Iv 让脚本在生产中运行一天多没有任何问题,但现在它已经过时了。守护进程控制文件表示该进程仍在运行,当我执行 ps -ef 时该进程就会显示出来,并且 monit 也认为该进程运行良好。但是数据库没有更新(如果我在另一个进程中再次运行脚本,数据库会正确更新)。
我尝试使用 GDB 通过运行 sudo gdb /usr/local/bin/ruby 25395 来调试进程的问题所在,其输出是
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...
/usr/local/bin/ruby: No such file or directory.
Attaching to process 25395
Reading symbols from /usr/bin/ruby1.8...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libruby1.8.so.1.8...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libruby1.8.so.1.8
Reading symbols from /lib/libpthread.so.0...
(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x2b7decb08d30 (LWP 25395)]
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libcrypt.so.1...
(no debugging symbols found)...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libc.so.6...
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/thread.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/thread.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/etc.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/etc.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/stringio.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/stringio.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/syck.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/syck.so
Reading symbols from /lib/libnss_compat.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_compat.so.2
Reading symbols from /lib/libnsl.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/libnss_nis.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_nis.so.2
Reading symbols from /lib/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_files.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/socket.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/socket.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/nkf.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/nkf.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/iconv.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/iconv.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/strscan.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/strscan.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/openssl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/openssl.so
Reading symbols from /lib/libssl.so.0.9.8...done.
Loaded symbols for /lib/libssl.so.0.9.8
Reading symbols from /lib/libcrypto.so.0.9.8...done.
Loaded symbols for /lib/libcrypto.so.0.9.8
Reading symbols from /lib/libz.so.1...done.
Loaded symbols for /lib/libz.so.1
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/digest.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/digest.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/fcntl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/fcntl.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so
Reading symbols from /usr/lib/libmysqlclient.so.15...done.
Loaded symbols for /usr/lib/libmysqlclient.so.15
0x00002b7dec86df10 in read ()
from /lib/libc.so.6
这表明问题出在mysql 连接。可能几个小时内都没有任何数据。我相信这可能会导致进程与Mysql断开连接?但如果这种情况确实发生,我会认为当脚本再次开始获取数据时,会抛出一个异常,这会阻止脚本运行,提示 monit 重新启动它。
I have a ruby script inside a Rails project. The script includes environment.rb so that Rails gets loaded etc. It then listens to data on a TCP socket of another server, parsing it to keep a mysql database up to date.
I'm using the daemons gem to be able to start and stop the process easily. Iv had the script running in production with no problems for over a day but now it has gone stale. The Daemons control file says the process is still running, the process is showing up when I do a ps -ef
and monit also thinks the process is running fine. However the database is not getting updated (if I run the script again in another process the database updated properly).
I have tried using GDB to debug what the issue with the process is by running sudo gdb /usr/local/bin/ruby 25395
, the output of this is
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...
/usr/local/bin/ruby: No such file or directory.
Attaching to process 25395
Reading symbols from /usr/bin/ruby1.8...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libruby1.8.so.1.8...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libruby1.8.so.1.8
Reading symbols from /lib/libpthread.so.0...
(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0x2b7decb08d30 (LWP 25395)]
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libcrypt.so.1...
(no debugging symbols found)...done.
Loaded symbols for /lib/libcrypt.so.1
Reading symbols from /lib/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libc.so.6...
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/thread.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/thread.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/etc.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/etc.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/stringio.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/stringio.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/syck.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/syck.so
Reading symbols from /lib/libnss_compat.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_compat.so.2
Reading symbols from /lib/libnsl.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libnsl.so.1
Reading symbols from /lib/libnss_nis.so.2...
(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_nis.so.2
Reading symbols from /lib/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libnss_files.so.2
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/socket.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/socket.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/nkf.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/nkf.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/bigdecimal.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/iconv.so...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/iconv.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/strscan.so...
(no debugging symbols found)...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/strscan.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.1/lib/fast_xs.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/parser.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/json-1.1.9/ext/json/ext/generator.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/racc/cparse.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/tmail-1.2.3.1/ext/tmailscanner/tmail/tmailscanner.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/openssl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/openssl.so
Reading symbols from /lib/libssl.so.0.9.8...done.
Loaded symbols for /lib/libssl.so.0.9.8
Reading symbols from /lib/libcrypto.so.0.9.8...done.
Loaded symbols for /lib/libcrypto.so.0.9.8
Reading symbols from /lib/libz.so.1...done.
Loaded symbols for /lib/libz.so.1
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/digest.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/digest.so
Reading symbols from /usr/lib/ruby/1.8/x86_64-linux/fcntl.so...done.
Loaded symbols for /usr/lib/ruby/1.8/x86_64-linux/fcntl.so
Reading symbols from /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so...done.
Loaded symbols for /usr/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so
Reading symbols from /usr/lib/libmysqlclient.so.15...done.
Loaded symbols for /usr/lib/libmysqlclient.so.15
0x00002b7dec86df10 in read ()
from /lib/libc.so.6
This would suggest that that the issue is with the mysql connection. It is possible that there may not have been any data for a few hours. I believe that this could cause the process to be disconnected from Mysql? But if this did happen, I would have though that when the script did start getting data again, an exception would have been thrown which would have stopped the script from running, prompting monit to restart it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题,实际上是TCP连接断开了。即使重新连接后,套接字上的 gets 方法也会挂起。我使用的解决方案是在 gets 方法周围放置一个超时块。例如,
在超时结束脚本后会引发异常。然后 Monit 可以自动重新启动它。
I solved this, It was actually down the TCP connection being disconnecting. Even after reconnection, the
gets
method on the socket would just hang. The solution I used was to put a timeout block around the gets method. For examplewhich would raise an exception after the timeout ending the script. Monit can then restart it automatically.