yasar
yasar

Reputation: 13758

how to send ssh job to background

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.

Upvotes: 9

Views: 14799

Answers (4)

Znik
Znik

Reputation: 1146

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.

Upvotes: 2

hovanessyan
hovanessyan

Reputation: 31463

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.

Upvotes: 8

Alex Howansky
Alex Howansky

Reputation: 53581

You can use screen.

Upvotes: -1

Steve Rukuts
Steve Rukuts

Reputation: 9367

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.

Upvotes: 10

Related Questions