I am not a expert in unix. I am basically a windows guy and have been using it for the last 6 years. But due to the kind of assignment I am currently involved in, I had to switch to Ubuntu. And I must admit that I am enjoying it. The old college-days impression about unix has started to fade away now, and now I get excited as soon as I get to learn some new cool unix command to ease a task.
Recently I was using a VMware image which runs my weblogic installation. I just don’t like to switch back and forth between my machine and the terminal in the image window, so I use ssh to login into the VMware image machine and then run commands from the same terminal.
I started to feel the pain when I had to keep a terminal window open just because I started the weblogic server which is in the VMware image. I thought “Why the hell I have to keep a terminal open when all I wanted is to start the weblogic server and nothing else. Even if I want to stop the server after some time, can’t I simply do a ssh to the VMware image again and stop the server?”
Although I was aware of a way to run a process in background using the ‘&’ and some basic usage of ‘bg’ and ‘fg’ commands, that didn’t solve my problem completely. Sometimes, the terminal running the ssh would hang and yet some other time, it would kill the process while logging out from the server.
Then I came to know about these 3 ways to accomplish what I wanted:
1. Redirecting the background process stdin/stdout/stderr streams (e.g. to files, or /dev/null if you don’t care about them). For example, in my case I had to start the weblogic server in the background(so that it is not stopped when I log out), what I used was:
[root@localhost ~]# ./startWebLogic.sh < /dev/null >& /dev/null &
now, I could log-out from the session and the process would continue to run( I tested it by accessing the weblogic console). For complete explanation of it, please check the ssh faqs.
2. Use the nohup: It provides the same functionality as the above one and more simpler to use.

as you can see, the output is appended to nohup.out, so if you login again after some time, you can see the logs for a process in this file.
3. Using a screen: This was the exciting one. Although the above two methods work, they are not that elegant. Once you have started to process(using above techniques) and log-out, and then you again log-in into the terminal using ssh, the only way to find out about the running process is using a ps -ef thingy. For example, when I wanted to stop the weblogic server, I used to call:
ps -ef | grep weblogic
to find out the process details corresponding to the running weblogic server and then I would call kill -9 pid to kill the server. Not elegant! I am still looking for a better way to get hold of the previously started process and then stop it.
The screen utility, on the other hand, provides a much elegant solution. In a nutshell, you can start(and enter into) a new named screen in the same terminal, do whatever commands you like or start a process it it, then detach the screen. and that’s it. You can log-out from the session and the screen would still be alive on the remote machine. And the best part, when you again choose to go back to the terminal, just log-in and re-get the screen. Here is how I do it:
rkumar@rkumar-laptop:~$ ssh root@wls-103
root@wls-103's password:
Last login: Sun Jul 26 08:16:55 2009 from 172.16.152.1
[root@localhost ~]# screen -dR weblogic_screen
above, I have opened a new screen named ‘weblogic_screen’. This opens a new screen in the same terminal(and not a new tab). In the new screen and I would go to the weblogic domain directory and then start my server there.
[root@localhost ~]# cd /opt/bea-10.3/user_projects/domains/adDomain/
[root@localhost adDomain]# ./startWebLogic.sh
then to detach my screen(without killing it), I will type Ctrl+a,d(Ctrl a then d). This would bring you back to your original screen and you continue to work with your original task or log-out of the session if you wish.
Now, if I would like to stop the running weblogic server, I would simply do a ssh to my VMware image, then check for all running screens using the following command:
[root@localhost ~]# screen -ls
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Detached)
2 Sockets in /var/run/screen/S-root.
It shows that I have the weblogic_screen still running. To enter again into the screen, I would again call ‘screen -dR weblogic_screen’ command and that’s it. I am there. I can stop the server and again detach the screen using Ctrl a,d.
One last thing, if you want to ‘wipe-out’ a screen, you first kill the process then use screen -wipe command. For example, in my case, if I want to delete the screen named ‘weblogic’(because I don’t need it any more) then I will do something like this:
[root@localhost ~]# kill -9 12322
[root@localhost ~]# screen -ls
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Dead ???)
Remove dead screens with 'screen -wipe'.
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# screen -wipe
There are screens on:
13490.weblogic_screen (Detached)
12322.weblogic (Removed)
1 socket wiped out.
1 Socket in /var/run/screen/S-root.