Linux/Unix Process Management: ps, kill, top, df, free, nice Commands & Process Scheduling - STUDY NOTES

Post Top Ad

Linux/Unix Process Management: ps, kill, top, df, free, nice Commands & Process Scheduling


What is a process?

An instance of a program is called a Process. In simple terms, any command that you give to your Linux machine starts a new process.



It's possible to have multiple processes for the same program.

Types of Processes:

  • Foreground Processes: They run on the screen and need input from the user. For example: Office Programs
  • Background Processes: They run in the background and usually do not need user input. For example: Antivirus.

Running a foreground Process


To start a foreground process, we can either run it from the dash board or we can run it from the terminal.

When using the Terminal, we will have to wait, until the foreground process runs. 



Running a Background process 

If we start a foreground program/process from the terminal, then we cannot work on the terminal, till the program is up and running.

Certain, data intensive tasks take lots of processing power and may even take hours to complete. we do not want our terminal to be held up for such a long time.

To avoid such a situation, we can run the program and send it to the background so that terminal remains available to us.

 


Fg

You can use the command "fg" to continue a program which was stopped and bring it to the foreground.

The simple syntax for this utility is: 

fg



Example

1.     Launch ’banshee' music player
2.     Stop it with the 'ctrl +z' command
3.    Continue it with the 'fg' utility.
 

 
  

Top 

This utility tells the user about all the running processes on the Linux machine.


 

Press 'q' on the keyboard to move out of the process display.

The terminology follows:


Field
Description
Example 1
Example 2
PID
The process ID of each task
1525
961
User
The username of task owner
Home
Root
PR
Priority Can be 20(highest) or -20(lowest)
20
20
NI
The nice value of a task
0
0
VIRT
Virtual memory used (kb)
1775
75972
RES
Physical memory used (kb)
100
51
SHR
Shared memory used (kb)
28
7952
S
Status
There are five types:
          'D' = uninterruptible sleep
          'R' = running
          'S' = sleeping
          'T' = traced or stopped
          'Z' = zombie
S
R
%CPU
% of CPU time
1.7
1.0
%MEM
Physical memory used
10
5.1
TIME+
Total CPU time
5:05.34
2:23.42
Command
Command name
Photoshop.exe
Xorg

PS


This command stands for 'Process Status'.  It is similar to the "Task Manager" that pop-ups in a Windows Machine when we use Cntrl+Alt+Del . This command is similar to 'top' command but the information displayed is different.

To check all the processes running under a user, use the command - ps ux
To check the process status of a single process, use the syntax - ps PID 


Kill 

This command terminates a running processes on a Linux machine.

In order to use this utility we need to know the PID (process id) of the process we want to kill

Syntax -

kill PID

To find the PID of a process simply type

pidof  Processname

 


How to Change Process Priority

Every running process in Linux has a priority assigned to it.

Linux can run a lot of processes at a time, which can slow down the speed of some high priority processes and result in poor performance.

To avoid this, we can change the process priority using nice and renice utility. This priority is called Niceness in Linux. Nice command will launch a process with a user defined scheduling priority. Renice command will modify the scheduling priority of a running process. It has a value between -20 to 19. The lower the Niceness index the higher would be priority given to that task.

A nice value of -20 represents highest priority, and a nice value of 19 represent least priority for a process.

The default value of all the processes is 0.

To start a process with a niceness value other than the default value use the following syntax

nice -n 'Nice value' process name

 


If there is some process already running on the system, then you can 'Renice' its value using syntax. 

renice 'nice value' -p 'PID' 

To change Niceness we can use the 'top' command to determine the PID (process id) and its Nice value.  Later use the renice command to change the value.

Let us understand this by an example.

 



DF


This utility reports the free disk space(Hard Disk) on all the file systems.

If you want the above information in a readable format , then use the command 

'df -h'



Free

This command shows the free and used memory (RAM) on the Linux system.

we can use the arguments

free -m to display output in MB

free -g to display output in GB



Scheduling processes



If we want to schedule some programs to run at later time or at a particular time, then we use some scheduling commands.

