I have started a Perl script from the terminal that runs in the background:

>perl myScript.pl &

If I logout, will it stop my script from running?



Yes, this is dead easy to test. Write a perl script that appends the date to a file every minute, and run it in the background. Log out, wait 5 minutes, log in, check for 5 entries corresponding to the times you are logged out. To get it working in future (too late if it's already running) use nohup as explained in another answer. You can background a foreground script after you start it (CTRL-Z) but you can only nohup it at execution

  • Thanks for the suggestion but the thing is my script has been running for a long time and I did not want to stop to test it. I had no idea it was going to run this long but I just ran some calculations and this looks to be about a 90hr job! – Allen Liu Mar 15 '12 at 22:46
  • 3
    If you have any doubt, then stick to a) password protected screensaver rather than logging out if security is the issue, or b) turn on fast user switching if someone else needing to use the computer is the issue - neither of these will affect it. – stuffe Mar 15 '12 at 22:51

In addition to @stuffe's answer, if you are logged in via ssh and run your command, it will die when you close the connection. So, if you were at home and ssh'ed into your office workstation and executed your perl myScript.pl & command, it would die when you exit'ed from the ssh connection.

To avoid this problem, you'll want to use the nohup command, which dates back to the teletype days and stands for "no hangup" --- essentially it causes the command to ignore the POSIX HUP (hangup) signal. It is used as:

nohup perl myScript.pl &

This will keep running on your office workstation even after you have disconnected/logged out from your home machine.

  • 2
    indeed, nohup is the answer! – stuffe Mar 16 '12 at 0:00
  • This is great. Thank you. After executing exactly as described, I get the message "nohup: ignoring input and appending output to `nohup.out'" as expected, but I don't see a Unix prompt. Based on your answer, it probably doesn't matter if connection is lost/closed. But say if I want to use the same command line window to do other things, is there a way to let the command line prompt to come up? – David C. Feb 26 '17 at 17:36
  • I think I figured it out: I typed in "bg" to let run in the background. To check the status, I typed in "jobs". – David C. Feb 26 '17 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .