2

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).

4 Answers4

7

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
  • 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/ –  Dec 31 '09 at 10:35
  • 1
    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 –  Dec 31 '09 at 10:44
  • 1
    Upstart is an alternative to the init daemon. –  Dec 31 '09 at 11:08
2

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
  • 71,383
  • 13
  • 135
  • 169
  • Hey bangpyper, thanks for mentioning supervisord. I will look at that and upstart and supervisord. –  Dec 31 '09 at 12:39
0

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
  • 10,076
  • 6
  • 38
  • 55
  • It is not periodic task.. I use the cron to start the service if it is crashed :-). –  Dec 31 '09 at 12:38
  • well, let's be fair, it is stated nowhere in your question – shylent Dec 31 '09 at 12:56
  • 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. –  Dec 31 '09 at 13:27
  • 2
    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 Dec 31 '09 at 17:00
0

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

I know is an old question but actually the example that you found is the recommended way to do that(start/stop/restart) in ubuntu and debian. And then you could do a cron job to see if your program is running.

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

The Script is written in shell command language.

Paco Valdez
  • 1,915
  • 14
  • 26