LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to run a perl script as a daemon ? (https://www.linuxquestions.org/questions/programming-9/how-to-run-a-perl-script-as-a-daemon-109978/)

paonethestar 10-29-2003 11:48 AM

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:

infamous41md 10-29-2003 11:50 AM

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!

szaroubi 10-29-2003 12:02 PM

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.

szaroubi 10-29-2003 12:05 PM

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.

paonethestar 10-29-2003 01:44 PM

Code:

#!/usr/bin/perl
close(STDIN);
close(STDOU);
close(STDERR);
exit if (fork());
exit if (fork());
print "\nHello...................";

Thanx for ur reply.See th above code.When I run it, it just print it and exited.ie no process running after execution of this.ie it's not the daemon's property as it should run till we kill it.So correct the above code if I made any mistake.Bye.

szaroubi 10-29-2003 02:07 PM

How about:
#!/usr/bin/perl
close(STDIN);
close(STDOU);
close(STDERR);
exit if (fork());
exit if (fork());
while (1){
#Do your work here
}

Hko 10-29-2003 02:21 PM

...or just:
Code:

#include <unistd.h>
/* ....*/
daemon();

<edit>
Oops. Wrong programming language in mind. Sorry
</edit>

szaroubi 10-29-2003 02:28 PM

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

SaTaN 10-29-2003 10:19 PM

Code:

use POSIX qw(setsid);
chdir '/';
umask 0;
open STDIN, '/dev/null';
open STDERR, '>/dev/null';
defined(my $pid = fork);
exit if $pid;
setsid;
while(1)
{
          sleep(5);
          print "Hello...\n";
}

Check out http://www.webreference.com/perl/tutorial/9/
if you want to know more ..

Welcome to LQ

paonethestar 10-30-2003 01:14 AM

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.

paonethestar 10-30-2003 01:17 AM

But I am not able to know why "open STDIN, '/dev/null' ".Can any body explain that in SaTan's code?.Thanx

szaroubi 10-30-2003 08:27 AM

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?

paonethestar 10-30-2003 08:33 AM

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 ??

/bin/bash 11-03-2003 05:38 PM

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.

paonethestar 11-05-2003 12:13 AM

Code:

#!/usr/bin/perl
$test="testing";
print $test,"\n";

This is the code of daemon I am trying for testing.I added "daemon /path to above file/" in /etc/rc.d/rc.local.But when I tried to print $test value through other program ,giving empty value.What I am trying is to use all variables or hashes from this daemon ,which always running till system shutdown, to all remaining applicaion programs.Why I am trying this is to reduce running time ,no matter how much memory waste since one seperate pc is dedicated to this program.Any idea ??


All times are GMT -5. The time now is 09:11 PM.