How to Make Linux Run Faster

Share This Post:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
How to make Linux Run Faster

Contrary to what you may seen on TV, the secret of knowing how to make Linux run faster does not involve putting faster a hamster inside your machine.

Make Linux Run Faster
Adding more hamster wheels to your Linux system will not make it run faster.

The secret to knowing how to make Linux run faster is understanding how Linux handles and runs applications on the system. Linux tracks of lots of programs, all running at the same time. In this article, Secur shows:

  • How Linux tracks active programs and how you can peek at that information.
  • How to make Linux run faster by using command-line tools to manage programs running on the system.

How to Check The Processes Running in Linux

On any given Linux system, there are many programs running simultaneously. A process, which is the Linux term for an active program, can run in the:

  • Foreground which displays output to a console display/graphical desktop
  • Background, working on data.

In order to control all the chaos, Linux assigns each process a process id (PID) and manages how the process uses memory and CPU time based on that PID.When a Linux system first boots, it starts a special process called the init process; it is the core of the Linux system runs scripts that start all of the other processes running on the system.  As seen in the screenshot below, the “ps” command let’s you watch which processes are currently running on your Linux system; the command shows the processes that are running in the current user shell, the command prompt shell running (Bash) and the “ps” command.

Using the ps command to view processes

The output of the “ps” command shows:

  • The PID assigned to each process,
  • The termi-nal (TTY) that they were started from,
  • The CPU time that the process has used.

The “ps” program used in Linux supports three different styles of command-line parameters:

  • BSD-style parameters: not preceded by a dash
  • GNU long parameters: preceded by a double dash
  • Unix-style parameters: preceded by a dash

This makes for lots of possible parameters,  options (and confusion) when using the “ps” command.   The screenshot below show the use of the command “ps -ef” to see every process running on the system.

  • Some process command names are shown in brackets, indicating processes that are “sleeping” or currently swapped from physical memory into virtual memory on the hard drive.
    • Linux kernel places a process into sleep mode while the process is waiting for an event.
    • When the event triggers, the kernel sends the process a signal.
      • If the process is in interruptible sleep mode, it  receives the signal immediately and wake up.
      • If the process is in uninterruptible sleep mode, it only wakes  based on an external event, such as hardware becoming available.
        • It will save any other signals sent while it was sleeping and act on them once it wakes up.
    • A process is considered a zombie if it ended but its parent process hasn’t acknowledged the termination signal because it’s sleeping, 
      • It’s stuck in a limbo state between running and terminating until the parent process acknowledges the termination signal.
viewing Linux process with the ps -ef command
You can see every process running on a Linux system using the "ps -ef" command.

The headings in the output correspond to the following:

  • UID: user responsible for running the process
  • PID:  process ID
  • PPID: process ID of the parent process, if started by another process
  • C: processor utilization over the lifetime of the process
  • STIME: system time when the process was started
  • TTY: terminal device from which the process was started
  • TIME: cumulative CPU time required to run the process
  • CMD: name of the program that was started in the process

Additionally, Linux’ “time” command helps in troubleshooting applications; the syntax is to include a running  application as a parameter to the time command in order to display the amount of CPU time the application consumed.

How to Monitor Linux Processes in Real Time

Identifying trends about processes frequently swapped in and out of memory is hard to do that with the “ps” command; in these situations, use the “top” command (seen in the screenshot below) to display real-time process information.

top command Make Linux Run Faster
The "top" command displays process information in real-time mode.

Assuming you don’t have any serious visual impairments, you can see the output from the “top” command is split into two parts:

  • Top Section: Shows general system information.
    • The first line shows:
      • The current time
      • How long the system has been up,
      • The number of users logged in
      • The load average on the system.
        • Appears as three numbers, the 1-minute, 5-minute, and 15-minute load averages.
        • The higher the values, the more load the system is experiencing.
        • 1-minute load value to be high for short bursts of activity; if the 15-minute load value is high, your system may be in trouble.
    • The second line shows tasks (general process information):
      • How many are running, sleeping, stopped, or in a zombie state.
    • The third line shows general CPU utilization information broken down into several categories depending on the owner of the process (user versus system processes) and the state of the processes (running, idle, or waiting).
    • Fourth and fifth lines  detail the status of the system memory:
      • The first line shows the status of the physical memory in the system (How much total memory there is, how much is currently being used, and how much is free).
      • The second  line shows the status of the swap memory area in the system
  • Bottom section:  Detailed list similar to the ps command output of the currently running processes.  By default, when you start top it sorts the processes based on the %CPU value.
    • PID: The process ID of the process
    • USER: The user name of the owner of the process
    • PR: The priority of the process
    • NI: The nice value of the process
    • VIRT: The total amount of virtual memory used by the process
    • RES: The amount of physical memory the process is using
    • SHR: The amount of memory the process is sharing with other processes
    • S: The process status (D = interruptible sleep, R = running, S = sleeping, T = traced or stopped, and Z = zombie)
    • %CPU: The share of CPU time that the process is using
    • %MEM: The share of available physical memory the process is using
    • TIME+: The total CPU time the process has used since starting
    • COMMAND: The command-line name of the process (program started)

Linux users have lots of control over the output of the “top” command:

  • Use the “F” or “O” command to toggle which field the sort order is based on. 
  • Use the “r” interactive command to reverse the current sorting and find the offending processes that have taken over your system.

