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

Persistent perl

by njcodewarrior (Pilgrim)
on Mar 24, 2006 at 21:10 UTC ( [id://539098]=perlquestion: print w/replies, xml ) Need Help??

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

I've written a script that monitors a number of directories for changes in file modification times. It goes something like this:

#! /usr/bin/perl use strict; use warnings; my @directories = qw{/foo /bar}; my $directory; my @files; while ( 1 ) { FILECHECK: foreach $directory ( @directories ) { @files = glob("${directory}/*.log"); next FILECHECK unless @files; do some stuff here... } sleep 30; }

Obviously, the script runs indefinitely and I need to keep it running after I logout of my shell. With shell scripts, I typically use nohup and put the pid in the background (&), but I was wondering if there are any other ways to do it?

Replies are listed 'Best First'.
Re: Persistent perl
by borisz (Canon) on Mar 24, 2006 at 22:05 UTC
    Detach your script from the terminal. Make it a daemon. Perhaps with Proc::Daemon.
    Boris
Re: Persistent perl
by Argel (Prior) on Mar 25, 2006 at 00:31 UTC
    Sounds like you want to make it a daemon:
    use IO::Socket; use POSIX qw(WNOHANG setsid); sub daemonize { $SIG{CHLD} = 'IGNORE'; # Configure to autoreap zombies die "Can't fork" unless defined ( my $child = fork ); # FORK +<<<<<<<<<<<< CORE::exit(0) if $child; # Parent exits setsid(); # Become session leader open( STDIN, "</dev/null" ); # Detach STDIN from shell open( STDOUT, ">/dev/null" ); # Detach STDOUT from shell open( STDERR, ">&STDOUT" ); # Detach STDERR from shell chdir '/tmp'; # Change working directory umask(0); # Reset umask $ENV{PATH} = '/bin:/sbin:/usr/sbin'; # Reset PATH }
    Note: Verify your system supports autoreapping of zombies.

    You can read more in this thread.

      Do I have to be the superuser/sysadmin to run the daemon?

        The code doesn't do anything special that other programs do not do for other reasons. Common things like manipulating filehandles, changing environment variables, and forking. There are no daemons under UNIX in the stict sense. It's just a concept, a term used to set expectations for the reader. So to answer your question any user can do it (though, other aspects of your program may require root).
Re: Persistent perl
by CountZero (Bishop) on Mar 24, 2006 at 21:14 UTC
    Perhaps run the script as a cron-job?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Persistent perl
by roboticus (Chancellor) on Mar 24, 2006 at 22:05 UTC
    njcodewarrior--

    Assuming your system uses it, you could create an inittab entry for your job. Then when you start the system to the appropriate runlevel, it will just run for you.

    --roboticus
Re: Persistent perl
by bowei_99 (Friar) on Mar 24, 2006 at 22:20 UTC
    It seems from your post that you're on a *nix platform, in which case you could also create a startup script in /etc/init.d/ to launch the process each time it boots. An earlier post to include it in /etc/inittab will also only be launched when it hits a given runlevel, e.g. at boot time.

    -- Burvil

Re: Persistent perl
by asz (Pilgrim) on Mar 25, 2006 at 18:44 UTC

    here's another way to do it: Guide to a happier unix life - screen

    1. start a screen named "dirmonitor"

      bash$ screen -S dirmonitor
    2. you are now inside screen; start your program

      bash$ ./dirmonitor.pl
    3. press CTRL-a d to detache the screen (your program now runs in the background inside screen)

    4. reattach the screen named "dirmonitor"

      bash$ screen -r dirmonitor
    :)))))
Re: Persistent perl
by davorg (Chancellor) on Mar 27, 2006 at 08:49 UTC

    There's alway PPerl.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found