LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2010, 01:14 PM   #1
bobbyright
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Running script in background?


If I wanted to run a script in the background and keep it running even if i close putty i do like

./perl.pl &

right?
 
Old 03-01-2010, 01:21 PM   #2
tom4everitt
Member
 
Registered: Jan 2009
Location: Stockholm
Distribution: Fedora, Ubuntu, Mac OS X
Posts: 78

Rep: Reputation: 23
That will run it in the background in the sense that you will get a new prompt.

If you want it to "survive" the terminal that you started it in (or putty in your case) you should start it with

nohup ./perl.pl &

or have a look at the screen command.
 
1 members found this post helpful.
Old 03-01-2010, 01:30 PM   #3
Prehistorik
Member
 
Registered: Oct 2006
Location: St. Petersburg, Russia
Distribution: Ubuntu
Posts: 30

Rep: Reputation: 1
Not quite. The script process will still be a child of your remotely open shell, so it will be terminated when you disconnect. A better solution is to use screen:

Code:
screen -S u ./perl.pl
  • -S tells screen to create a session from scratch
  • u is the name of your screen session in which the script is being run. You may choose a different one if you like
  • After all the options and the session name your command goes (along with arguments, if any).

Then you detach it (Ctrl-A, then Ctrl-D) and log out of the SSH session. To reattach it and see the results of your script you may log in to your remote server again and issue a command:

Code:
screen -d -r u
  • -r tells screen to reattach the detached session
  • -d is used to detach it first if it is already attached elsewhere
  • Again, u is your session name
 
Old 03-01-2010, 01:33 PM   #4
Prehistorik
Member
 
Registered: Oct 2006
Location: St. Petersburg, Russia
Distribution: Ubuntu
Posts: 30

Rep: Reputation: 1
Quote:
Originally Posted by tom4everitt View Post
nohup ./perl.pl &
Didn't know that before, thanks a lot
 
Old 03-02-2010, 03:17 AM   #5
tom4everitt
Member
 
Registered: Jan 2009
Location: Stockholm
Distribution: Fedora, Ubuntu, Mac OS X
Posts: 78

Rep: Reputation: 23
No problem Prehistoriks way (with the screen command) is more powerful and perhaps also more common. Its very convenient, especially when you're connected from a remote location. It sort of allows you to have several terminals (screens) in the same terminal.

The nohup (which comes from "no hangup", so you remember it ) is sort of neat in its simpleness though.

Last edited by tom4everitt; 03-02-2010 at 03:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running script in background bob81 Linux - Newbie 1 03-04-2009 01:11 AM
How to stop a process running in background (on a shell script) Azk00 Linux - Newbie 3 02-01-2008 05:24 AM
logging script, running background how lhrt Linux - General 2 06-09-2006 11:06 AM
Running script in background skguha Linux - Newbie 7 04-02-2004 09:32 AM
Running a script in background on a remote machine TheCoffeeMug Linux - Networking 3 11-06-2003 11:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration