Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a program continue to run after log out from ssh? [duplicate]

Tags:

linux

bash

ssh

Possible Duplicate:
Prevent a background process from being stopped after closing SSH client

I have a program that takes a lot of time to finish. It is running as root over ssh.
I want it to continue to run after I logout,is this possible and how would I achieve this?

like image 794
omg Avatar asked Jun 05 '09 04:06

omg


People also ask

Which command is used to keep a process running even after logout from shell?

When you want a process to continue running even after you log off a Linux system, you have a couple options. One of them is to use the disown command. It tells your shell to refrain from sending a HUP (hangup) signal to the process when you log off. So, the process continues running.

How do you keep a terminal session alive?

When you log in to the server, the terminal session won't automatically close. Instead, the configuration file will keep sending the alive signal after the specific interval set in the configuration file to keep the terminal session alive.

How do I keep a shell script running in the background?

Running shell command or script in background using nohup command. Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.


6 Answers

Assuming that you have a program running in the foreground, press ctrl-Z, then:

[1]+  Stopped                 myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout

If there is only one job, then you don't need to specify the job number. Just use disown -h and bg.

Explanation of the above steps:

You press ctrl-Z. The system suspends the running program, displays a job number and a "Stopped" message and returns you to a bash prompt.

You type the disown -h %1 command (here, I've used a 1, but you'd use the job number that was displayed in the Stopped message) which marks the job so it ignores the SIGHUP signal (it will not be stopped by logging out).

Next, type the bg command using the same job number; this resumes the running of the program in the background and a message is displayed confirming that.

You can now log out and it will continue running..

like image 151
Dennis Williamson Avatar answered Oct 04 '22 05:10

Dennis Williamson


You should try using nohup and running it in the background:

nohup sleep 3600 &
like image 41
paxdiablo Avatar answered Oct 04 '22 04:10

paxdiablo


I would try the program screen.

like image 20
Janusz Avatar answered Oct 04 '22 04:10

Janusz


Start in the background:

./long_running_process options &

And disown the job before you log out:

disown
like image 44
diciu Avatar answered Oct 04 '22 03:10

diciu


You want nohup. See http://nixcraft.com/linux-software/313-ssh-nohup-connection.html

like image 34
Matthew Flaschen Avatar answered Oct 04 '22 03:10

Matthew Flaschen


You could use screen, detach and reattach

like image 37
brndnmg Avatar answered Oct 04 '22 04:10

brndnmg