Run a command in a shell and keep running the command when you close the session

前端 未结 9 2009
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:11

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

相关标签:
9条回答
  • 2020-11-27 11:19

    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.

    0 讨论(0)
  • 2020-11-27 11:19

    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)
      • run some commands in one window
      • press CTRL-A,C to create a new window open a file there in vim
      • press CTRL-A,0 to go back to the first window and issue some command on the file you just edited
      • CTRL-A, 1 to go back to your vim session
      • CTRL-A, C for yet another window and maybe do "sudo - su" (because you just happen to need a full root shell)
      • CTRL-A, 0 and start a background process
      • CTRL-A, C to create yet a new window, "tail -f" the log for that background process
      • CTRL-A, d to disconnect your screen then CTRL-D to disconnect from the server
      • Go on vacation for three weeks
      • Log on to the server again and issue "screen -dR" to connect to your existing screen session
      • check the log in the the fourth window with CTRL-A, 3 (it's like you've been there watching it all the time)
      • CTRL-A, 1 to pick up that vim session again
      • 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.

    EDIT: Just want to mention there's now also tmux. Very much like screen, but has some unique features, splitting the windows being the most prominent one.

    0 讨论(0)
  • 2020-11-27 11:20

    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.

    0 讨论(0)
  • 2020-11-27 11:20

    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.

    0 讨论(0)
  • 2020-11-27 11:28
    ./command & disown
    
    0 讨论(0)
  • 2020-11-27 11:32

    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.

    0 讨论(0)
提交回复
热议问题