0% found this document useful (0 votes)
16 views8 pages

Unit-2 Part 2

Uploaded by

komal
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)
16 views8 pages

Unit-2 Part 2

Uploaded by

komal
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/ 8

Jobs and Job Control in Linux

Introduction

In the Linux operating system, jobs refer to processes that are running in the background or foreground. Job control
refers to the ability to manipulate these processes, including suspending, resuming, and terminating them. This can
be useful for managing multiple tasks or for debugging problems with a process.

Job control is made possible by the shell, which is a command-line interface that allows users to interact with the
operating system. The most common shell in Linux is the Bourne Again Shell (BASH), but other shells such as the Z
Shell (ZSH) and the Korn Shell (KSH) are also available.

In this article, we will explore the basics of job control in Linux and how to use it to manage processes.

Understanding Processes and Jobs in Linux

In Linux, every program that is running is considered a process. A process can be a standalone program or a part of a
larger program.

Each process is assigned a unique identifier called a process ID (PID). The PID can be used to refer to the process and
perform actions on it, such as suspending or terminating it.

A job is a process that is either running in the foreground or the background. The foreground is the active window in
the terminal, and the background is any process that is running but not actively being used in the terminal.

By default, when you run a command in the terminal, it runs in the foreground. You can tell that a process is running
in the foreground because it displays output and you cannot enter any more commands until it finishes.

To run a process in the background, you can use the & symbol at the end of the command.

Example

$ sleep 30 &

[1] 12345

In this example, the sleep command causes the process to sleep for 30 seconds. The & symbol causes the process to
run in the background, and the output [1] 12345 indicates that it is job number 1 with a PID of 12345.

Managing Jobs with the fg and bg Commands

The fg (foreground) and bg (background) commands allow you to move jobs between the foreground and the
background.

To bring a background job to the foreground, you can use the fg command followed by the job number or the PID.

Example

$ fg %1

sleep 30

This brings the sleep command, which is job number 1, to the foreground and displays its output.

To send a foreground job to the background, you can use the bg command followed by the job number or the PID.

Example
$ sleep 30

[1] 12345

^Z

[1]+ Stopped sleep 30

$ bg %1

[1]+ sleep 30 &

In this example, the sleep command is run in the foreground and then suspended with the ^Z keyboard shortcut.
The bg command is then used to resume the job in the background.

Suspend or Resume Jobs in Linux

The suspend command allows you to temporarily stop a job, while the kill command allows you to permanently
terminate a job.

To suspend a job, you can use the suspend command followed by the job number or the PID.

Example

$ sleep 30 &

[1] 12345

$ suspend %1

[1]+ Suspended

This suspends the sleep command, which is job number 1. The job can then be resumed with the fg command or left
suspended in the background.

To terminate a job, you can use the kill command followed by the job number or the PID.

Example

$ sleep 30 &

[1] 12345

$ kill %1

This terminates the sleep command, which is job number 1.

View and Manage Jobs with the jobs and ps Commands

The jobs command allows you to view a list of all jobs running in the background or suspended in the current shell.
The ps command allows you to view a list of all processes running on the system.

Example

$ sleep 30 &

[1] 12345

$ sleep 60 &
[2] 12346

$ jobs

[1] Running sleep 30 &

[2] Running sleep 60 &

You can use the jobs command to view the status of each job and its job number or PID.

The ps command allows you to view a list of all the processes that are currently running on the system. You can use
the -a flag to show all processes and the -x flag to show processes that are not associated with a terminal.

Example

$ ps -ax

PID TTY STAT TIME COMMAND

1 ? Ss 0:00 /sbin/init

2? S 0:00 [kthreadd]

3? S 0:00 [ksoftirqd/0]

4? S 0:00 [kworker/0:0]

...

The ps command displays the PID, terminal (TTY), status, time, and command for each process.

Aliases:

Linux users often need to use one command over and over again. Typing or copying the same command over and
over again reduces your productivity and distracts you from what you are supposed to be doing.

Aliases are like custom shortcuts that represent a command (or set of commands) that can be executed with or
without custom options. Chances are you are already using aliases on your Linux system without even knowing it.

List Currently Defined Aliases in Linux

You can see a list of defined aliases on your profile by simply executing the alias command.

$ alias

Here you can see the default aliases defined for your user in the Ubuntu system.

List Aliases in Linux


As you can see, executing the ll command is equivalent to running ls -alF command.

$ ll

$ ls -alF

Listing Files in Linux

You can create an alias with a single character that will be equivalent to a command of your choice.

How to Create Aliases in Linux

