views:

929

answers:

3

I have a python based application which works like a feed aggregator and needs to be part of init.d script so that I could control the execution with start/stop/restart options. Also I want the init.d script to be setup as a cron job (I have example here).

I found one sample here http://homepage.hispeed.ch/py430/python/daemon

(PS. I don't want the script to be written in python itself).

+5  A: 

You could consider writing a Upstart task for operating systems which use Upstart.

Example:

# Start zeya
#

description     "Start Zeya music server"

start on startup

task
exec python /home/r00t/code-hacking/serve-music/zeya/src/zeya/zeya.py
--path=/home/r00t/Music

Add this to a file, say 'zeya.conf' in /etc/init

and then you can control the job using 'initctl'. For eg:

initctl status zeya
initctl stop zeya
Amit
thanks for suggestion. I never heard about that Do you have any example or link for python based app? Got this link http://upstart.ubuntu.com/
Gopalakrishnan Subramani
RTFM! For the love of God, you're already on the right site, click Getting Started and read the docs! They explain exactly how to write an Upstart job!
Oli
Is this alternative to init.d? I will learn this and let me decide? Since I am newbie, you can suggest me. I want something easy to configure and do
Gopalakrishnan Subramani
Upstart is an alternative to the init daemon.
Amit
+3  A: 

I did something like this recently and wrote some small config files using Supervisord.

From the init script (pretty much barebones), I simply called supervisor-ctl with the appropriate arguments.

Also, you should note that the actual functions (eg. start-stop-daemon) vary from distro to distro.

Noufal Ibrahim
Hey bangpyper, thanks for mentioning supervisord. I will look at that and upstart and supervisord.
Gopalakrishnan Subramani
+1  A: 

A counter-question really, but I've noticed, that you've mentioned cron, meaning, your app is going to be run periodically, as opposed to being run continously, in a so-called daemon fashion.

What is the sense in having commands like start, stop and restart for an application that is run periodically? I mean, your app is going to run once per hour (or something), why the need for start, stop and restart?

Anyway, since you've mentioned ubuntu, I must say, that the script you've linked doesn't comply with the current standard for initscripts neither for ubuntu nor for debian lenny. You can see the correct template in /etc/init.d/skeleton

To reiterate, why would you need an initscript for a cron job?

EDIT
Taking into consideration the comment, the somewhat "canonical" way to keep the application running even if it crashes or gets terminated is inittab. Of course, you can do it with a cron job as well.

shylent
It is not periodic task.. I use the cron to start the service if it is crashed :-).
Gopalakrishnan Subramani
well, let's be fair, it is stated nowhere in your question
shylent
it is my mistake and sorry about that. In fact, I don't know to how to restart the application if it is crashed and I found some one saying that cron is the best way to start the application if it is crashed. If you know alternativive way, i will be greatful to you.
Gopalakrishnan Subramani
check my edit --
shylent
Having a cron job poll if a job is running and restarting it is working around a bug that makes your program crash isn't it?
Noufal Ibrahim