views:

2623

answers:

9

Hello, I am using Putty to connect to a remote server. What I want to know is if there is any way to write my commands and allow them to keep running after I close the session with Putty. The reason for this is that I do not want to keep the computer ON all the time. Is there any way to do this?.

Update with the solution

For my question as it is presented the best solution is use one of the commands provided such as nohup, because you do not have to install any additional software. But if you are in the same problem use screen, install it and use it. It is amazing.

I have selected the answer of Norman Ramsey as favourite because propose several solutions using commands and screen. But please check the other answers specially the one of PEZ, then you get an insight of what screen is able todo.

+6  A: 

What you are looking for is nohup.

See the wiki link for how to use it.

Stephen Cox
+9  A: 

Try using GNU Screen. It allows you to have several shells open at once. And you can disconnect from those running shells (i.e. close session with Putty) and they will keep doing their thing.

Carlos Rendon
Screen is also nice because it will handle both accidental and purposeful disconnections. I use it anytime I'm working with flaky connectivity.
Tim Whitcomb
+1  A: 

i agree with the above answers, screen is the way to go.

Richard
+1  A: 
./command & disown
Nick Stinemates
+11  A: 

nohup, disown, and screen are all good but screen is the best because unlike the other two, screen allows you to disconnect from the remote server, keep everything running, and then reconnect later to see what is happening. With nohup and disown you can't resume interacting.

Norman Ramsey
+3  A: 

If you can't use screen (because, for instance, your SSH session is being programmatically driven), you can also use daemonize to run the program as a daemon.

Brian Clapper
+19  A: 

screen! It's the best thing since sliced bread. (Yeah, I know others have already suggested it, but it's so good the whole world should join in and suggest it too.)

screen is like, like, ummmm ... like using VNC or the like to connect to a GUI destop, but for command shell windows. You can have several shell "windows" open at once in the same screen session. You can do stuff like:

  1. Start a screens session using "screen -dR" (get used to using -dR)
  2. run some commands in one window
  3. press CTRL-A,C to create a new window open a file there in vim
  4. press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
  5. CTRL-A, 1 to go back to your vim session
  6. CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
  7. CTRL-A, 0 and start a background process
  8. CTRL-A, C to create yet a new window, "tail -f" the log for that background process
  9. CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
  10. Go on vacation for three weeks
  11. Log on to the server again and issue "screen -dR" to connect to your existing screen session
  12. check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
  13. CTRL-A, 1 to pick up that vim session again
  14. I guess you're starting to get the picture now? =)

It's like magic. I've been using screen for longer than I can remember and I'm still totally amazed with how bloody great it is.

PEZ
PEZ, totally right, I have been using it for 5 minuts, and I do not how could I work before using it.
Eduardo
Hehe, that was my thought after my first five minutes too. And it has never left me.
PEZ
+1 for excitedness
Carlos Rendon
I start/join my screen session with `screen -xRR`.
J.F. Sebastian
J.F. pretend I'm lazy and don't want to do "man screen". Can you tell us why -xRR?
PEZ
I agree. Screen rules. But isn't the default command key "CTRL-a", which means that your examples above wouldn't work for a new user?
matli
Thanks. I got carried away. =) Now fixed.
PEZ
+1 : Most informative answer
Tim Post
+2  A: 

screen is the best.

Try:

screen -dmS "MyTail" tail -f /var/log/syslog

This start command in background.

Use screen -r to list, and or screen -r Mytail to enter session.

If more users need access same session, use: screen -rx MyTail, and both or more users share the session.

olarva
Hey, I didn't know that -rx trick. Thanks!
PEZ
A: 

One way that works well for me is at.

at works like cron, but for a one-time job. I used it today to download a large file without having to keep my session alive.

for example:

$ at 23:55
at> wget http://file.to.download.com/bigfile.iso
at> ^D

You pass at a time (in the future) and it gives you a prompt. You enter the commands you want to run at that time and hit ctrl+d. You can exit out of your session and it will run the commands at the specified time.

Wikipedia has more info on at.

joeshmo