How to send ssh work to background

I went to the remote server via ssh and ran the php script. Seems like it will take 17 hours to complete, is there a way to break the connection but keep the script running? I did not redirect any output, so I see all the output.

+6
source share
4 answers

Can you stop the process right now? If yes, start screen , start the process and open the screen using ctrl-a, then ctrl-d. Use the -r screen to get a session later.

This should be available on most distributions, otherwise the package will definitely be available to you.

+7
source
ctrl + z 

will stop him. Than type

 bg 

to send him to the background. Record the PID of the process for later use;)

EDIT: I forgot, you need to execute

 disown -$PID 

where $ PID is the pid of your process

after that, and the process will not be killed after the terminal is closed.

+6
source

You can use screen .

0
source

You have described that it is important to protect the continuation of the script. Unfortunately, I do not know if you are doing any interaction with script and script.

  • continued protects the 'screen' command. your connection will break, but the screen will protect the pseudo-terminal, you can connect to it again, see a person.
  • if you don't need script interaction operators, you can just put the script in the background at the beginning and end the logging to the log file. Just use the command:

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

    output.log redirects the output to the log file, 2 &> 1 will add a stream of errors to the output, effectively to the log file. last & will put the command in the background. Note: the nohup command will disconnect the process from the terminal group.

    Now you can safely exit the ssh shell. Since your script is outside the terminal group, it will not be killed. It will be returned from your shell process to the INIT system process. This is the unified behavior of the system. Full output that you can control with the command

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

    Using this method, you do not need to use ^ Z, bg tricks, etc. To place a team in the background.

Note that using redirection to the nohup command is preferred. Otherwise, nohup will automatically redirect all outputs to the nohup.out file in the current directory.

0
source

Source: https://habr.com/ru/post/901813/


All Articles