- Linux jobs и команды по управлению ими
- Linux / Unix: jobs Command Examples
- Purpose
- Syntax
- Starting few jobs for demonstration purpose
- jobs command examples
- How do I show process IDs in addition to the normal information?
- How do I list only processes that have changed status since the last notification?
- Display lists process IDs (PIDs) only
- How do I display only running jobs?
- How do I display only jobs that have stopped?
- jobs command options
- A note about /usr/bin/jobs and shell builtin
- Related media
- Linux jobs command
- Syntax
- Options
- Examples
- Related commands
- Job Control in Linux
- What are jobs ?
- Unique identifiers for a job
- Managing jobs in Linux
- List the running jobs
- Bring a job to the foreground
- Suspend the job
- Send a job to the background
- List only running jobs
- List only suspended jobs
- List process IDs of the jobs
- Terminate a job
- Understanding the job control commands in Linux – bg, fg and CTRL+Z
- Whats a job in Linux
- Job Control Commands
- Running a Job in the Background
- Managing the background jobs
Linux jobs и команды по управлению ими
CTRL+Z работает с большинством процессов — переводит их в подвешенное состояние — останавливает и отправляет на задний план. Все остановленные команды можно посмотреть введя в терминале jobs
Запустим второй процесс
Сейчас выполняются оба находясь при этом в фоне:
[1]- Stopped ping ya.ru
[2]+ Stopped top
Теперь можно переместить одно из заданий на передний план указав его номер
ping ya.ru
64 bytes from www.yandex.ru (93.158.134.3): icmp_seq=4 ttl=57 time=30.3 ms
64 bytes from www.yandex.ru (93.158.134.3): icmp_seq=5 ttl=57 time=29.0 ms
64 bytes from www.yandex.ru (93.158.134.3): icmp_seq=6 ttl=57 time=29.0 ms
64 bytes from www.yandex.ru (93.158.134.3): icmp_seq=7 ttl=57 time=29.6 ms
При переводе на передний план вывод производится в основной терминал, более ничего не меняется и задание выполняется в любом случае
Можно таким же образом вывести команду на задний план
Вывод команды таким образом продолжит быть присоединенным к терминалу, результат tail -f снова выведется в консоль как только в лог будет записываться информация (сэмулировать ситуацию можно перезапустив какой-либо демон подключившись к системе через другой терминал).
При выполнении в bg таким образом для нескольких команд их вывод в терминал будет смешиваться случайным образом, что может быть не очень удобно.
Удалить процесс можно указывая его номер со знаком процента
Работа с Linux jobs является типичной операцией поручному управлению системными процессами. Читайте подробнее про ps aux.
Источник
Linux / Unix: jobs Command Examples
I am new Linux and Unix user. How do I show the active jobs on Linux or Unix-like systems using BASH/KSH/TCSH or POSIX based shell? How can I display status of jobs in the current session on Unix/Linux?
Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.
jobs command details | |
---|---|
Description | Show the active jobs in shell |
Category | Processes Management |
Difficulty | Easy |
Root privileges | No |
Est. reading time | 3 minutes |
Table of contents
|
Purpose
Displays status of jobs in the current shell session.
Syntax
The basic syntax is as follows:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
jobs [options] jobID
Starting few jobs for demonstration purpose
Before you start using jobs command, you need to start couple of jobs on your system. Type the following commands to start jobs:
Finally, run ping command in foreground:
To suspend ping command job hit the Ctrl-Z key sequence.
jobs command examples
To display the status of jobs in the current shell, enter:
$ jobs
Sample outputs:
To display the process ID or jobs for the job whose name begins with “p,” enter:
$ jobs -p %p
OR
$ jobs %p
Sample outputs:
The character % introduces a job specification. In this example, you are using the string whose name begins with suspended command such as %ping .
How do I show process IDs in addition to the normal information?
Pass the -l (lowercase L) option to jobs command for more information about each job listed, run:
$ jobs -l
Sample outputs:
Fig.01: Displaying the status of jobs in the shell
How do I list only processes that have changed status since the last notification?
First, start a new job as follows:
$ sleep 100 &
Now, only show jobs that have stopped or exited since last notified, type:
$ jobs -n
Sample outputs:
Display lists process IDs (PIDs) only
Pass the -p option to jobs command to display PIDs only:
$ jobs -p
Sample outputs:
How do I display only running jobs?
Pass the -r option to jobs command to display only running jobs only, type:
$ jobs -r
Sample outputs:
How do I display only jobs that have stopped?
Pass the -s option to jobs command to display only stopped jobs only, type:
$ jobs -s
Sample outputs:
To resume the ping cyberciti.biz job by entering the following bg command:
$ bg %4
jobs command options
Option | Description |
---|---|
-l | Show process id’s in addition to the normal information. |
-p | Show process id’s only. |
-n | Show only processes that have changed status since the last notification are printed. |
-r | Restrict output to running jobs only. |
-s | Restrict output to stopped jobs only. |
-x | COMMAND is run after all job specifications that appear in ARGS have been replaced with the process ID of that job’s process group leader./td> |
A note about /usr/bin/jobs and shell builtin
Type the following type command/command command to find out whether jobs is part of shell, external command or both:
$ type -a jobs
$ command -V jobs
Sample outputs:
In almost all cases you need to use the jobs command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/jobs command can not be used in the current shell. The /usr/bin/jobs command operates in a different environment and does not share the parent bash/ksh’s shells understanding of jobs.
Related media
This tutorials is also available in a quick video format:
You learned about jobs command on Linux and Unix-like systems. See also:
- bash(1) Linux/Unix command man page
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux jobs command
On Unix-like operating systems, the jobs shell command lists the status of all running jobs.
This page covers the bash built-in version of jobs.
Syntax
Options
JOBSPEC | Job name or number. |
-l | Lists process IDs in addition to the normal information. |
-n | List only processes that have changed status since the last notification. |
-p | Lists process IDs only. |
-r | Restrict output to running jobs. |
-s | Restrict output to stopped jobs. |
Examples
Displays all running jobs. Output is similar to the following:
Here, the job number 1 was suspended by the user, and the process in this case is the top utility.
When supplied the -l option, jobs displays process IDs in addition to job number, similar to the following output:
Related commands
at — Schedule a command to run at a certain time.
csh — The C shell command interpreter.
kill — Send a signal to a process, affecting its behavior or killing it.
ksh — The Korn shell command interpreter.
ps — Report the status of a process or processes.
sh — The Bourne shell command interpreter.
Источник
Job Control in Linux
This is a short tutorial on job control in Linux and Unix.
What are jobs ?
In Linux or Unix, a job is defined as a task or command that has started running but not yet finished what it is doing.
As Linux and Unix are multitasking operating systems, they allow you to run multiple commands simultaneously.
Each running command is called a job, and is assigned a unique id called the job number.
It is very easy to manage jobs in Linux.
The main commands you use for job control in Linux are as follows.
- jobs — List all the jobs that are running or suspended.
- fg — Bring the job to the foreground.
- bg — Send the job to the background.
- stop or Ctrl + z — Suspend the job.
- kill or Ctrl + c — Terminate the job.
Unique identifiers for a job
You can point out a particular job using special identifiers. They are as follows.
%n — Job whose job number / job ID is the number n.
%word — Job whose command line starts with string word.
%?word — Job whose command line contains the string word.
%+ — Current job. You can also use %% .
Managing jobs in Linux
How to manage jobs in Linux can be easily understood via an example.
Lets simultaneously run a few commands that takes some time to complete.
Here we have executed 4 sleep commands and all are started in the background as denoted by & .
List the running jobs
And the output is .
In the output above, the number within [ and ] is the job number (job ID). The job number is unique for each job.
Bring a job to the foreground
To bring job number 2 to the foreground, you run the following command.
Suspend the job
To suspend a job, you first bring the job to the foreground and then press the keys Ctrl + z .
Alternately, you can also suspend a job running in the background as follows.
Send a job to the background
To resume the suspended job 2 in the background, you can run the following command.
List only running jobs
List only suspended jobs
List process IDs of the jobs
This command will list process IDs of the jobs in addition to the normal information.
Terminate a job
To terminate a job, you first bring the running job to the foreground, and then press the keys Ctrl + c .
You can also terminate a job without bringing it in the foreground just by passing the job number to kill .
For example, to kill the 3rd job, you can do as follows.
Источник
Understanding the job control commands in Linux – bg, fg and CTRL+Z
Whats a job in Linux
A job is a process that the shell manages. Each job is assigned a sequential job ID. Because a job is a process, each job has an associated PID. There are three types of job statuses:
1. Foreground: When you enter a command in a terminal window, the command occupies that terminal window until it completes. This is a foreground job.
2. Background: When you enter an ampersand (&) symbol at the end of a command line, the command runs without occupying the terminal window. The shell prompt is displayed immediately after you press Return. This is an example of a background job.
3. Stopped: If you press Control + Z for a foreground job, or enter the stop command for a background job, the job stops. This job is called a stopped job.
Job Control Commands
Job control commands enable you to place jobs in the foreground or background, and to start or stop jobs. The table describes the job control commands.
Option | Description |
---|---|
jobs | Lists all jobs |
bg %n | Places the current or specified job in the background, where n is the job ID |
fg %n | Brings the current or specified job into the foreground, where n is the job ID |
Control-Z | Stops the foreground job and places it in the background as a stopped job |
Running a Job in the Background
To run a job in the background, you need to enter the command that you want to run, followed by an ampersand (&) symbol at the end of the command line. For example, run the sleep command in the background.
The shell returns the job ID, in brackets, that it assigns to the command and the associated PID. With the job ID, you can use the job control commands to manage the job whereas the kernel uses PIDs to manage jobs.
When a background job is complete and you press Return, the shell displays a message indicating the job is done.
Managing the background jobs
You can use the jobs command to list the jobs that are currently running or suspended in the background.
You can use the fg command to bring a background job to the foreground.
You can use the ‘Control+Z keys and bg command to return a job to the background. The Control+Z keys suspend the job, and place it in the background as a stopped job. The bg command runs the job in the background. For example:
1. Using CTRL+Z
Источник