LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-29-2003, 11:48 AM   #1
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 15
Lightbulb 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?.
 
Old 10-29-2003, 11:50 AM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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!
 
Old 10-29-2003, 12:02 PM   #3
szaroubi
Member
 
Registered: Oct 2003
Location: Montreal
Distribution: All/Any
Posts: 59

Rep: Reputation: 15
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.
 
Old 10-29-2003, 12:05 PM   #4
szaroubi
Member
 
Registered: Oct 2003
Location: Montreal
Distribution: All/Any
Posts: 59

Rep: Reputation: 15
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.
 
Old 10-29-2003, 01:44 PM   #5
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Original Poster
Rep: Reputation: 15
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.
 
Old 10-29-2003, 02:07 PM   #6
szaroubi
Member
 
Registered: Oct 2003
Location: Montreal
Distribution: All/Any
Posts: 59

Rep: Reputation: 15
How about:
#!/usr/bin/perl
close(STDIN);
close(STDOU);
close(STDERR);
exit if (fork());
exit if (fork());
while (1){
#Do your work here
}
 
Old 10-29-2003, 02:21 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
...or just:
Code:
#include <unistd.h>
/* ....*/
daemon();
<edit>
Oops. Wrong programming language in mind. Sorry
</edit>

Last edited by Hko; 10-29-2003 at 02:22 PM.
 
Old 10-29-2003, 02:28 PM   #8
szaroubi
Member
 
Registered: Oct 2003
Location: Montreal
Distribution: All/Any
Posts: 59

Rep: Reputation: 15
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:aemon::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:aemon::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:aemon leaves the implementation of other desired functionality to the caller, or other module/library (like Sys::Syslog).

There is no meaningful return value Proc:aemon::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.
------------------------------------------

Last edited by szaroubi; 10-29-2003 at 02:30 PM.
 
Old 10-29-2003, 10:19 PM   #9
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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

Last edited by SaTaN; 10-29-2003 at 10:34 PM.
 
Old 10-30-2003, 01:14 AM   #10
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Original Poster
Rep: Reputation: 15
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.

Last edited by paonethestar; 10-30-2003 at 01:15 AM.
 
Old 10-30-2003, 01:17 AM   #11
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Original Poster
Rep: Reputation: 15
But I am not able to know why "open STDIN, '/dev/null' ".Can any body explain that in SaTan's code?.Thanx
 
Old 10-30-2003, 08:27 AM   #12
szaroubi
Member
 
Registered: Oct 2003
Location: Montreal
Distribution: All/Any
Posts: 59

Rep: Reputation: 15
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?
 
Old 10-30-2003, 08:33 AM   #13
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Original Poster
Rep: Reputation: 15
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 ??
 
Old 11-03-2003, 05:38 PM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 11-05-2003, 12:13 AM   #15
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Original Poster
Rep: Reputation: 15
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 ??
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run my bash script as a daemon. jaimese Linux - Newbie 12 02-10-2011 03:28 PM
unable to run perl script using browser madhuri Debian 3 12-02-2005 04:46 PM
unable to run perl script using browser madhuri Linux - Distributions 1 11-22-2005 02:19 PM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM
can't get perl script to run from www victorus Linux - General 0 03-27-2001 05:32 PM


All times are GMT -5. The time now is 02:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration