20 June 2021
Sometimes it happen that inside the same terminal I need to do different things, so I need to stop some process and recalling some others.
Fortunately a Unix command come to the rescue: jobs.
Just to have a clean environment start a container
docker run -it ubuntu /bin/bash
# apt update && apt install -y vim
Show current top process
# top
top - 05:13:23 up 30 min, 0 users, load average: 1.73, 1.95, 1.80
Tasks: 2 total, 1 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 26.7 us, 6.2 sy, 0.0 ni, 65.8 id, 0.0 wa, 0.0 hi, 1.2 si, 0.0 st
MiB Mem : 2124.0 total, 2024.2 free, 100.0 used, 1539.4 buff/cache
MiB Swap: 164.0 total, 164.0 free, 0.0 used. 984.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 4116 3468 2916 S 0.0 0.0 0:00.07 bash
10 root 20 0 6108 3360 2824 R 0.0 0.0 0:00.01 top
Then press Ctrl + z
to send it to background
[1]+ Stopped top
Check process stopped via jobs:
# jobs
[1]+ Stopped top
Start a new process in background using &
at the end of the command:
# vi /tmp/baground.txt &
[2] 608
[2]+ Stopped vi /tmp/baground.txt
Using jobs could be find both:
# jobs
[1]- Stopped top
[2]+ Stopped vi /tmp/baground.txt
Recall second task (vi /tmp/baground.txt
) using %2
, so if we want recall the first command we need to use %1
:
# fg %2
Exit to vi using :q!
(exit without saving)
# jobs
[1]- Stopped top
The last command we use in this example is to close top sending a SIGTERM using kill command:
# kill %1
Full commands descriptions could be found running
jobs --help
fg --help
bg --help
kill --help