![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| More Than HPLIP Service for Linux - LinuxPlanet | iBot | UNIX and Linux RSS News | 0 | 08-13-2007 02:10 PM |
| More Than HPLIP Service for Linux - LinuxPlanet | iBot | UNIX and Linux RSS News | 0 | 08-13-2007 09:00 AM |
| Basic questions on writing a Unix Service (newbie help!) | Rutland Gizz | High Level Programming | 3 | 12-06-2006 03:48 PM |
| I'm writing a new Linux program! | Danny_10 | What's on Your Mind? | 11 | 10-03-2004 07:09 PM |
| Writing a .NET service to be Hosted in UNIX | yeslekmc | High Level Programming | 0 | 05-21-2002 03:03 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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.
__________________
Not quite as cool as all the other Kids... |
|
#3
|
|||
|
|||
|
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
__________________
Sivam |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 */
}
}
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 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 */
}
}
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 |
|||
| Google The UNIX and Linux Forums |