How to run a perl script as a daemon ?
Hi,
Can we run a perl script as a daemon ?.How do we do it ?.Where should we place all our progs?.:Pengy: |
i would do it the standard daemonization way.
fork() parent exits call setsid clear umask close all unneeded open descriptors chroot to '/' now exec ur script ps. if i 4got one of the steps i apologize, stevens is all the way in the house! |
A daemon is a program that is not connected to a shell .
The logic is (If I remember right) Fork close the parent. From the child fork again. Close the child And run your code in the grandchild. |
I think oyu might need to close STDIN STDOUT and STDERR.
something like close(STDIN); close(STDOU); close(STDERR); exit if (fork()); exit if (fork()); and hopefully you will be a daemon. |
Code:
#!/usr/bin/perl |
How about:
#!/usr/bin/perl close(STDIN); close(STDOU); close(STDERR); exit if (fork()); exit if (fork()); while (1){ #Do your work here } |
...or just:
Code:
#include <unistd.h> Oops. Wrong programming language in mind. Sorry </edit> |
I found a page .. borken link ...
but god bless yahoo cache ... This module will do what the about post does ... but in perl not in C . http://216.239.37.104/search?q=cache...hl=en&ie=UTF-8 from the perldoc page (link above) ---------------------------------------------------- DESCRIPTION This module contains the routine Init which can be called by a Perl program to initialize itself as a daemon. A daemon is a process that runs in the background with no controlling terminal. Generally servers (like FTP and HTTP servers) run as daemon processes. Note, do not make the mistake that a daemon == server. The Proc::Daemon::Init function does the following: 1. Forks a child and exits the parent process. 2. Becomes a session leader (which detaches the program from the controlling terminal). 3. Forks another child process and exits first child. This prevents the potential of acquiring a controlling terminal. 4. Changes the current working directory to ``/''. 5. Clears the file creation mask. 6. Closes all open file descriptors. You will notice that no logging facility, or other functionality is performed. Proc::Daemon::Init just performs the main steps to initialize a program as daemon. Since other funtionality can vary depending on the nature of the program, Proc::Daemon leaves the implementation of other desired functionality to the caller, or other module/library (like Sys::Syslog). There is no meaningful return value Proc::Daemon::Init. If an error occurs in Init so it cannot perform the above steps, than it croaks with an error message. One can prevent program termination by using eval. ------------------------------------------ |
Code:
use POSIX qw(setsid); if you want to know more .. Welcome to LQ |
Dear szaroubi,
But I don't want to execute my code so much times.Since in this code I have to allocate something which is used by another progs.More ever that code won't be run as daemon when u don't put while loop.SaTan's prog worked fine.Any way thanx for ur concepts.Thanx to all. |
But I am not able to know why "open STDIN, '/dev/null' ".Can any body explain that in SaTan's code?.Thanx
|
When the program is running in the background, It doesn't need input ...
The point of a daemon is to have a process just run. You can kill the session from which you started it and it will stay alive., Daemons run in the background. Seeing that they do, you cannot and should not interact with them throu STDIN, STDOUT. Usualy a daemon runs in a while(true) loop, well at least the onces that need to listen for connections. What are you trying to do exactly? |
I want to run a daemon which loads %Dict hash so that other progs may use this hash.I want this daemon should be in startup while booting.So can u give any idea ??
|
The rc.sysinit and rc.local files both load the functions file. In the functions file is a function daemon() that starts a program in daemon mode. To use this just put a line in /etc/rc.d/rc.local like this
daemon /path/to/script.pl I don't think it matters where you put the script. |
Code:
#!/usr/bin/perl |
All times are GMT -5. The time now is 09:11 PM. |