There are two main tools used to perform scheduled tasks, at and cron. There is an another tool named anacron if our computer does not run continuously, as cron will only work if computer is left on (anacron can catch up with the scheduled tasks the next time the computer is on...). 


at command 

'at' executes a command once on a particular day, at a particular time. at will add a particular command to be executed. 

1. Schedule first job using at command

The at command schedules a command to be run once at a particular time that we normally have permission to run. The at command can be anything from a simple reminder message, to a complex script. We can start by typing ‘at’ command at the command line, passing it the scheduled time as the option. It then places us at a special prompt, where we can type in the command (or series of commands) to be run at the scheduled time. When all done, press Control-D on a new line, and our command will be placed in the queue.

A typical at command sequence looks like this:


at 9:30 PM Tue

warning: commands will be executed using /bin/sh

at> echo "Well gosh golly, it's 9:30 PM on Tuesday."

at> ^D

job 1 at Tue Sep 04 09:30:00 2017

When we ran the command, the first thing at did was give us a "warning" telling us what command shell our commands will be run with: in this case, /bin/sh, the Bourne Shell. This shell is the traditional standard Unix shell.

It then places us at the at> prompt. Here we type in a simple echocommand, which echoes a string of text. We press enter, and we're placed at a new at> prompt. We then press Control-D, telling at we're all done with our commands. It then tells us that our job is job number 1 and that it will run next Tuesday.


2. List the scheduled jobs using atq

When we list jobs by root account using atq , it shows all users jobs in result. But if we execute it from non root account, it will show only that users jobs.

# atq

1       2017-09-04 09:30 a root
5       2017-09-04 10:00 a rahul
3       2017-09-04 12:00 a root

Fields description:
First filed: job id
Second filed: Job execution date
third filed: Job execution time
Last field: User name, under which job is scheduled.


3. Remove scheduled job using atrm

You can remove any at job using atrm with their job id.
 
# atrm 3

# atq

1       2017-09-04 09:30 a root

5       2017-09-04 10:00 a Rahul  


4. Check the content of scheduled at job

atq command only shows the list of jobs but if you want to check what script/commands are scheduled with that task, below example will help you.

# at -c 5

cron

cron can be used to schedule a particular function to occur every minute, hour, day, week, or month.

It's normal to use the crontab to perform the editing functions as this automates the process for the cron daemon and makes it easier for normal users to use cron.


Anacron 

anacron is another tool designed for systems which are not always on, such as home computers.

While cron will not run if the computer is off, anacron will simply run the command when the computer is next on (it catches up with things).


Crontab

Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon for running commands on a repeating schedule.

crontab [-u user] [file]

crontab [-u user] -l | -r [-i] | -e

-u user           Specify which user’s schedule to adjust (only for root). The default is for your own account. 

-l          List the currently scheduled commands

-r         Remove the currently scheduled commands

-I         Prompt the user to confirm removal of the scheduled commands

-e        Edit the list of scheduled commands
  

Batch command 

The batch command is used to schedule a one-time task to be executed when the systems load average drops below 0.8.

After typing the batch command, the at> prompt is displayed. Type the command to execute, press [Enter], and type Ctrl-D. More than one command can be specified by typing each command followed by the [Enter] key. After typing all the commands, press[Enter] to go to a blank line and type Ctrl-D. As soon as the load average is below 0.8, the set of commands or script is executed.

If the set of commands or script tries to display information to standard out, the output is emailed to the user. 

Linux sleep command 

The sleep command is used to delay for a specified amount of time defined by NUMBER.

sleep NUMBER[SUFFIX]

Where SUFFIX may be:

1.    s for seconds (the default)
2.    m for minutes.
3.    h for hours.
4.    d for days.

Examples

To sleep for 5 seconds, use:

sleep 5

To sleep for 2 minutes, use:

sleep 2m

To sleep for 3 hours, use:

sleep 3h

To sleep for 5 days, use:

sleep 5d

To sleep for 1.5 seconds:

sleep 1.5

To sleep for .5 seconds:

sleep .5




https://books2notes.blogspot.in/p/redirection-in-linuxunix.htmlhttps://books2notes.blogspot.in/p/redirection-in-linuxunix.html

No comments:

Post a Comment

Post Bottom Ad