In Unix systems, interactive processes refer to processes which are created from terminal sessions. This blog post aims to explain the basics of controlling interactive process in Unix systems.
Foreground and background process
There are two types of interactive process in Unix systems, namely foreground and background process. Foreground process is a process that runs continuously and only returns control to the shell after it finishes. Background process, on the other hand, refers to a process that runs in the background while the user has the control of the shell.
Executing a process in foreground and background mode
Since there are two kinds of interactive process, you can control a process’ mode when you execute it. If you want to run a process as a foreground process, just run it from your terminal. If you want to run in background mode, you have to append &
to your command.
$ sleep 60 &
When you start a background process, the control of shell is returned to you and you can continue typing other commands.
Managing background processes
To list all background processes, use command jobs
.
$ sleep 60 &
[1] 5483
$ sleep 60 &
[2] 5488
$ jobs
[1] - running sleep 60
[2] + running sleep 60
Each background process is assigned a number (in square brackets). If you want to kill a background process, use kill %n
where n
is the assigned number.
$ kill %1
[1] - 5483 terminated sleep 60
Switching between the modes
From foreground to background
If you want to switch a process to background in the middle of execution, you should first suspend it with the SIGTSTP
signal by typing ^Z
(Ctrl + Z). Then type bg
to resume execution in background mode. If you have multiple stopped processes, you can specify which process to resume with bg %n
.
$ sleep 60
^Z
[1] + 5513 suspended sleep 60
$ bg %1
[1] - 5513 continued sleep 60
From background to foreground
It’s also possible to bring a process back to foreground mode from background mode. First, you need to find out the assigned number of that process from the jobs
command. Then, use the fg %n
command to bring a process back to foreground mode.
$ sleep 60 &
[1] 5561
$ jobs
[1] + running sleep 60
$ fg %1
[1] + 5561 running sleep 60
Conclusion
This post discussed the basics of interactive process management in Unix systems. With these commands, you can easily manage interactive process (e.g. to change its execution mode). Hope you find these useful in your daily work on Unix systems.
comments powered by Disqus