![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| More Than HPLIP Service for Linux - LinuxPlanet | iBot | UNIX and Linux RSS News | 0 | 08-13-2007 06:10 PM |
| More Than HPLIP Service for Linux - LinuxPlanet | iBot | UNIX and Linux RSS News | 0 | 08-13-2007 01:00 PM |
| Basic questions on writing a Unix Service (newbie help!) | Rutland Gizz | High Level Programming | 3 | 12-06-2006 06:48 PM |
| I'm writing a new Linux program! | Danny_10 | What's on Your Mind? | 11 | 10-03-2004 11:09 PM |
| Writing a .NET service to be Hosted in UNIX | yeslekmc | High Level Programming | 0 | 05-21-2002 07:03 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
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: Code:
#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: Code:
#!/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: Code:
#include <unistd.h>
int main()
{
close(0);
close(1);
close(2);
chdir("/");
while(1)
{
/* do stuff */
}
}
/etc/inittab: Code:
# # 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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|