Probably every nix-oid knows that by running a command with an ampersand at the end - it goes into the background, continuing to work.
Thus, the running command turns into a
job .
More advanced ones know that you can display a list of running jobs with the jobs command, and switch between them with the fg commands (bring the background task to the shell) / bg (send a stopped task to the background). The rest of the teams are used less often, and most beginners of Linux users read about them briefly, but forgot, or never knew. And by the way except:
jobs ,
fg and
bg is
disown ,
wait and even
kill .
Apart from those that can be used inside a job or to control the commands written above: enable, builtin.
So if you're wondering how to do it:
1. Suspending a job. Stop (kill).
2. Run a previously suspended job.
3. Advanced task numbering.
4. Waiting for completion of background tasks.
5. Team disown.
Run three tasks:
$ nice gzip < /dev/zero > /dev/null &
[1] 15727
$ bzip2 < /dev/zero > /dev/null &
[2] 15728
$ xz < /dev/zero > /dev/null &
[3] 15730
Three tasks, with numbers 1, 2, 3. and their PIDs.
Stop tasks
')
Suspense
Suspend the task, for example, the first:
this is done by the kill command, passing a SIGSTOP signal.
kill -SIGSTOP% 1

In this case, the internal bash command was used: kill, and not the external program / bin / kill.
Yes, / bin / kill can also be used to send a SIGSTOP signal, but the external program cannot handle the job number, give it a PID.
Linux is still quite convenient: 4-5 digit PID (2 bytes), but on AIX, pids are mostly 8-digit PIDs, but they can also have 9 digits, besides not consecutive, but scattered across their 4-byte range.

A SIGSTOP signal is one of three signals that an application cannot ignore (SIGSTOP, SIGCONT, SIGKILL which is usually 9). Yes, of course, each of them has a numeric code (for example, kill -9 means kill-SIGKILL), but they do not coincide on all platforms. It is possible to find out their correspondence either from the documentation (man signal / man 7 signal / man kill), or directly by the (internal) command
kill -l .
Completion of the task
Similarly:
kill -SIGKILL% 1 or process PID.
Starting previously suspended
bg% 1 - sending a suspended job to be performed in the background.
fg% 1 - switching to a stopped (or running task)
Another option: switch to this task and stop it Ctrl + Z:
$ fg% 1
nice gzip </ dev / zero> / dev / null
^ Z
[1] + Stopped nice gzip </ dev / zero> / dev / null
Advanced task numbering
Here I continue to compress the zeros of the three processes:
[1] 15727 Running nice gzip </ dev / zero> / dev / null &
[2] - 15728 Running bzip2 </ dev / zero> / dev / null &
[3] + 15730 Running xz </ dev / zero> / dev / null &
The first field in square brackets is the job number. It can be accessed by commands such as fg, bg, kill (internal), disown, wait.
The second field: has
- (minus) for the 2nd task and
+ (plus) for the 3rd - these are the numbers of tasks in the notation: “Last” and “Current”.
The current one is “we work with now”. If we have one task running, then only it will have +. If we switch to another task with the fg command, it will become “current”. The fg and bg commands without job numbers will work with the Current. It can also be accessed through% + or %%
The last is the one that was before the “Current”. If we complete the current one (kill -9 %%), then the Last will become Current. Such a stack of tasks. It can be accessed through% -
Also for the commands fg, bg, kill, disown, wait, the tasks can be accessed via the name of the command being run:
run another sleep
kill -9 #x
Kills only one task that starts at x (my backup will suffer with the xz command)
kill -9 #? gzip
will kill the command that contains gzip. (I also have only one such process), and if the mask is such that it includes several tasks (for example, the previous command kill -9 #? Zip, then this will not work, because nice gzip and bzip2 fall under this mask, and it is impossible to determine exactly which task the command belongs in. Although we can see: for fg, you can still understand that one argument is needed, and kill can take several arguments, bg cannot but could, but in general all commands cannot understand ambiguous masks)
See the
ABS Guide for details.
enable and builtin
This is where you should tell about the enable and builtin commands:
From the link above:
The
builtin BUILTIN_COMMAND construct runs the internal "BUILTIN_COMMAND" command, temporarily banning the use of functions and external system commands with the same name.
enable either prohibits or allows the call of internal commands. For example, enable -n kill disallows the use of the internal kill command, as a result, when the interpreter encounters the kill command, it will call the external kill command, i.e. / bin / kill.
Internal commands are called faster than external, full-fledged programs, if only because there is no need to fork the child process, to switch between them. The list of internal commands can be viewed
enable -a .
Waiting for completion of background tasks
The wait command - suspends the script until all background tasks are completed or until the task / process with the specified task number / process PID is completed. Returns the exit code of the specified job / process.
Often used to synchronize processes: run one or more tasks in the background (in parallel), wait for them to complete, and continue the next phase.
cat url.list | while read url; do wget -qc "$ url" & done
wait
mv ./* / media / flash /
It will start downloading
at the same time the list of all url, and
only when all downloads are
completed will it be transferred to a USB flash drive.
Command disown
disown - Removes a task from the table of active tasks of the shell. Those. the process continues to run in the background, but is no longer a task.
It is useful for processes started by the nohup command - it intercepts the SIGHUP signal, and also takes the output streams to its nohup.out file.
There are also commands: suspend, times and logout, and times.
For example, times% N - displays the accumulated time in userspace and system for the job and the current shell. Suspend suspends the current shell, as could be done by pressing ctrl + Z for another command, but provided that the current shell does not work in the login shell mode. But I can not even imagine how they can be applied.