Forum: Ruby on Rails Problem with crontab and script/runner

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jose vicente R. (Guest)
on 2009-04-21 12:37
Hi guys, I'm trying to run some process in backgroung each minute using
script/runner.

To do this I use crontab-e and I edit the crontab. This is the line i
wrote:

*/1 * * * * /usr/local/bin/ruby /Users/jose/mimandote.com/script/runner
'Usuario.usuario_del_dia'

The console says me taht I have new mail in /var/mail/jose. the mail say
this:

From removed_email_address@domain.invalid  Tue Apr 21 10:34:00 2009
Return-Path: <removed_email_address@domain.invalid>
X-Original-To: jose
Delivered-To: removed_email_address@domain.invalid
Received: by Jose.local (Postfix, from userid 503)
        id 8E2C853DAC4; Tue, 21 Apr 2009 10:34:00 +0200 (CEST)
From: removed_email_address@domain.invalid (Cron Daemon)
To: removed_email_address@domain.invalid
Subject: Cron <jose@Jose> /usr/local/bin/ruby
/Users/jose/mimandote.com/script/runner 'Usuario.usuario_del_dia'
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=jose>
X-Cron-Env: <USER=jose>
X-Cron-Env: <HOME=/Users/jose>
Message-Id: <removed_email_address@domain.invalid>
Date: Tue, 21 Apr 2009 10:34:00 +0200 (CEST)

/bin/sh: /usr/local/bin/ruby: No such file or directory

Somebody can help me to solve this error?
Frederick C. (Guest)
on 2009-04-21 12:46
(Received via mailing list)
On Apr 21, 9:37 am, Jose vicente Ribera pellicer <rails-mailing-
removed_email_address@domain.invalid> wrote:
> /bin/sh: /usr/local/bin/ruby: No such file or directory
>
> Somebody can help me to solve this error?

At the risk of sounding completely stupid, where is your ruby
interpreter ?
Brandon O. (Guest)
on 2009-04-21 12:56
(Received via mailing list)
Hi,

Ruby isn't in /usr/local/bin/ruby. Run `which ruby' to see where it is.

You shouldn't need to have that path to ruby though before calling
script/runner, because it looks like it already has a shebang line.

Brandon
Jose vicente R. (Guest)
on 2009-04-21 13:12
Brandon O. wrote:
> Hi,
>
> Ruby isn't in /usr/local/bin/ruby. Run `which ruby' to see where it is.
>
> You shouldn't need to have that path to ruby though before calling
> script/runner, because it looks like it already has a shebang line.
>
> Brandon


Hi, ruby is in /usr/bin/ruby  :(

Now this is my line in crontab:

*/1 * * * * /usr/bin/ruby /Users/jose/mimandote.com/script/runner
'Usuario.usuario_del_dia'

The problem is that since i change /usr/local/bin/ruby to /usr/bin/ruby
i don't recive any mail. This would say thet it runs good? (i say it
because my application doesm't work and i don't know if is by cron or by
the programation of the model method).
Jose vicente R. (Guest)
on 2009-04-21 13:54
I tried with this:


*/1 * * * * /Users/jose/mimandote.com/script/runner
'Usuario.usuario_del_dia'

with the same result :(
Brandon O. (Guest)
on 2009-04-21 14:15
(Received via mailing list)
>
> */1 * * * * /Users/jose/mimandote.com/script/runner
> 'Usuario.usuario_del_dia'
>
> with the same result :(

Have you tried running it directly to see if it works or not?

If it's not giving any errors, then the script is probably running.

Brandon
Wang P. (Guest)
on 2009-04-22 10:37
(Received via mailing list)
Sometimes you should test the file which is executed or not.
So I suggest you can write some code in the script to show whether it
work or not, such as
`date >> ~/test.txt`
After testing it ,you can permit the situation.

On Apr 21, 5:12 pm, Jose vicente Ribera pellicer <rails-mailing-
Jose vicente R. (Guest)
on 2009-04-23 12:51
Wang P. wrote:
> Sometimes you should test the file which is executed or not.
> So I suggest you can write some code in the script to show whether it
> work or not, such as
> `date >> ~/test.txt`
> After testing it ,you can permit the situation.
>
> On Apr 21, 5:12�pm, Jose vicente Ribera pellicer <rails-mailing-

I try with this:

*       *       *       *       *       echo
"hola">>/Users/jose/prueba.txt

and it works perfectly.
-----------------------------
-----------------------------

This is my code...

crontab:

*       */6       *       *       *       /opt/local/bin/ruby
/Users/jose/mimandote.com/script/runner 'Usuario.new_usr_dia'

In my aplication

In usuario.rb -->Selects the new user of the day

 def self.new_usr_dia
    cambiado = false
    Usuario.all(:conditions => "usuario_del_dia is NULL", :order =>
"posicion_ranking", :limit => 200).each do |usuario|
      if cambiado == false and usuario.avatar.existe? # El usuario del
dia tiene que tener avatar
        usuario.update_attribute('usuario_del_dia',Time.now.to_s(:db))
        cambiado = true
      end
    end
    'UPDATE usuarios SET usuario_del_dia = NULL'.lanza_sql    if
cambiado == false
  end

--------------------------------
In avatar.rb:

The constant DIRECTORIO:

DIRECTORIO = File.join("public", "images", "avatar")
(I change it to this -->DIRECTORIO =
File.join(Rails.public_path, "images", "avatar"). This error doesn´t
appears now:

/opt/local/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/commands/runner.rb:47:
/Users/jose/mimandote.com/app/models/avatar.rb:21:in `mkdir': No such
file or directory - public/images/avatar (Errno::ENOENT)

But the day usuary doesn't change :(

Some methods:

 def initialize(usuario, imagen=nil)   #It creates the directory if
doesn`t exists
    @usuario = usuario
    @imagen = imagen
    Dir.mkdir(DIRECTORIO) unless File.directory?(DIRECTORIO)
  end

  def existe?
    logger.debug "AVATAR.RB(existe?): directorio #{DIRECTORIO} - todo
#{File.join(DIRECTORIO, nombre_archivo)}"
    File.exists?(File.join(DIRECTORIO, nombre_archivo))
  end

------
Read
------
It's very strange for me that before I tried to run in background this
method it runs perfecly running it from application's control zone. I
only do this from my controller

def accion_control
....
 Usuario.new_usr_dia if params[:accion] == 'new-usr-dia'
...
end


PD.
Sorry...because i know my enlish is not the best.
Thanks a lot!!
Jose vicente R. (Guest)
on 2009-04-23 15:16
Solved, the code it wasn't the problem
This topic is locked and can not be replied to.