Unit-4 Process in Linux
Unit-4 Process in Linux
PROCESS IN LINUX
Processes in Linux
A program/command when executed, a special instance is provided by the system
to the process. This instance consists of all the services/resources that may be
utilized by the process under execution.
• Whenever a command is issued in Unix/Linux, it creates/starts a new
process. For example, pwd when issued which is used to list the current
directory location the user is in, a process starts.
• Through a 5 digit ID number Unix/Linux keeps an account of the
processes, this number is called process ID or PID. Each process in the
system has a unique PID.
• Used up pid’s can be used in again for a newer process since all the
possible combinations are used.
• At any point of time, no two processes with the same pid exist in the
system because it is the pid that Unix uses to track each process.
Initializing a process
A process can be run in two ways:
Method 1: Foreground Process : Every process when started runs in foreground
by default, receives input from the keyboard, and sends output to the screen. When
issuing pwd command
$ ls pwd
Output:
$ /home/BCA/root
When a command/process is running in the foreground and is taking a lot of time,
no other processes can be run or started because the prompt would not be available
until the program finishes processing and comes out.
Output:
[1] + Done pwd
$
That first line contains information about the background process – the job number
and the process ID. It tells you that the ls command background process finishes
successfully. The second is a prompt for another command.
Column Meaning
UID User ID of the process owner (52471 in this case)
PID Process ID (unique identifier for the process)
PPID Parent Process ID (the process that spawned this process)
C CPU utilization (not significant here since it's 0)
STIME Start time of the process (07:20 and 08:04)
TTY Terminal associated with the process (pts/1)
CPU time used by the process (both are 00:00:00, meaning they used
TIME
negligible CPU)
CMD Command that started the process (sh, ps -f)
There are other options which can be used along with ps command :
• -a: Shows information about all users
• -x: Shows information about processes without terminals
• -u: Shows additional information like -f option
• -e: Displays extended information
Stopping a process:
When running in foreground, hitting Ctrl + c (interrupt character) will exit the
command. For processes running in background kill command can be used if it’s
pid is known.
$ ps –f
$ kill 19
Terminated
If a process ignores a regular kill command, you can use kill -9 followed by the
process ID.
$ kill -9 19
Terminated
Other process commands:
bg: A job control command that resumes suspended jobs while keeping them
running in the background
Syntax:
bg [ job ]
For example:
bg %19
fg: It continues a stopped job by running it in the foreground.
Syntax:
fg [ %job_id ]
For example
fg 19
top: This command is used to show all the running processes within the working
environment of Linux.
Syntax:
top
nice: It starts a new process (job) and assigns it a priority (nice) value at the same
time.
Syntax:
nice [-nice value]
nice value ranges from -20 to 19, where -20 is of the highest priority.
renice : To change the priority of an already running process renice is used.
Syntax:
renice [-nice value] [process id]
df: It shows the amount of available disk space being used by file systems
Syntax:
df
Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/loop0 18761008 15246876 2554440 86% /
none 4 0 4 0% /sys/fs/cgroup
udev 493812 4 493808 1% /dev
tmpfs 100672 1364 99308 2% /run
none 5120 0 5120 0% /run/lock
none 503352 1764 501588 1% /run/shm
none 102400 20 102380 1% /run/user
/dev/sda3 174766076 164417964 10348112 95% /host
free: It shows the total amount of free and used physical and swap memory in the
system, as well as the buffers used by the kernel
Syntax:
free
Output:
total used free shared buffers cached
Mem: 1006708 935872 70836 0 148244 346656
Types of Processes
1. Parent and Child process : The 2nd and 3rd column of the ps –f
command shows process id and parent’s process id number. For each
user process, there’s a parent process in the system, with most of the
commands having shell as their parent.
2. Zombie and Orphan process : After completing its execution a child
process is terminated or killed and SIGCHLD updates the parent process
about the termination and thus can continue the task assigned to it. But at
times when the parent process is killed before the termination of the
child process, the child processes become orphan processes, with the
parent of all processes “init” process, becomes their new pid.
A process which is killed but still shows its entry in the process status or
the process table is called a zombie process, they are dead and are not
used.
3. Daemon process : They are system-related background processes that
often run with the permissions of root and services requests from other
processes, they most of the time run in the background and wait for
processes it can work along with for ex print daemon.
When ps –ef is executed, the process with ? in the tty field are daemon
processes.
The ‘at’ is a command that allows the users to schedule one-time tasks or recurring
jobs at a specific time and date. It is mainly useful for automating the system
maintenance, backups, software updates, and various administrative tasks. The
‘at’ command works by following queuing mechanism for the commands to be
executed by the system’s job scheduler at the specified time.
When to use the ‘at’ utility
The ‘at' utility is best used for scheduling one-time tasks to run at a specific time in
the future. Here are some scenarios where at is particularly useful:
Example:
echo "echo 'Backup complete'" | at 2am
This schedules a one-time task to echo “Backup complete” at 2 AM.
Syntax:
The basic syntax of the AT command is straightforward:
at [-V] [-q queue] [-f file] [-mldbv] timespec
• ‘-l’: List the at jobs in the queue.
• ‘-d’: Delete the at job specified by the job number.
• ‘-b’: Submit a batch job. This is the default behavior.
• ‘-v’: Display verbose information about the job.
• ‘timespec’: Specify the time and date when the job should be executed.
Flags of at Command
The following are the some of the flags of at command:
Flag Description
Syntax:
batch
It is important to note that batch does not accepts any parameters.
Other Similar Commands:
• atq: Used to display the queue of pending jobs(this is because at and
batch both uses the same job queue).
• atrm: Used to remove the specified job from job queue.
Example:
• Working with the batch command.
top
• Executing some commands using batch.
o See the average load is lower than 1.5 that’s why the job
queue is empty and command executed instantly.
o Use ctrl +d when done giving commands to batch.
cron command in Linux with Examples
The cron is a software utility, offered by a Linux-like operating system that
automates the scheduled task at a predetermined time. It is a daemon
process, which runs as a background process and performs the specified
operations at the predefined time when a certain event or condition is
triggered without the intervention of a user. Dealing with a repeated task
frequently is an intimidating task for the system administrator and thus he can
schedule such processes to run automatically in the background at regular
intervals of time by creating a list of those commands using cron. It enables the
users to execute the scheduled task on a regular basis unobtrusively like doing
the backup every day at midnight, scheduling updates on a weekly basis,
synchronizing the files at some regular interval. Cron checks for the scheduled
job recurrently and when the scheduled time fields match the current time
fields, the scheduled commands are executed. It is started automatically from
/etc/init.d on entering multi-user run levels.
Basic cron Commands in Linux
1. View/Edit the Cron Jobs
o Open the cron table (crontab) to edit:
crontab -e
o List existing cron jobs:
crontab -l
o Remove all scheduled jobs:
crontab -r
2. Cron Job Syntax The format for a cron job entry:
* * * * * command_to_run
│││││
│ │ │ │ └── Day of the week (0 - 7) [Sunday = 0 or 7]
│ │ │ └──── Month (1 - 12)
│ │ └────── Day of the month (1 - 31)
│ └──────── Hour (0 - 23)
└────────── Minute (0 - 59)
The rules which govern the format of date and time field as
follows:
• When any of the first five fields are set to an asterisk(*), it stands for all
the values of the field. For instance, to execute a command daily, we can
put an asterisk(*) in the week’s field.
• One can also use a range of numbers, separated with a hyphen(-) in the
time and date field to include more than one contiguous value but not all
the values of the field. For example, we can use the 7-10 to run a
command from July to October.
• The comma (, ) operator is used to include a list of numbers which may or
may not be consecutive. For example, “1, 3, 5” in the weeks’ field
signifies execution of a command every Monday, Wednesday, and
Friday.
• A slash character(/) is included to skip given number of values. For
instance, “*/4” in the hour’s field specifies ‘every 4 hours’ which is
equivalent to 0, 4, 8, 12, 16, 20.
3. Examples
o Run a script every day at 2 AM:
0 2 * * * /path/to/script.sh
o Run a backup script every Sunday at 1 AM:
0 1 * * 0 /path/to/backup.sh
o Run a command every 5 minutes:
*/5 * * * * /path/to/command
4. Managing cron Jobs for Other Users (Root Privileges Needed)
o List cron jobs for a specific user:
crontab -l -u username
o Edit cron jobs for another user:
crontab -e -u username