Creating aliases is a relatively easy and quick process. You can create two types of aliases –
temporary and permanent. We will review both types.

Creating Temporary Aliases in Linux

What you need to do is type the word alias then use the name you wish to use to execute a command followed
by "=" sign and quote the command you wish to alias.

The syntax is as follows:

$ alias shortName="your custom command here"

Here is an actual example:

$ alias wr=”cd /var/www/html”

You can then use "wr" shortcut to go to the webroot directory. The problem with that alias is that it will only be
available for your current terminal session.

If you open a new terminal session, the alias will no longer be available. If you wish to save your aliases across
sessions you will need a permanent alias.
Creating Permanent Aliases in Linux

To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:

 Bash – ~/.bashrc

 ZSH – ~/.zshrc

 Fish – ~/.config/fish/config.fish

The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the
fact that you will be saving it in a file this time. So for example, in bash, you can open a .bashrc file with your favorite
editor like this:

$ vim ~/.bashrc

Find a place in the file, where you want to keep the aliases. For example, you can add them at the end of the file. For
organization purposes, you can leave a comment before your aliases something like this:

#My custom aliases

alias home=”ssh -i ~/.ssh/mykep.pem [email protected]

alias ll="ls -alF"

Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in
the current session, issue the following command:

$ source ~/.bashrc

To remove an alias added via the command line can be unaliased using the unalias command.

$ unalias alias_name

$ unalias -a [remove all alias]

Variables:
In every programming language variables plays an important role , In Linux shell scripting we can use two types of
variables :

a). System Defined Variables


b). User Defined Variables.

A variable in a shell script is a means of referencing a numeric or character value. And unlike formal programming
languages, a shell script doesn’t require you to declare a type for your variables
System Defined Variables
These are the variables which are created and maintained by Operating System(Linux) itself. Generally these
variables are defined in CAPITAL LETTERS. We can see these variables by using the command “$ set“. Some of
the system defined variables are given below :

$ set

To print the value of above variables, use echo command as shown below :

We can use environment


variables in our bash scripts by
using the environment
variable’s name preceded by a
dollar sign. Example is shown
below,

Bash Script – Working of Bash Var...

Notice that the environment variables in the echo commands are replaced by their current values when the script is
run. Also notice that we were able to place the $USER system variable within the double quotation marks in the first
string, and the shell script was still able to figure out what we meant. There is a drawback to using this method,
however. Look at what happens in this example:

That is obviously not what was To display an actual dollar sign,


intended. Whenever the script you must precede it with
sees a dollar sign within quotes, a backslash character:
it assumes you’re referencing a
variable. In this example the
script attempted to display
the variable $1 (which was not
defined), and then the number 5.

That’s better. The backslash allowed the shell script to interpret the dollar sign as an actual dollar sign, and not a
variable.
User Defined Variables

These variables are defined by users. A shell script allows us to set and use our own variables within the script.
Setting variables allows you to temporarily store data and use it throughout the script, making the shell script more
like a real computer program.

User variables can be any text string of up to 20 letters, digits, or an underscore character. User variables are case
sensitive, so the variable Var1 is different from the variable var1. This little rule often gets novice script programmers
in trouble.

Values are assigned to user variables using an equal sign. No spaces can appear between the variable, the equal sign,
and the value (another trouble spot for novices). Here are a few examples of assigning values to user variables:

The shell script automatically determines the data type used for
the variable value. Variables defined within the shell script
maintain their values throughout the life of the shell script but are
deleted when the shell script completes.

Just like system variables, user variables can be referenced using the dollar sign:

Running the script produces the following output,

Each time the variable is referenced, it produces the value currently assigned to it. It’s important to remember that
when referencing a variable value you use the dollar sign, but when referencing the variable to assign a value to it,
you do not use the dollar sign. Here’s an example of what I mean:
When you use the value of the value1 variable in the assignment statement, you must still use the dollar sign. This
code produces the following output:

If you forget the dollar sign, and make the value2 assignment line look like:

Without the dollar sign the shell interprets the variable name as a normal text string, which is most likely not what
you wanted.

Use of Backtick symbol (`) in shell variables

The backtick allows you to assign the output of a shell command to a variable. While this doesn’t seem like much, it
is a major building block in script programming. You must surround the entire command line command with
backtick characters:

The shell runs the command within the backticks and assigns the output to the variable testing. Here’s an example of
creating a variable using the output from a normal shell command:

The variable testing receives the output from the date command, and it is used in the echo statement to display it.

Running the shell script Note : In bash you can also use
produces the following output: the alternative $(…) syntax in
place of backtick (`),which has
the advantage of being re-
entrant.

You might also like