Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OS Lab Manual (4th Expt) - Muddappa

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Expt-4

4.a) Linux commands related to process creation and management- system calls fork()
and exec(); bg, fg, nohup, pkill, nice, top, ps.

Process :
A process is any active (running) instance of a program. In other words, process is a program under
execution.
 A new process can be created by the fork() system call.
 The new process consists of a copy of the address space of the original process. fork()
 creates new process from existing process.
 Existing process is called the parent process and the process is created newly is called
child process.

 Each process is given a unique process identification number (PID).


 PID is usually five-digit number.
o This number is used to manage each process.
o user can also use the process name to manage process.

Starting a Process
A process executes in two ways they are,
 Foreground Processes
 Background Processes

Foreground Processes
 Every process by default runs in Foreground (which means the output is printed on the screen.) The
best example for the foreground process is the ls command which prints the output on the screen by
listing the files and directories.
 When a program is running in the foreground, user cannot start another process without completing the
previous process. [ It is because of this reason foreground process is considered a time-consuming
process.]
Background Processes
 When a process starts running and it is not visible on the screen it is called a background process.
 User can simultaneously run ‘n’ number of commands in the background process.
 To enable the background process, provide ampersand symbol (&) at the end of the command.
Example : ls &

bg: Puts a process in the background, allowing it to continue execution while the user interacts with the terminal.

15 | P a g e
when you run sleep 10 &, you are starting a process that sleeps for 10 seconds in the background, and you can
continue to use the terminal for other commands without waiting for the sleep command to complete.

After you execute sleep 10 &, you will see output similar to [2] 69. Here's what this output means: the background
job you started is the second background job, and its process ID is 69.

fg: Brings a background process to the foreground, making it the active process.

When you run fg %1, the job with job number 1 is moved to the foreground, and the shell waits for it to complete.
You can then interact with the process directly in the terminal.

Remember that you can use the jobs command to see a list of background jobs and their job numbers before
using fg.

(Note: If you want to bring a specific process to the foreground by using its PID, you can use the % symbol
followed by the PID as a job specification with fg.)

nohup: short for no hang up is a command in Linux systems that keep processes running even after
exiting the shell or terminal.

 nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP)signal.
 This is a signal that is sent to a process upon closing or exiting the terminal.
 Every command in Linux starts a process at the time of its execution, which automatically
gets terminated upon exiting the terminal.
 Suppose, user executing programs over SSH and if the connection drops, the session will be terminated,
all the executed processes will stop, and user may face a huge accidental crisis.

 In such cases, running commands in the background can be very helpful to the user and this is where
nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs
the process even after logging out from the shell/terminal.
 Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for
terminating the process after closing/exiting the terminal.
 nohup command prevents the process from receiving this signal upon closing or exiting the
terminal/shell.

16 | P a g e
 Once a job is started or executed using the nohup command, stdin will not be available to the user and
nohup.out file is used as the default file for stdout and stderr.
 If the output of the nohup command is redirected to some other file, nohup.out file is not generated.
Syntax: nohup command [command-argument ...]
 nohup command can be used to run multiple commands in the background.

Syntax: nohup bash -c 'commands'

Example 1:
nohup bash -c 'cal && ls'

in the above example bash -c 'cal && ls': This part of the command invokes the Bash shell (bash) and passes it a
series of commands to execute. The -c option tells Bash to read commands from the following string. In this case,
the string 'cal && ls' contains two commands separated by &&. cal is a command to display a calendar for the
current month, and ls is a command to list files and directories in the current directory.

Here, the output will be by default stored in nohup.out. To redirect it, type:
Syntax: nohup bash -c 'commands' > filename.txt
Example 2:
nohup bash -c 'cal && ls' > output.txt

Using nohup is particularly useful when you want to run a process that takes a long time to complete, and you
don't want it to be terminated when you log out or close your terminal session.

pkill:
 Sends signals to processes based on their name or other attributes.

pkill: The command used to send signals to processes based on certain criteria.

My_process: The pattern or criteria used to match against process names. In this case, it targets
processes whose names contain the string "my_process."

(Note:in kill command PID number will be mentioned)


17 | P a g e
nice:
 Alters the priority of a process, influencing its share of CPU time.

 The nice command in Unix-like operating systems is used to adjust the scheduling priority of a process.
The priority, also known as the "niceness," ranges from -20 (highest priority) to 19 (lowest priority). By
default, processes have a niceness of 0.

Syntax: nice –n ‘Nice value’ ‘process name’


If the process already running in the system we can renice its value using sysntax renice ‘nice value’ –p ‘PID’

To know the nice value run ‘top’ command.

