Process Management in Linux

Everything which happens on linux server is a process. For that reason, process management is a key skill for the administrators. Process can be classified into two parts –

  • shell jobs (starts from the command line)
  •  Service Daemons

When the process starts, It can use multiple threads.

MANAGING SHELL JOBS

When the user types a command, a shell job is started as a foreground process, occupying the terminal.

RUNNING THE JOBS IN THE BACKGROUND AND FOREGROUND

Any command executed by you started in the foreground job. You can take two approaches to run a process in the background.

If you are sure that process will take a long time to complete, you can start with & behind it. It immediately starts the job in the background a creates a room for another command to run on the terminal.

to move the last job that was started in the background use fg command

If your job is taking lot more time than expected, you can use ctl+z to temporary stop the job. It does remove from the memory, it just paused, it can be managed later. You can continue this paused job using bg command in the background. Alternate you can use ctrl+c key sequence to stop the job.

When a process started from a shell, it automatically becomes a child process of the current shell. Parent-Child relationship in processes is important. The parent process is required to manage all the child processes. If the shell is terminated then all the processes will stop. If you started processes from the current shell in the background, then they will not be killed, when the shell is terminated. You have to use the kill command to terminate that process which is running in the background.

All the tasks typically started in the Linux as processes. One process can start multiple worker threads. If the process is very busy then threads can be handled by different CPUs or CPU cores available in the machine.  As an administrator, you can only manage process not thread. Each background process starts with a unique Process Identification Identified (PID). You can easily find the kernel thread because they have a name in square brackets. You can see kernel threads using the ps aux | head command. You can not manage the kernel threads even can not change the priority and kill.

GET CURRENTLY STARTED BY CURRENT USER

If you run ps command with any option, it displays currently started processes by the user.

look for a short summary of active processes

if you are not looking only for the name of the process but also for the exact command that was used to start the process

looking for the parent-child relationship

get process id by the process name. For example – Web server Apache is running on my machine.

or

ADJUST PROCESS PRIORITY

When linux processes are started, they are started with the specified priority. By default, all processes are equal and started with equal priority 20. If you have to need to change the priority, use nice and renice commands.

If you want to start a process with adjusted priority, use nice command and want to adjust the priority of an already running process, use renice command. You can also use r option with the top command to adjust the priority.

If you are thinking to adjust priority using nice or renice command, you can select values ranging from -20 to 19.  The default niceness of a process is set to 0 (which results in the priority value of 20). By applying a negative niceness, you increase the priority. Use a positive niceness to decrease the priority. It is a good idea not to use the
ultimate values immediately. Instead, use increments of 5 and see how it affects the application.

KILL PROCESS USING KILL AND PKILL

used uses PID to kill the process. First get the process is using ps aux | grep <process> or pgrep <processname>

LINUX PROCESS STATES

State Description
R (Running) The process is currently active and using CPU
S (Sleeping) The process is wating for event
Z (Zombie) The process has been stopped but could not be removed by the parent, which has put it is in an unmanageable state

 

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">