http://www.perlmonks.org?node_id=312106

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

I have a perl script that I want to leave running in the background when I log off. I only have telnet access to the linux webserver that runs my wbesite (I don't have root). Everytime I log off, the script that I was running in the background is killed. How do I leave it running when I log off? -Kevin

Replies are listed 'Best First'.
Re: Leave it running
by edan (Curate) on Dec 04, 2003 at 06:01 UTC

    shell> nohup kevins_perl_script.pl &

    Your script's output will be in a file called nohup.out

    --
    3dan

      thanx buddy! -Kevin
Re: Leave it running
by QM (Parson) on Dec 04, 2003 at 06:39 UTC
    You may also be interested in screen, which allows you to "detach" the terminal from the process, and "reattach" to the process later from the same or another terminal (on the same machine).

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Screen
by barbie (Deacon) on Dec 04, 2003 at 06:42 UTC
    You may also be interested in a little app called 'screen', available on most Unix style OSs. It allows you to rehook up to a previously running virtual console running on a remote server.

    Type 'man screen' on the command line for more info.

    --
    Barbie | Birmingham Perl Mongers | http://birmingham.pm.org/

Re: Leave it running
by Zaxo (Archbishop) on Dec 04, 2003 at 06:03 UTC

    See man nohup or else your script can remove itself from its controlling terminal with &POSIX::setsid. You should also make sure that the script runs from the system root directory, since it will not be unmounted.

    After Compline,
    Zaxo

Re: Leave it running
by Roger (Parson) on Dec 04, 2003 at 06:02 UTC
Re: Leave it running
by Anonymous Monk on Dec 04, 2003 at 06:51 UTC
    my webhost sucks....I have had to manually install all of my own perl modules in my own library (since I don't have root priveledge, thanks to perlmonks for showing me how). My system does not have a lot of stuff installed already. man screen does not work, this sucks, I hate my webhost. -Kevin
      You may have luck installing screen on your own, since it is available through this link.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

      nohup doesn't work either!

      -Kevin

        Try this, inside your perl script:

        $SIG{HUP} = 'IGNORE';

        Then try running it in the background (script.pl &).

        Or, to act like a real daemon process, read perlipc under the 'Complete Dissociation of Child from Parent' section.

        --
        3dan

Re: Leave it running
by Anonymous Monk on Dec 05, 2003 at 00:01 UTC
    I used the $SIG{HUP}='IGNORE'; method. I logged off an logged back on, the script is running (For some reason 'ps' doesn't work on my machine so I can see it, but it is doing work.) It does not seem to be outputting to it's normal output file though, And there is not a nohup file. -Kevin
Re: Leave it running
by Anonymous Monk on Dec 04, 2003 at 06:35 UTC
    my system does not recoginize the command nohup.
      If you can't use nohup for some reason then you can still "daemonize" your script. Someone mentioned POSIX::setsid() - I have had good luck with using the following subroutine:
      sub daemonize { my $childpid; if(($childpid = fork()) < 0) { die("can't fork: $!"); } elsif($childpid > 0) { exit(0); # parent - exits } (setpgrp() != -1) || die("Can't change process group: $!"); $SIG{HUP} = 'IGNORE'; if(($childpid = fork()) < 0) { die("can't fork: $!"); } elsif($childpid > 0) { exit(0); # parent - exits } chdir("/"); umask(0); }
      This code was borrowed from Stevens' "Unix Network Programming" - a book that should be in every programmers bookshelf...

      Michael