I have also heard quite some answers advising "ugly hacks". I beleve that installing third party utility to do the same thing you can do with default system capability is an ugly hack. Without some real gain that is.
From the time process is created by fork() and until it ends (disappears from process table) process goes trough many states (running, interruptable, uninterruptable,stopped, zombie).
State process is currently in depends on preceding event (system call, interrupt, request for resource that is not available...). We are going to focus on interupting process by suspending it (state stopped) and mainpulating by moving it between foreground and background (resuming it) and detaching it from it's parent.
Now fire up your terminal and let's get started.
For efficient process managament on linux system you should really need to know:
1. How to read (man and info pages are gold mine of usefull information)
2. Use ps (man ps)
3. Use lsof(man lsof) <-- it's way to complex to cover it in such short blog post but it's very worthwhile learning. In a nutshell it is used to list all open files and identify which process is using them. Since on *nixes everything is a file (yeah even network sockets), this command should not be left out of your toolbox. Today you should probably just run pstree as it gives the best presentation of process hirarchy. Although it's nothing you could not see with ps faux. I will not go into detail how it works but if you're interested there is a good explanation on http://en.wikipedia.org/wiki/Fork_(operating_system).
When you start an application from terminal here is what happens:
In a nutshell parent executes fork system call (basically clones itself (spawns another instance of itself)) and child process imediately executes exec system call which causes that program who executed it is replaced by the program who's name was passed as argument to exec system call. In another words PID stays the same. PID (process identifier) is as we know used by kernel to uniqely identify a process.
After fork() PID of a child is passed to a parent so it can refer to the child in system calls.
So what I will show you today is very simple but yet sometimes extremly handy to know. How to put process in background, how to put it in foreground, how to suspend it, how to detach child from parent process and how to keep process alive after your session has ended.
To put process into background thus enabling you to continue your work in the shell from which you have started it use &.
For example we want to start gedit from gnome terminal. We would just do:
bash-3.2$gedit &
This would start gedit text editor while still leave me with interactive prompt. Beware terminal you have started this process trough is still it's parent and upon exit it will kill it's children too unless you detach them.
To do this we use bash builtin disown.
Check your job id with jobs command. It displays background jobs for current shell.
All you need to do now is: disown -h %number.
So what is so special about that you might ask? Well combined with ctrl+z that suspends currently running process (sends it SIGTSTP signal) you can detach process from parent without knowing in advance that you would for some reason wish to do so (http://en.wikipedia.org/wiki/Signal_(computing) <-- more info on signals). When job is suspended it's state is stopped and it can be moved between foreground and background with fg and bg
commands. fg job_number (fg 1 <-- example) bg job_number (bg 1 <-- example)
All you need to do is check jobs: find a corresponding intiger value put the process into background disown it with disown -h %number (% is mandatory), so if it would be process with job id 1 syntax is: disown -h %1. Now you can kill the terminal that started the process and detached process will not exit. The other extremely useful utility is nohup. Nohup command preceding the process tells the process to ignore SIGHUP signal. In other process will continue to run after user session has ended. So, you could log in to remote system via ssh and do the following:
nohup ddrescue -r 3 /dev/sdb /mnt/backup/rescued.img
Now since you have used nohup this process will continue to run even after you log out on purpose or if the line breaks and you'r user session is ended forcefully. There is one thing I should mention here.
A superb application called GNU Screen (terminal multiplexer) also has (among many, many other options) and option to detach session (it is disconnected from the terminal and put it into the background) and latter on reattach it. It's a great application well worth trying out (http://www.gnu.org/software/screen/) but you do not need to install gnu screen for preventing process which was started durring your session to run after you disconnect. This was very common answer to questions (How can I keep my process running after I disconnect?) that should of been answered "nohup" on irc.
2 comments:
Nice article. I wondered why disown did not worked and I have failed to notice obligatory % parameter.
You can also suspend process by using bash native suspend command.
Thanks for the post, pstree and disown really useful tools.
Post a Comment