history_job control_alias
history_job control_alias
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.
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.
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
CTRL+Z keyboard shortcut. The bg command is then used to resume the job in the background.
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
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.
This command displays the last 5 commands from the command history.
Execute a Command by Event Number
Execute a command using its event number with the! Symbol. For instance:
!1997
This will rerun the command with event number 1997.
This will display the command associated with event number 1997 without
executing it.
This command filters and displays only the commands containing the term
“chpasswd.”
This ensures that the command won’t be stored in the history file.
This command removes the command with event number 1996 from history.
Be cautious, as this action irreversibly removes all commands from the history
View the Last 10 Commands History
Use history | tail to view the last 10 commands from the history:
history | tail
This command efficiently displays the most recent commands in the terminal.
How to Create and Use Alias Command in
Linux
Last Updated : 03 Jul, 2024
alias
name='value' Creates a new alias with the specified name and command
alias -t Temporarily creates an alias that lasts only for the current shell session
Option Description
For example
If we want to create an alias for the ‘cd Desktop’ command
(which is to move into Desktop directory using cd command),
you can use:
alias CD="cd Desktop"
Here we have give shortname= CD and our longer command =
cd Desktop
This alias allows you to type ‘CD’ instead of ‘cd Desktop’ for the
same result.
List All Aliases in Linux
To list all aliases in Linux, you can use the alias command without
any arguments. This will display all the currently defined aliases
in your shell session. Here’s how it works:
1. Open Terminal: Launch your terminal.
2. Run the Command: Type alias and press Enter.
alias
This will output a list of all aliases, showing their names and the
commands they represent. For example:
alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
Each line shows an alias name followed by the command it
represents, making it easy to see and manage your shortcuts.
How to Remove an Alias?
The removing an existing alias is known as unaliasing. It
following is the syntax of it.
Syntax
unalias [alias name]
This accurately reflects the process of removing an alias using
the ‘unalias’ command in Linux.
With this alias, typing ‘update’ in the terminal will execute both
update and upgrade commands in sequence.