9

I logged in to a remote server via ssh and started a php script. Appereantly, it will take 17 hours to complete, is there a way to break the connection but the keep the script executing? I didn't make any output redirection, so I am seeing all the output.

yasar
  • 13,158
  • 28
  • 95
  • 160
  • 1
    Duplicate? http://stackoverflow.com/questions/4071496/how-to-start-a-process-that-wont-end-when-my-ssh-session-ends – B Johnson Nov 18 '11 at 15:48
  • 1
    you don't have to stop the process. You can pause it and send it to background. You also won't need to install additional packages. – hovanessyan Nov 18 '11 at 15:48

4 Answers4

10

Can you stop the process right now? If so, launch screen, start the process and detach screen using ctrl-a then ctrl-d. Use screen -r to retrieve the session later.

This should be available in most distros, failing that, a package will definitely be available for you.

Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72
  • 3
    If you can't stop it, you can steal the terminal into a screen session with reptyr. See http://serverfault.com/questions/24425/can-i-nohup-screen-an-already-started-process/272526#272526 – Matt K Nov 18 '11 at 15:29
  • I am not familiar with the screen, can you explain it with a little bit more detail? – yasar Nov 18 '11 at 15:33
  • Interesting that ^a^d detaches--i've always just used ^ad. – William Pursell Nov 18 '11 at 18:47
8
ctrl + z 

will pause it. Than type

bg

to send it to background. Write down the PID of the process for later usage ;)

EDIT: I forgot, you have to execute

disown -$PID

where $PID is the pid of your process

after that, and the process will not be killed after you close the terminal.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • 1
    Processes that are sent to the background cannot be retrieved if you end the shell process. This is what will happen if the SSH connection drops. – Steve Rukuts Nov 18 '11 at 15:26
  • 1
    This would allow him to work on other things in the same session but if he disconnects this background process will be killed. – Fosco Nov 18 '11 at 15:27
  • yeah - I forgot to add "disown", sorry about that. – hovanessyan Nov 18 '11 at 15:34
  • Please don't use this, if you disown, you cannot (easily) get hold of the process again! There are way better other options like using `screen` – moritzg Dec 27 '21 at 17:47
2

you described it's important to protect script continuation. Unfortunately I don't know, you make any interaction with script and script is made by you.

  1. continuation protects 'screen' command. your connection will break, but screen protect pseudo terminal, you can reconnect to this later, see man.
  2. if you don't need operators interaction with script, you simply can put script to background at the start, and log complete output into log file. Simply use command:

    nohup /where/is/your.script.php >output.log 2&>1 &
    

    >output.log will redirect output into log file, 2&>1 will append error stream into output, effectively into log file. last & will put command into background. Notice, nohup command will detach process from terminal group.

    At now you can safely exit from ssh shell. Because your script is out of terminal group, then it won't be killed. It will be rejoined from your shell process, into system INIT process. It is unix like system behavior. Complete output you can monitor using command

    tail -f output.log   #allways breakable by ^C, it is only watching
    

    Using this method you do not need use ^Z , bg etc shell tricks for putting command to the background.

Notice, using redirection to nohup command is preferred. Otherwise nohup will auto redirect all outputs for you to nohup.out file in the current directory.

Znik
  • 1,047
  • 12
  • 17
0

You can use screen.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98