how to send ssh job to background

12,195

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
12,195
yasar
Author by

yasar

I am an economics student, who enjoys programming.

Updated on July 24, 2022

Comments

  • yasar
    yasar almost 2 years

    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.