Writing a service in Linux

Hi All:
I want to write a program that runs like a service(in the background) and should start up when the system boots. It should always be running, no matter who has logged in, no matter if anybody has logged in et all.
Is there any online help i could get on this topic, appreciate the help
Thanks,
Preetham.

It's pretty simple to do in unix. No where near as complicated as it is in Win32.

You can execute your code with `nohup` (man nohup for more information) in the startup of your system. If need be, you should be able to sudo to any user you desire for this daemon for security purposes.

I use `nohup` with some network monitoring stuff that I do with tethereal and it works like a charm.

I got u r question ... u mean that it should run in back ground for ever till the server is rebooted.
What u have to do is

define the service in a program set the program in such a way that it should become the group leader.

 setsid=getpid\(\) ; /* This will make u r service the leader of u r group u belong to */ 

i don't remember the whole syntax in detail u can go thru the daemon concepts and find it out.

u can run this by manually running the program or setting this particular program in u r cron job if u have any difficulties in cron please feel free to contact me at sivhard@rediffmail.com

Why don't you just create a shell scripts and put it in /etc/rc.d/rc3.d and make a link over /etc/init.d/scripts-name. This should run when server boot up. (Linux)

Quan

If your program is written in C, your program should use the daemon() library call to make your program a daemon. This will cause your program to close its standard input, output, and error, and fork() into the background.

You should then add a command to one of your boot scripts to cause your program to run. The location of the script to change differs between Linux distributions. If you are running Red Hat, you should edit /etc/rc.d/rc.local. The script you need to edit is started directly or indirectly from /etc/inittab.

my-daemon.c:

#include <unistd.h>
#include <err.h>

int main()
{
   if(daemon(0,0) == -1)
       err(1, NULL);

   while(1)
   {
      /* do stuff */
   }
}

/etc/rc.d/rc.local:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

# Run my-daemon in the background
/usr/sbin/my-daemon

Your daemon will run until the system shuts down, or until it crashes. Also, if your daemon has a memory leak, the kernel will kill it when it uses too much RAM.

If you make your program a non-daemon that closes its standard input/output/err, you can start it directly from inittab, with the action field set to "respawn". In this case, if your program crashes, init will restart it.

my-service.c:

#include <unistd.h>

int main()
{
   close(0);
   close(1);
   close(2);
   chdir("/");

   while(1)
   {
      /* do stuff */
   }
}

/etc/inittab:

#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#

# [ snip for brevity ]

# Run only in runlevel 5. Init will kill my-service if
# another runlevel is selected. If it crashes quickly
# and repeatedly, init will stop restarting it for 5 minutes.

mine:5:respawn:/usr/sbin/my-service