running a bash command in background

Red Squirrel

No Lifer
May 24, 2003
70,157
13,567
126
www.anyf.ca
How do I run a bash command completly in background and inderpendant of my session?

I have a script that prompts for a few values then runs a command using & at the end.

But I want to be able to close the ssh console, and the program continues. If I close the ssh console, then it stops the process.
 
 

Skeeedunt

Platinum Member
Oct 7, 2005
2,777
3
76
One way is to use nohup, e.g.

nohup whatever.sh &

I think "console" does the same thing, but lets you reconnect to the output stream again later, or something. I've never used it.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You can use nohup but after that it won't be possible to reattach to the process. You can see the commands output in the nohup.out file, unless you redirect it somewhere else but that's about it.

I personally prefer screen.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
I also use screen for this. Install screen with your package manager and then just type 'screen' to get into a session. Some useful commands within screen are "ctl-a, ?' for help, 'ctl-a, d' to detach from your screen session ( you can then exit the ssh console and screen will keep running in the background ), 'ctl-a, c' to create a new screen "window", 'ctl-a, n' to go to the next "windows", 'ctl-a, p' to go to the previous "window", and 'ctl-a, " ' to list all "windows". I used to keep these commands on a little cheat sheet next to my monitor, but they are so useful I used them enough to have them memorized in about a week.

After detaching from a screen session, you can reattach with 'screen -r', or show all screen session with 'screen -ls' but I always just open one session and then use ctl-a, c to create multiple windows within that session. You can get more info with 'man screen' or 'screen --help'.
 

DaiShan

Diamond Member
Jul 5, 2001
9,617
1
0
Heh screen is an absolute necessity if you use Gentoo :p screen emerge -u world *cross fingers*