Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

daemonize a perl script on redhat linux 7.3

by Continuum83 (Acolyte)
on Jul 31, 2002 at 03:16 UTC ( [id://186412]=perlquestion: print w/replies, xml ) Need Help??

Continuum83 has asked for the wisdom of the Perl Monks concerning the following question:

salutations, i know there is some info on cpan about daemonization, but i am fairly new to perl, and i dont think that is what i need...

would anyone know a way for me to run a perl script as a deamon... i am actually testing out that nifty tutorial on the IO Sockets MP3 server http://perlmonks.org/index.pl?node=MP3%20server%20with%20IO%3A%3ASocket and i was wondering how i could make that run daemonized... the script works well, but making it as a daemon would be really neat.

thanks for all your help monks

Replies are listed 'Best First'.
Re: daemonize a perl script on redhat linux 7.3
by sauoq (Abbot) on Jul 31, 2002 at 04:00 UTC

    Basically, you want to

    • disassociate yourself from the tty
      • use ioctl() or POSIX::setsid()
      • re-open your file descriptors.
    • possibly chroot and/or change your working directory to '/'
    • fork && exit.
    Proc::Daemon will do it all for you though.
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: daemonize a perl script on redhat linux 7.3
by theorbtwo (Prior) on Jul 31, 2002 at 04:03 UTC

    Hmm. Try doing a search on cpan for "daemon". Either run cpan (perl -MCPAN -eshell), and use the i command (i /daemon/), or go to search.cpan.org and use the search box in the mid-left. Or, for bonus points, /msg yourself [cpan://daemon].

    Any of the above will point you at Proc::Daemon, which will do what you want.


    Confession: It does an Immortal Body good.

Re: daemonize a perl script on redhat linux 7.3
by Zaxo (Archbishop) on Jul 31, 2002 at 04:30 UTC

    I posted a short daemonizer script at Re: Daemons???. Specifying a log path and adding exec '/path/to/yourscript', @args; at the end should do it. That code follows the Stevens rules from APUE.

    After Compline,
    Zaxo

Re: daemonize a perl script on redhat linux 7.3
by simeon2000 (Monk) on Jul 31, 2002 at 04:06 UTC
    CPAN - Proc::Daemon

    As a side note, has anyone ever tried using threads instead of fork for handling the clients in a perl-based MP3 streamer? I would imagine it would be much more efficient, but I haven't kept up to date on the efficiency or stability of the latest releases of perl threads...

    <code>perl -e "print qq/just another perl hacker who doesn't grok japh\n/"

Re: daemonize a perl script on redhat linux 7.3
by Rex(Wrecks) (Curate) on Jul 31, 2002 at 17:24 UTC
    The above will get you there. However, if you really want to understand the intracacies of Daemonizing in Perl I would recomend reading Network Programming with Perl by Lincoln D. Stein. It's amazing, and available online!

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

      I heartily second the recommendation of the Network Programming volume. I would also suggest Net::Server as a good module to use for writing daemons, as it natively supports several forking models. POE is also a very powerful and useful tool, but with a longer learning period.

      As far as being RH 7.3-centric, none of these (or the previously-mentioned Proc::Daemon) are shipped with Perl in RH 7.3. You will have to retrieve any of these (and their dependancies) from CPAN.

      --rjray

        I heartily third that recommendation. I've gotten so much

        use out of that book. The best thing is the example code,

        much like the Richard Stevens 'C' code from "Advance

        Programming in the Unix Enviorment", you can take the code,

        make minimal changes and your up an running. I've had to

        write a few daemon processes in the last year, and the book

        was a great help. The daemon code in Chapter 14 will give

        you syslogging and kill -HUP functionality as well.

        STH

Re: daemonize a perl script on redhat linux 7.3
by Ryszard (Priest) on Jul 31, 2002 at 12:41 UTC
    Am I missing something when i say run it using init.d ?
      Running it from init.d won't daemonize it. It will just execute the program. You need to make sure the process will piddle about forever waiting for input/whatever. Hence disassociating it from the terminal and using setsid().
Re: daemonize a perl script on redhat linux 7.3
by elwarren (Priest) on Jul 31, 2002 at 19:20 UTC
    You could just run it with the nohup command if you're on *nix system. This basically wraps whatever program you launch and catches signals sent to it, keeping it from closing when you logout of your session. All output will be logged to a file named nohup.out in the dir you started from.
    nohup mp3server.pl &
Re: daemonize a perl script on redhat linux 7.3
by Anonymous Monk on Jul 31, 2002 at 22:37 UTC
    thanks for all the replies fellow monks, but i tried: use Proc::Daemon; Proc::Daemon::Init; and it worked fine going into daemon mode, but when i try to connect to the box, winamp gives me an error saying, "cannot sync to mpeg" ... any thoughts? thanks.
      Which version of the code are you using - the database version, the orig version, the patched version?

      I'm wondering if:

    • Winamp is connecting to the server
    • The server has access to the mp3 (file permissions)
    • The mp3's actually exist

      Are you running it chroot? That could have a lot to do with accessing your mp3's.

      Is your link too slow? Perhaps winamp cant sync because its recieving data at a slow rate...

      I run the database version on my redhat 7.2 box with no worries at all. I have it running locally and (simultaneously) also across the network (100Mb) to winamp with no worries. My redhat box is an OLD P-II, (indicating, a non high performance requirement)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186412]
Approved by kvale
Front-paged by Trimbach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found