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

Hi, I was wondering if it is possible to run a perl script on the unix command line so that it runs in the background, as a background process, thanks.

Replies are listed 'Best First'.
Re: Running a Perl Process
by Aristotle (Chancellor) on Jan 01, 2005 at 20:26 UTC

    Just append an ampersand to the commandline, as you would to put any process in the background in shell: perl script params &. Or launch it, hit Ctrl-Z to stop it and return to the shell, and restart it in the background by issuing bg.

    If that's on a remote machine and you want to close the connection after starting the process, investigate nohup(1) and/or screen(1).

    Makeshifts last the longest.

Re: Running a Perl Process
by aristotle73 (Sexton) on Jan 02, 2005 at 02:39 UTC
    You could also excute the script with one ampersand at the end of the command. So you would use the following at the shell prompt:
    myPerlScript.pl &
    myPerlScript.pl should now be running in the background.

      Actually this only works if a) you have set the executable bit on that file and b) the file is placed somewhere in the path (~/bin or /usr/local/bin are good choices). Some systems have the current directory (.) as part of the $PATH but this is a bad idea for security reasons (you could end up running a program different from the one you thought because it happens to have the same name). The method recommended by Aristotle is the more typical usage.

      Update Sorry if this sounds harsh. I just said it because someone not familiar with Unix might try running a Perl program that way and not understand why it doesn't work.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Running a Perl Process
by aquarium (Curate) on Jan 02, 2005 at 10:36 UTC
    i recommend you read a unix system administration book, so you understand what you're doing on the command line. most such books discuss this in a separate chapter, and process id numbers, and how to terminate properly a background task. if you run a script that runs forever (or several thereof), and eat up CPU time on some production unix server, then you're likely to aggrevate the system administrator.
    the hardest line to type correctly is: stty erase ^H