问题描述:

Basically, I'm working on a remote server, and connecting via SSH. Now, I can run processes / execute bash scripts normally with my SSH console, but there's one problem. As soon as I log off, those bash scripts just quit.

Is there any way for me to start a bash script, from SSH, and let it run, even if my computer, on which I initiated the bash script to the server, is switched off?

Thanks in advance,

Peace! ~Tom

网友答案:

In my opinion one of the easiest ways to achieve this is to use 'screen'. This will create a virtual terminal which won't kill your tasks after you logout.

  1. Login into server
  2. Start screen (just type screen)
  3. Start Your Command
  4. Logout

Your process will now continue to work.

After you relogin type 'screen -list' and find the pid of the last screen session. Then type

screen -dr YOUR_PID

To reopen the screen and continue your work.

P.S. Screen is full of features that are super usefull for remote administration. Just have a look at a tutorial.

网友答案:

The nohup program jim suggests is good. See its command line options to change log location.

Another cool option is screen. When you start it you can have multiple screens and if you log off an later log in, then you can reattach to the same session with screen -r. It has many cool features but you need to read the doc.

The simplest thing is when you start some scrtips with & at end, you can use the disown bash command to make these scripts not terminate with bash exit.

Hope this gives you some clues.

Update, a quick getting started with screen:

  • type screen to start a session
  • you can immediately start commands
  • Ctrl+a+c will create a new window and you go to it, you can start other ocmmands
  • Ctrl+a+# where # is from 0 to 9 will move you to the window you select
  • Ctrl+a+d detach your session and you are back to normal terminal
  • screen -r re-attaches you to a session, if you have multiple, you are asked to type part of its name (must be a unique sequence contained in the session name)
  • .screenrc can be used for goodies like scrollback size, key bindings, terminal options, etc. See these two example files for nice options: one and two.
网友答案:

Test script

script call it remote.sh:

at now <<!
/path/to/test.h> /tmp/test.out
!

copy this to the remote box: scp remote.sh [email protected]: ssh remote 'chmod +x remote.sh'

  1. write the commands you want in a copy of test.sh locally, example:

cd /tmp; cp *.foo /path/to/somewhere/else

  1. copy the script to the remote server

    scp test.sh [email protected]: ssh remote 'chmod +x test.sh'

  2. ssh remote './remote.sh'

Repeat steps 1, 2 & 3 each time you need to run your script without waiting for it.

相关阅读:
Top