top:
 Displays a dynamic view of system processes, updating in real-time.

 PID : Process ID of each task.


 USER : user name of the task owner.
 PR : The process's priority.(highest is 20,lowest is -20)
 NI : Nice value of the task.
 VIRT : Total virtual memory used (in kb)by the task.
 RES : Secondary (Physical) memory used.
 SHR : Shared memory used (in kb) by the task.
 S : Status of the job(totally 5 status :S-sleeping, R-unning, T-Traced to stoped,
D- Uninturruptible sleep,Z-Zombie)
 USER : User name of owner of task.
 %CPU : Represents the CPU usage.
 TIME+ : CPU Time, the same as 'TIME', but reflecting more granularity through

18 | P a g e
hundredths of a second.

 COMMAND: Command which is used.

Ps(Process Status):
The default output of ps is a simple list of the processes running in current terminal.

In this output:
 PID: Process ID
 TTY: Terminal associated with the process
 TIME: CPU time consumed by the process
 CMD: Command being executed by the process

Provides a snapshot of the current processes or a detailed list of processes (Like Task Manager In
Windows.).

We can use these syntax for PS command: ps ux or ps PID

We can get the PID of a process using the command ‘pidof processname’

4.b) cron and at commands to schedule tasks.


cron
 It is a set of commands that are used for running regular scheduling tasks.
 Crontab stands for “cron table”.
 It allows the use of a job scheduler, which is known as cron to execute tasks.
 Crontab of Linux has six fields.
 The first five fields define the time and date of execution, and the 6’th field is used for command execution.

19 | P a g e
Crontab syntax:

To schedule a task using cron, you need to edit a special file called the crontab file in a text editor and add your
task in it in a particular format. Then cron will run the task for you at the time you specify in the crontab file.
You can specify any intervals of time, from seconds to weeks and even years

Permitting users to run cron jobs:


 The user must be listed in this file to be able to run cron jobs if the file exists.
o /etc/cron.allow
 If the cron.allow file doesn’t exist but the cron.deny file exists, then a user must not be listed in
this file to be able to run the cron job.
o /etc/cron.deny
 Note: If neither of these files exists then only the superuser(system administrator) will be allowed to
use a given command.
Simple Example:

File Name: task.sh path: /home/admincs/(say for example)


Contents:
echo Welcome to Task Scheduler Demo echo
Try date Command
date
echo Try time Command time
echo List all file/folders in a home directory ls
echo End of Task Scheduler Demo
 Change the execution permission to task.sh
chmod 764 task.sh
 Add the task to /etc/crontab (Task will be executed at 4.13pm, User can Change timings
according to the requirements)
13 16 * * * root sh /home/admincs/task.sh > /home/admincs/output.txt
 Save and Exit. And Wait till 4.13pm
 After that Check the output.txt by the command
cat /home/admincs/output.txt
 Output of the task.sh is present in output.txt.
 Alternatively /var/log/syslog can be verified about the execution of the Task.

20 | P a g e
Example 1:
 Run /home/folder/gfg-code.sh every hour, from 9:00 AM to 6:00 PM, everyday.
o 00 09-18 * * * /home/folder/gfg-code.sh
 Run /usr/local/bin/backup at 11:30 PM, every weekday.
o 30 23 * * Mon, Tue, Wed, Thu, Fri /usr/local/bin/backup
 Run sample-command.sh at 07:30, 09:30, 13:30 and 15:30.
o 30 07, 09, 13, 15 * * * sample-command.sh

Example 2: Other Simple Example to Schedule user or System Tasks:


 Note: To schedule user task, the shell script path must be specified in place of
/path/to/script
 Specify the interval at which user want to schedule the tasks using the cron job entries we specified
earlier on.
 To reboot a system daily at 12:30 pm, use the syntax:
o 30 12 * * * /sbin/reboot
 To schedule the reboot at 4:00 am use the syntax:
o 4 * * * /sbin/reboot
 Note: The asterisk * is used to match all records

at :
 at command is a command-line utility that is used to schedule a command to be
executed at a particular time in the future.
 Jobs created with at command are executed only once.
 The at command can be used to execute any program or mail at any time in the future.
 It executes commands at a particular time and accepts times of the form HH:MM to run a job
at a specific time of day.
 In the command, expression like noon, midnight, teatime, tomorrow, next week, next
Monday, etc. could be used with at command to schedule a job.

 Syntax: at [OPTION...] runtime


Working with at command
 Command to list the user’s pending jobs:
o at -l
OR
o atq

 Schedule a job for the coming Monday at a time twenty minutes later than the current
time:
o at Monday +20 minutes
 Schedule a job to run at 1:45 Aug 12 2020:
o at 1:45 081220

21 | P a g e
 Schedule a job to run at 3pm four days from now:
o at 3pm + 4 days
 Schedule a job to shutdown the system at 4:30 today:
o echo "shutdown -h now" | at -m 4:30
 Schedule a job to run five hour from now:
o at now +5 hours

22 | P a g e

You might also like