# Background daemon

**URL:** <https://www.ruby-forum.com/t/background-daemon/173033>\
**Category:** Rails\
**Created:** [July 31, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/background-daemon/173033 "2009-07-31T19:35:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [July 31, 2009, 7:35pm UTC](https://www.ruby-forum.com/t/background-daemon/173033/1 "2009-07-31T19:35:14Z")

</div>

It’s sending e-mail every hour, but I changed to sleep for a day, but  
keep send the e-mail by hour. I don’t know what to do to send daily.  
Could somebody help me?

thanks  
############

# mailer.rb

###############  
#!/usr/bin/env ruby

# You might want to change this

ENV[“RAILS\_ENV”] ||= “production”

require File.dirname( **FILE** ) + “/…/…/config/environment”

$running = true  
Signal.trap(“TERM”) do  
$running = false  
end

while($running) do

```
Notifier.deliver_report_daily
#ActiveRecord::Base.logger.info "This daemon is still running at

```

#{Time.now}.\n"

```
sleep 86400

```

end

###########

# deploy.rb

############  
desc “Stop daemons before deploying”  
task :before\_deploy do  
run “#{current\_path}/script/daemons stop”  
end

desc “Start daemons after deploying”  
task :after\_deploy do  
run “#{current\_path}/script/daemons start”  
end

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 3, 2009, 4:47am UTC](https://www.ruby-forum.com/t/background-daemon/173033/2 "2009-08-03T04:47:13Z")

</div>

Penelope W. wrote:

> It’s sending e-mail every hour, but I changed to sleep for a day, but  
> keep send the e-mail by hour. I don’t know what to do to send daily.  
> Could somebody help me?
> 
> thanks  
> ############
> 
> # mailer.rb
> 
> ###############  
> #!/usr/bin/env ruby
> 
> # You might want to change this
> 
> ENV[“RAILS\_ENV”] ||= “production”
> 
> require File.dirname( **FILE** ) + “/…/…/config/environment”
> 
> $running = true  
> Signal.trap(“TERM”) do  
> $running = false  
> end
> 
> while($running) do
> 
> ```
> Notifier.deliver_report_daily
> #ActiveRecord::Base.logger.info "This daemon is still running at
> 
> ```
> 
> #{Time.now}.\n"
> 
> ```
> sleep 86400
> 
> ```
> 
> end
> 
> ###########
> 
> # deploy.rb
> 
> ############  
> desc “Stop daemons before deploying”  
> task :before\_deploy do  
> run “#{current\_path}/script/daemons stop”  
> end
> 
> desc “Start daemons after deploying”  
> task :after\_deploy do  
> run “#{current\_path}/script/daemons start”  
> end

I certainly would not use a long running daemon for this purpose. You  
should look into using cron and script/runner to execute the task daily.

0 8 \* \* \* /path/to/rails/app/script/runner -e production  
“Notifier.deliver\_report\_daily”

This will deliver your daily report each day at 8 am.

Best,  
Michael G.

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 3, 2009, 10:17am UTC](https://www.ruby-forum.com/t/background-daemon/173033/3 "2009-08-03T10:17:59Z")

</div>

if you had initially set it to sending every hour and then changed it  
to every day in between, then u may have to re-start the daemon… did  
you do that?  
but i second Michael… you should be using a cron for this.

On Aug 3, 7:47 am, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 4, 2009, 11:21am UTC](https://www.ruby-forum.com/t/background-daemon/173033/4 "2009-08-04T11:21:08Z")

</div>

Use the delayed\_job plugin if you find it hard to tweak scripts to  
your taste.

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 4, 2009, 10:43am UTC](https://www.ruby-forum.com/t/background-daemon/173033/5 "2009-08-04T10:43:39Z")

</div>

Yes, I restart, but nothing change. Continuing send the old e-mail.  
Send one time the new, but after every hour send the old.

Do you know what I have to do?  
Thanks

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 7, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/background-daemon/173033/6 "2009-08-07T18:31:50Z")

</div>

I’m using what you suggested. Its working fine for development, but  
I’m not receiving email from production. Could you help me?

thanks

---

<div class="post-metadata">

**Author:** ![penelope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@penelope](https://www.ruby-forum.com/u/penelope)\
**Post date:** [August 7, 2009, 6:34pm UTC](https://www.ruby-forum.com/t/background-daemon/173033/7 "2009-08-07T18:34:46Z")

</div>

Now its working, thanks a lot
