Process management
In general, Linux is a relatively stable operating system (OS). Occasionally, things do go wrong, however, and sometimes you may want to tweak the system's running to better suit your needs. In this article, you can take a brief look at how you can manage programs or processes on Linux.
A program is a series of instructions that tell the computer what to do. When you run a program, those instructions are copied into the computer's memory, and space is allocated for variables and other stuff required to manage the program's execution. This running instance of a program is called a process, and it's processes that you manage.
Running processes
Like most modern OS, Linux is a multitasking operating system. This means that many processes can be running at the same time. As well as
the processes you are running, there may be other users on the system also running stuff, and the OS itself is usually running various processes to manage everything in general. To get a snapshot of what is currently happening on the system, use the top program.
top
Below is a simplified version of what you should see when you run this program.
user@bash: top
Tasks: 174 total, 3 running, 171 sleeping, 0 stopped
KiB Mem: 4050604 total, 3114428 used, 936176 free
Kib Swap: 2104476 total, 18132 used, 2086344 free
PID USER %CPU %MEM COMMAND
6978 ryan 3.0 21.2 firefox
11 root 0.3 0.0 rcu_preempt
6601 ryan 2.0 2.4 kwin
...
Let's break down the example above:
- Line 2—A task is just another name for a process. It's typical to have quite a few processes running on your system at any given time. Most of them are system processes. Many of them are typically sleeping. This is ok. It just means they are waiting until a particular event occurs, which they can act upon.
- Line 3—This is a breakdown of working memory (RAM). Don't worry if a large amount of your memory is used. Linux keeps recently used programs in memory to speed up performance if they are run again. If another process needs that memory, it can easily be cleared to accommodate this.
- Line 4—This is a breakdown of virtual memory on your system. If a large amount of this is in use, you may want to consider increasing its size. For most people with most modern systems having gigabytes of RAM, you shouldn't experience any issues here.
- Lines 6—10—This is a listing of the most resource-intensive processes on the system in the order of resource usage. This list updates in real time and so is interesting to watch to get an idea of what is happening on your system. The two important columns to consider are
%MEMand%CPU. If either of these is high for a particular process over a period of time, it may be worth looking into why this is so. TheUSERcolumn shows who owns the process, and thePIDcolumn identifies the process's ID, which is a unique identifier for that process.
The top program gives you a real-time view of the system and only shows the number of processes that fit on the screen.
Another program to look at
processes is called ps, which stands for processes. In its normal usage, it shows you just the processes running in your current terminal
(which is usually not very much). If you add the aux argument, it shows a complete system view, which is a bit more helpful.
ps [aux]
It does give quite a bit of output, so people usually pipe the output to grep to filter out just the data they are after. In the next bit, you will see an example of this.
Killing crashed process
It doesn't happen often, but when a program crashes, it can be quite annoying. Let's say you've got a browser running and, all of a sudden, it locks up. You try and close the window, but nothing happens, it has become completely unresponsive. No worries, you can easily kill Firefox and then re-open it. To start off, you need to identify the process id.
user@bash: ps aux | grep 'firefox'
ryan 6978 8.8 23.5 2344096 945452 ? Sl 08:03 49:53 /usr/lib64/firefox/firefox
It is the number next to the process owner that is the PID (Process ID). You use this to identify which process to kill. To do so, use a program appropriately called kill.
kill [signal] <PID>
Example
user@bash: kill 6978
user@bash: ps aux | grep 'firefox'
ryan 6978 8.8 23.5 2344096 945452 ? Sl 08:03 49:53 /usr/lib64/firefox/firefox
user@bash:
Sometimes, you are lucky, and running kill normally gets the process to stop and exit. When you do this, kill sends the default signal
(1) to the process, which effectively asks the process nicely to quit.
You always try this option first as a clean quit is the best option. Sometimes, this does not work, however. In the example above, ps was run again, and the process was still running. In this case, you can run kill again, but this time, supply a signal of 9, which effectively means—go in with a sledgehammer and make sure the process is well and truly gone.
user@bash: kill -9 6978
user@bash: ps aux | grep 'firefox'
user@bash:
Normal users can only kill those processes where they are owners. The root user on the system can kill anyone's processes.
Fixing locked-up desktop
On rare occasions, when a process crashes and locks up, it can lock up the entire desktop. If this happens, there is still hope.
Linux runs several virtual consoles. Most of the time, you only see console 7, which is the graphical user interface (GUI), but you can easily get to the others. If the GUI has locked up and you are in luck, you can get to another console and kill the offending process from there. To switch between consoles, use the CTRL + ALT + F Console** keyboard sequence .
So, CTRL + ALT F2 gets you to a console (if all goes well), where you can run the commands as above to identify process IDs and kill them. Then, CTRL + ALT F7 gets you back to the GUI to see if it has been fixed. The general approach is to keep killing processes until the lockup is fixed. Normally, you can look for tell-tale signs, such as high CPU or memory usage, and start with those processes first. Sometimes, this approach works, sometimes it doesn't, and you need to restart the computer.
Foreground and background jobs
You probably won't need to do too much with foreground and background jobs, but it's worth knowing about them just for those rare occasions. When you run programs normally (like it's been done so far), they are run in the foreground. Most of them run to completion in a fraction of a second as well.
Say, you need to start a process that takes a bit of time and happily does its thing without intervention from you, for instance, processing a very large text file or compiling a program, for instance.
In this case, you can run the program in the background, and then you can continue working. To demonstrate this, a program called sleep is used in the example below. All sleep does is wait a given number of seconds and then quit. You can
also use a program called jobs that lists currently running background jobs for us.
jobs
user@bash: sleep 5
user@bash:
If you run the above example yourself, you notice that the terminal waits 5 seconds before presenting you with a prompt again. Now, if you run
the same command but instead put an ampersand (&) at its end, you are telling the terminal to run this process in the background.
user@bash: sleep 5 &
[1] 21634
[1]+ Done sleep 5
This time, you will notice that it assigns the process a job number, tells you what that number is, and gives you the prompt back straight away.
You can continue working while the process runs in the background. If you wait 5 seconds or so and then hit ENTER, you will see a message come up telling you the job has been completed.
You can move jobs between the foreground and background as well. If you press CTRL + z, the currently running foreground process is paused and moved into the background. You can then use a program called fg, which stands for foreground, to bring background processes into the foreground.
fg <job number>
Example
user@bash: sleep 15 &
[1] 21637
user@bash: sleep 10
(you press CTRL + z, notice the prompt comes back.)
user@bash: jobs
[1]- Running sleep 15 &
[2]+ Stopped sleep 10
user@bash: fg 2
[1] Done sleep 15
user@bash:
CTRL + z is used in Windows to run the undo command. It is not uncommon for people from the Windows world to accidentally hit the key combo (especially in the Vim editor, for instance) and wonder why their program just disappeared and the prompt returned.
If you do this, don't worry, you can use jobs to identify which job it has been assigned to and then fg to bring it back and
continue working.
Activities for practicing
Try the following:
- First off, start a few programs on your desktop. Then, use
psto identify their PID and kill them. - Now, see if you can do the same, but switch to another virtual console first.
- Finally, play about with the
sleepcommand and moving processes between the foreground and background.