Saturday, June 8, 2013

Write a simple Service using Upstart on Ubuntu

There are two ways of writing a service on Ubuntu. One is by dropping a complex config file in init.d and the other is by using "upstart".

Upstart makes it effortless to write services. All you have to do is put a configuration file in /etc/init and upstart takes care of the rest. You can then start and stop services by using:
start test
stop test

To get the status of the service, you can do
status test

Here's a sample service that calls a shell script that prints strings on the console:

################ test.conf
description     "Hive Server"
author          "Yash Ranadive"

start on runlevel [2345]
stop on runlevel [016]


  /home/yranadive/ >> /tmp/print_delay.out 2>&1
end script

You need to know what kind of a program you are trying to run. For. e.g. if the program forks itself after running it or not. If the program is a service you will need to add expect daemon to the conf file.

Here's the shell script

while [ $c -le 5 ]
  echo "Test line at $(date)"
  sleep 10
  (( c++ ))

No comments:

Post a Comment