Question

This'll probably be one of those face-palm experiences for me, but humility is a virtue and I'm running out of leads... The summary of this post is that I need to discover what might be invoking a daily ruby run outside of our core website.

We're running on Ubuntu 10.04 LTS with Ruby 1.9.3-p484/Rails 3.2.17 on Apache/Passenger with MySQL on the backend. We use resque, god, and passenger as part of our stack. I've been programming in Ruby for about 4-5 months from an inherited code base (so certainly am still learning), but have worked with Linux (off and on) for years.

I have a class method of a model that seems to be invoked daily for updating our model contents (I can see it executing in the Ruby logs), but I can't tell what's invoking it. It does not appear to be within the Apache/Passenger stack since it appears to be run under a process that appears only to execute the routine and then exit, suggesting a cron job. I've grepped for Resque.enqueues, looked through the app/jobs directory, and reviewed the /etc/cron files. I've even grepped the file system at a pretty high level for the name of method being called. The Gemfile doesn't suggest any other background processing facilities other than Resque.

I've put a "caller" line for logging that but it of course only shows the call stack within Ruby though it does corroborate that the process just exists for running the method (the PID doesn't show up with "ps aux" when I check later):

"(eval):1:in `<top (required)>'",
"/var/www/myapp/shared/bundle/ruby/1.9.1/gems/railties-3.2.17/lib/rails/commands/runner.rb:54:in `eval'",
"/var/www/myapp/shared/bundle/ruby/1.9.1/gems/railties-3.2.17/lib/rails/commands/runner.rb:54:in `<top (required)>'",
"/var/www/myapp/shared/bundle/ruby/1.9.1/gems/railties-3.2.17/lib/rails/commands.rb:64:in `require'",
"/var/www/myapp/shared/bundle/ruby/1.9.1/gems/railties-3.2.17/lib/rails/commands.rb:64:in `<top (required)>'",
"./script/rails:6:in `require'", "./script/rails:6:in `<main>'"

My question is (1) is there some other scheduling facility that I should look at (perhaps a Ruby facility I'm not familiar with), or (2) is there a way for me to add a call from within Ruby (say, like I did with "caller") that would show what the invoking command is?

UPDATE (4/27/14): Based on the Process.ppid clue give by @BasileStarynkevitch (thank you!), I was able to find the command invoking the method I was searching using the following code

Rails.logger.warn "pid = >#{Process.pid}<, parent pid = >#{Process.ppid}<"
_cmd = "cat /proc/#{Process.pid}/cmdline"
Rails.logger.warn "current = >#{`#{_cmd}`}<" rescue nil
_cmd = "cat /proc/#{Process.ppid}/cmdline"
Rails.logger.warn "parent  = >#{`#{_cmd}`}<" rescue nil

The log reviewed that a Rails runner script was being started from a bash script:

Apr 27 13:12:22 svrprod rails[26730]: pid = >26730<, parent pid = >26729<
Apr 27 13:12:22 svrprod rails[26730]: current = >ruby./script/rails runner Model.method<
Apr 27 13:12:22 svrprod rails[26730]: parent  = >/bin/sh -c(cd /var/www/myapp/current && bash -l -c "RAILS_ENV=production ./script/rails runner 'Model.method'" > /tmp/method_output 2>&1)<

Unfortunately this takes me back to trying to run a grep to discover where this script is what is invoking it. It does support the notion that cron is somehow triggering it but until I can find the script, I'm back to square one.

UPDATE 2 (4/27/14): Well, learn something new everyday. It was a cron job, but I didn't now about /var/spool/cron (I was focused more on /etc/cron). That was where the trigger for the rails runner job was from. But I learned about Process.ppid and rails runner along with /var/spool/cron. Thanks for all your help.

Was it helpful?

Solution 2

As I commented, use Process.pid & Process.ppid (to get the current and parent pid).

OTHER TIPS

Interesting question, I have actually wondered about this before too.

Check out this question regarding outputting the current stack trace, without raising an exception.

Get current stack trace in Ruby without raising an exception

Synopsis:

In your method call, add the following line

puts Kernel.caller

Kernel.caller is a method that returns the current stack trace. You could just use caller instead of Kernel.caller, but I like being explicit :). Hopefully you can then find out where the mystery cron job is coming from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top