The table below shows more of the commands you can use to change the sort order of “top“; these commands are single characters you can press while “top” is running:

Command Description
qExits the top command.
1Toggles the single CPU and Symmetric Multiprocessor (SMP) state
bToggles the bolding of important numbers in the tables
I (upper capital “i”)Toggles Irix/Solaris mode
zConfigures color for the table
l (lower case “L”)Toggles display of the load average information line
tToggles display of the CPU information line
mToggles display of the MEM and SWAP information lines
fAdds or removes different information columns
oChanges the display order of information columns
F or OSelects a field on which to sort the processes (%CPU by default)
< or >Moves the sort field one column left (<) or right (>).
rToggles normal or reverse sort order. Will change the priority/renice
of a specific process if process owner or if root user
hToggles showing of threads
cToggles showing of the command name or the full command line
(including parameters) of processes
iToggles showing of idle processes
SToggles showing of the cumulative CPU time or relative CPU time
xToggles highlighting of the sort field
yToggles highlighting of running tasks
uShows processes for a specific user
n or #Sets the number of processes to display
kKills a specific process (only if process owner or if root user)
d or sChanges the update interval (default three seconds)
WWrites current settings to a configuration file

How to Manage Runaway Linux Processes

Managing runaway Linux processes is a part of knowing how to make Linux run faster.  As we’ve already gone through how to  monitor how processes are doing on the system, we are now going to see how to kill a runaway process. 

How To Set Application Priority Levels

Before diving into managing processes as a way to make Linux run faster, keep in mind that the kernel is the final judge in determining application priority levels, regardless of what you set; if the Linux system is overloaded, the kernel can ignore any nice or renice commands.

Out of the box, all Linux processes all have the same priority to obtain CPU time and memory resources; some applications don’t need to have the same level of priority and you can use the “nice” and “renice” commands to set and change the priority level assigned to an application process.

 

How To Use nice Command in Linux

The “nice” command starts an application with a non-default priority setting; the syntax is as follows:
nice -n <value> <command>
The “value” parameter: Numeric value from -20 to 19 (default priority is zero.). The lower the number, the higher priority the process receives; only the root user account can set a priority value less than 0 or decrease the priority value of a running process. The “command” parameter: indicates the program to start at the specified priority The “renice” command changes the priority of multiple processes based on:
  • A list of PID values
  • All of the processes started by one or more users
  • All of the processes started by one or more groups.
Change the priority of a process that’s already running with the “renice” command:
renice priority [-p pids] [-u users] [-g groups]
 

How To Stop A Process in Linux

One of the things that you notice as you learn how to make Linux run faster is that process gets hung up, stop or run away with the CPU In both cases, you need a command that will allow you to control a process.

In Linux, processes communicate with each other using “process signals“, a predefined message that processes recognize and may choose to ignore or act on.  Program developers determine how a process handles signals and most applications have the ability to receive and act on the standard process signals.  A list of these signals are available below:

Signal NamesNumber for OptionDescription
SIGHUP1This is known as the Hangup (HUP) signal. It stops the
designated process and restarts it with the same PID.
SIGINT2This is the Interrupt (INT) signal. It is a weak kill signal that
isn’t guaranteed to work, but it works in most cases.
SIGQUIT3This is known as the core dump. It terminates the process
and saves the process information in memory, and then it
saves this information in the current working directory to a
file named core.
SIGKILL9This is the absolute kill signal. It forces the process to stop
by sending the process’s resources to a special device, /dev/null.
SIGSEGV11Segment Violation
SIGTERM15This is the Termination (TERM) signal. It is the kill command’s
default kill signal.
SIGSTOP17Stops unconditionally, but doesn’t terminate
SIGTSTP18Stops or pauses, but continues to run in background.
SIGCONT19Resumes execution after STOP or TSTP

How to Use The Linux kill Command

Let’s open this segment of the article with a public service announcement, be cautious killing processes that may have open files as they may be damaged and unrepairable if the process is abruptly stopped, so run “lsof” command first to see a list of the open files and their processes.

The “kill” command sends signals to processes based on their process ID (PID).  The default signal is  TERM and when using the command line,  you can only use the process PID of an application rather than its command name, making the command’s use impractical.
To send a process signal, you must either be:

  • The owner of the process.
  • Logged in as the root user.

The generally accepted procedure is to first try the TERM signal. If the process ignores that, try the INT or HUP signal.

  • The TERM signal only “asks” the process to kindly stop running, so chances are if you have a runaway process, use the “-s” parameter or you will see the request likely ignored the request.
    • If the program recognizes these signals, it will try to gracefully stop doing what it was doing before shutting down.
  • The most forceful signal is the KILL signal. When a process receives this signal, it immediately stops running.
    • Use this as a last resort, as it can lead to corrupt files.
    • There’s no output from it.
      • To see if the command was effective, run another ps or top command to see if the target process stopped.

How To Use The Linux pkill Command

The pkill command allows you to stop processes by using their names (and wildcard characters) rather than the PID numbers. Be careful with the search capability of the pkill command.  Check the search term with the “pgrep” command allows you to display all processes that match the search term to make sure you don’t accidentally kill any other processes that match the search.

Share This Post:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents

You May Like

Related Posts

Getting started with wireshark
Hacking
Linux Administrator

Getting Started With Wireshark

So, you’ve hit that point in your journey where you’ve decided getting started with Wireshark was a move in the right direction. Congratulations for joining

Read More »