Wednesday, 17 June 2015

Creating upstart script

Upstart scripts have to placed at /etc/init/, ending with .conf.
Basically they require 2 sessions: one to indicate when to start, and another with the command to exec.
The easiest script to start with your sample is:
# myprogram.conf
start on filesystem
exec /usr/bin/java -jar /path_to/program
Of course, created as root under /etc/init/myprogram.conf.
If your script requires more than one command line, use the script section:
# myprogram.conf
start on filesystem
script
    /usr/bin/java -jar /path_to/program
    echo "Another command"
end script
To enable bash completion for your service, add a symlink into /etc/init.d folder:
sudo ln -s /etc/init/myprogram.conf /etc/init.d/myprogram
Then try start and stop it:
sudo service myprogram start

Example
#Name: phantomserver.conf
description "Phantom server for screenshot generation"
author "Sandeep: 17/06/2015"

pre-start script
    echo 'starting service - server.js...'
end script

post-stop script
    echo 'stopped service - server.js...'
end script

start on runlevel [2534]
stop on runlevel [!2534]

exec phantomjs /var/www/html/phantomserver/server.js
respawn

No comments:

Post a Comment