Ruby Script as Service

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



Leave a reply



Submit