Ruby script as service
I've actually found a much better way of doing that by using ruby scripts.
This is how I did it:
First of all, I installed daemon
gem install daemons
Then I did:
require 'rubygems'
require 'daemons'
pwd = File.dirname(File.expand_path(__FILE__))
file = pwd + '/runner.rb'
Daemons.run_proc(
'my_project', # name of daemon
:log_output => true
) do
exec "ruby #{file}"
end
I then create a file called runner.rb, in which I can call my scripts such as:
require "/var/www/rails/my_project/config/environment"
Post.send('details....')
Daemons is a great gem!
Running a Ruby Program as a Windows Service?
Check out the following library: Win32Utils. You can create a simple service that you can start/stop/restart at your leisure. I'm currently using it to manage a Mongrel instance for a Windows hosted Rails app and it works flawlessly.
how to monitor and restart two ruby scripts with God?
You should be able to specify watches for both in the same God config file. You might also want to add a group called listeners
if you ever needed to stop or restart them together. E.g.,
God.watch do |w|
w.name = "twilio"
w.group = "listeners"
w.start = "ruby ~/code/site/iron.io/twilio-listen.rb"
w.keepalive
end
God.watch do |w|
w.name = "slack"
w.group = "listeners"
w.start = "ruby ~/code/site/iron.io/slack-listen.rb"
w.keepalive
end
How to stop a service from a script called by a cron job?
Figured out the issue, which was silly. I realized that the service command doesn't reside in the environment paths that the cron job has, so that's why it never worked. Neither is ifconfig
, which is what I was trying to for testing.
So I had to replace my service
command in the Ruby script with /usr/sbin/service
and it worked flawlessly afterwards.
Start systemd user service from Rails
I was able to solve the issue by telling the shell where to find the correct DBUS for the user. I set the environment variable XDG_RUNTIME_DIR to the location of DBUS for the user, 1001 in my case. Check echo $UID
to find user id, I couldn't use this inline.
`export XDG_RUNTIME_DIR="/run/user/1001" && systemctl --user start myservice`
Thanks to D.j. Molny for the initial sign post and NeilCasey for the remainder
ruby restart script automatically once it ends execution
I have a ruby daemon running on my server and for some mysterious reasons it crashes some times. I wrapped it into a while true
loop and print me some timestamps to stdout
, like that:
#!/bin/bash
while true; do
echo $(date +%Y%m%d%H%M%S) "Starting my custom ruby daemon..."
ruby /home/user/my/custom/ruby/daemon.rb
echo $(date +%Y%m%d%H%M%S) "My custom ruby daemon crashed. Respawning in 10 sec..."
sleep 10
done
That works well. Putting a sleep into it is highly recommended, in case something goes wrong the loop wont blow up your shell.
Related Topics
Ruby on Rails HTML-Table Generator
Disable Devise's :Confirmable On-The-Fly to Batch-Generate Users
What's The Most Efficient Way Get The First Day of The Current Month
Rails Render Partial in Helper
Preventing Delayed_Job Background Jobs from Consuming Too Much CPU on a Single Server
Ip Range to Cidr in Ruby/Rails
Open a File Case-Insensitively in Ruby Under Linux
How to Add Attribute to Nokogiri Node
Best Practice for Limiting the Number of Associations Within a Has_Many Relationship
Ruby (With Rails) Convert a String of Time into Seconds
Ruby Array Concat Versus + Speed
How Can a Nested Class Access a Method in The Outer Class in Ruby
Retrieve All Posts Where the Given User Has Commented, Ruby on Rails
Why Are My Thread Variables Intermittent in Rails
Did I Install Ruby 1.9.3 Correctly on Rhel
Ruby 'split': Invalid Byte Sequence in Utf-8 (Argumenterror)