0% found this document useful (0 votes)
4 views13 pages

Unit-4 Process in Linux

The document provides an overview of processes in Linux, detailing how they are created, tracked, and managed. It explains the difference between foreground and background processes, how to use commands like ps, kill, and at for process management, and introduces the cron utility for scheduling recurring tasks. Additionally, it covers types of processes such as parent, child, zombie, orphan, and daemon processes.

Uploaded by

Anugrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Unit-4 Process in Linux

The document provides an overview of processes in Linux, detailing how they are created, tracked, and managed. It explains the difference between foreground and background processes, how to use commands like ps, kill, and at for process management, and introduces the cron utility for scheduling recurring tasks. Additionally, it covers types of processes such as parent, child, zombie, orphan, and daemon processes.

Uploaded by

Anugrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIT-4 (PART-2)

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.

Method 2: Background Process: It runs in the background without keyboard


input and waits till keyboard input is required. Thus, other processes can be done
in parallel with the process running in the background since they do not have to
wait for the previous process to be completed.
Adding & along with the command starts it as a background process
$ pwd &
Since pwd does not want any input from the keyboard, it goes to the stop state until
moved to the foreground and given any data input. Thus, on pressing Enter:

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.

Tracking ongoing processes


ps (Process status) can be used to see/list all the running processes.
$ ps

PID TTY TIME CMD


19 pts/1 00:00:00 sh
24 pts/1 00:00:00 ps
For more information -f (full) can be used along with ps
$ ps –f

UID PID PPID C STIME TTY TIME CMD


52471 19 1 0 07:20 pts/1 00:00:00f sh
52471 25 19 0 08:04 pts/1 00:00:00 ps -f

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)

For single-process information, ps along with process id is used


$ ps 19

PID TTY TIME CMD


19 pts/1 00:00:00 sh
For a running program (named process) Pidof finds the process id’s (pids)
Fields described by ps are described as:
• UID: User ID that this process belongs to (the person running it)
• PID: Process ID
• PPID: Parent process ID (the ID of the process that started it)
• C: CPU utilization of process
• STIME: Process start time
• TTY: Terminal type associated with the process
• TIME: CPU time is taken by the process
• CMD: The command that started this process

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

UID PID PPID C STIME TTY TIME CMD


52471 19 1 0 07:20 pts/1 00:00:00 sh
52471 25 19 0 08:04 pts/1 00:00:00 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.

at Command in Linux with Examples


In the world of Linux operating systems, there exists a powerful tool known as the
“at command.” The ‘at’ command provides users with the ability to schedule tasks
to be executed at a later time, offering a convenient way to automate processes
without manual intervention. Whether you need to run a script, execute a command,
or perform a system task at a specific time, the ‘at’ command provides the flexibility
to accomplish these tasks efficiently.
Here, we will look into the details of the ‘at’ command in Linux, exploring its
syntax, options, and practical examples.

What is the ‘at’ Command?

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:

• Delayed Execution: Running a command or script after a certain delay


without needing repeated execution.
• System Maintenance: Scheduling maintenance tasks like backups or
updates during off-peak hours.
• Reminders: Sending reminders or notifications at a future time.
• Resource Management: Running resource-intensive tasks at a time
when system usage is low.

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

-m Send mail to the user when the job is done

-f file Read job from a specified file instead of standard input

-l Lists all pending jobs (same as atq)

-d Deletes a job (same as atrm)

-v Shows the time the job is scheduled to run


Flag Description

-q queue Assigns the job to a specific queue

-c job Displays the commands in the specified job

Examples of Using the ‘at’ Command


The following are the some of the examples used for ‘at’ command:
1. Listing Scheduled Jobs
To list all scheduled jobs in the AT queue, you can use the `-l` option:
at -l
This command will display a list of all pending jobs along with their job
numbers.

2. Deleting a Scheduled Job


If you need to remove a scheduled job from the queue, you can do so by
specifying its job number with the ‘-d' option:
at -d 1
This command will delete the job with job number 1 from the ‘at’ queue.

3. Scheduling a Job for the Coming Monday


To schedule a job for the coming Monday at a time twenty minutes later than the
current time, the syntax would be:
at Monday +20 minutes
• 'at': The command to schedule a one-time task.
• 'Monday': Specifies the next Monday as the starting point.
• '+20 minutes': Adds 20 minutes to the specified starting point.
This command instructs the system to execute the specified job on the upcoming
Monday, twenty minutes after the current time.

4. Scheduling a Job for a Specific Date and Time


To schedule a job to run at 1:45 on August 12, 2020, the command is:
at 1:45 081220
• '1:45': The time at which the command will be executed.
• '081220': The date in MMDDYY format, which stands for
August 12,
2020
Here, the command specifies the exact date and time when the job should be
executed, in the format of hour:minute monthdate.
5. Scheduling a Job for a Future Time
If you need to schedule a job to run at 3 PM four days from the current time, the
command would be:
at 3pm + 4 days
• 'at 3pm': Specifies the time of day when the task should run, which is 3
PM.
• '+ 4 days': Adds four days to the current date to determine the execution
date.
This command schedules the job to be executed at 3 PM, four days from the current
time.

6. Scheduling a System Shutdown


To schedule a job to shutdown the system at 4:30 PM today, you can use the
following command:
echo "shutdown -h now" | at 4:30
• 'echo "shutdown -h now"': This part creates a command that shuts down
the system immediately.
• '|': The pipe operator (|) passes the output of the first command (echo
"shutdown -h now") as input to the next command.
• 'at -m 4:30': This schedules the provided command to run at 4:30 (AM or
PM).
This command pipes the shutdown command to the ‘at’ command,
specifying that the system should be shut down at 4:30 PM.

batch command in Linux with Examples


batch command is used to read commands from standard input or a specified file
and execute them when system load levels permit i.e. when the load average
drops below 1.5.

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

time command in Linux with examples

‘time’ command in Linux is used to execute a command and prints a summary


of real-time, user CPU time and system CPU time spent by executing a
command when it terminates. ‘real‘ time is the time elapsed wall clock time
taken by a command to get executed, while ‘user‘ and ‘sys‘ time are the
number of CPU seconds that command uses in user and kernel mode
respectively.
Understanding Time Command Basics
The ‘time’ command measures the execution time of a specified command or
program and reports various metrics, including real, user, and system time.
Here’s a breakdown of these metrics:
• Real Time: The actual elapsed time, from start to finish, including time
spent waiting for I/O and other processes.
• User Time: The amount of CPU time spent executing user-mode
instructions within the process.
• System Time: The amount of CPU time spent executing system-level
instructions on behalf of the process.
Syntax:
The syntax for using the time command is:
time [options] command [arguments]
Examples of time Command
1. Measure Execution Time of a Command
time ls -l
Output:
real 0m0.002s
user 0m0.001s
sys 0m0.001s
Here:
• real is the total elapsed time.
• user is the CPU time spent in user mode.
• sys is the CPU time spent in kernel mode.

2. Measure Execution Time of a Script


time ./my_script.sh
This will show how long the script takes to execute.

3. Measure Execution Time of a Complex Command


time find / -name "testfile"
This measures the time taken to search for a file in the entire system.
4. Using time with Piping
When using pipes (|), use /usr/bin/time instead of time because the built-in time
command in shells like Bash doesn’t work with pipes.
/usr/bin/time ls -l | wc -l

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy