Friday, December 19, 2014

Create service in ubuntu to run ruby script

My Project requirement to continuously run a ruby script to do some stuff. I achieved it with nohup. If the some exception comes in my script is stop so decide to run it as a service and if service stops it should start again. 

sudo touch /etc/init/service_name.conf

Add these line to the file

description "test service by sachin choudhary"
author "sachin choudhary "


# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [016]

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
# This option does not seem to be of great importance, so it does not need to be set.
#expect fork

# Specify working directory , where your script file
chdir /home/sachin/Desktop/service

# Specify the process/command to start, e.g.
exec /home/sachin/.rvm/rubies/ruby-2.1.5/bin/ruby test.rb


And you all done :-)

NOTE:- For Ruby guys if you are using rvm and your script might require some gems, then your required gems must be installed in rvm global set else your script not load them.