tags:

views:

1130

answers:

7

Possible Duplicate:
Linux: 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?

+2  A: 

Start in the background:

./long_running_process options &

And disown the job before you log out:

disown
diciu
but the programme has already started to run..
Shore
And will it contine to run as root?
Shore
If the program is already running on the console and you can access the console it's running on hit "Ctrl+Z" to send it in the background and then disown the newly created job. As long as a process is started as root it will continue to run as root unless it drops privileges itself.
diciu
But the command is named 'disown',isn't that to say root will disown the program,and the program will not continue to run as root ?
Shore
After stopping the process with "Ctrl+Z" you send it in the background with "bg" before disowning it.
diciu
"disown" is just for abandoning control of a job it does not change the privileges of the process.
diciu
+7  A: 

Have you tried using nohup and running it in the background?

nohup sleep 3600 &
paxdiablo
+9  A: 

I would try screen.

Janusz
While screen is a mighty nice tool, nohup is probably better suited for this task. Screen is only needed when you require the program to be interactive, and to be able to go back to the application at a later time. To be entirely honest, I often find myself using screen for the exact same reason as the question above.
wvdschel
Even with non-interactive task, it's nice to see that the program finished without errors. It's also good practice to always use screen in case of disconnection.
brunoqc
+1  A: 

You could use screen, detach and reattach

bananarepub
+7  A: 

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
Dennis Williamson
Can you explain to me what exactly happens after each step?
Shore
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...
Dennis Williamson
Dennis Williamson
+1  A: 

The nohup(1) idea is better than disown IMHO because disown is a shell-specific built-in of BASH while nohup is part of coreutils and likely to be everywhere.

Brian Reiter