bash - start - how to cancel a job in unix




What is the meaning of the `+`, `-` and ` ` signs that precedes `Done` when a background process ends? (2)

So here is my understanding of it:

1- Job flagged or having a + is the one that was sent to the background last.

2- Job flagged or having a - was sent to the background second last.

3- Other background jobs are not flagged.

Here is an example I just ran on my system:

$bash: /singh/test1 &
[1] 9223
$bash:  /singh/test2 &
[2] 9226
$bash:  /singh/test3 &
[3] 9234
$bash:  /singh/test4 &
[4] 9237
$bash:  jobs
[1]   Running                 /singh/test &
[2]   Running                 /singh/test2 &
[3]-  Running                 /singh/test3 &
[4]+  Running                 /singh/test4 &

I could see from man bash:

There are a number of ways to refer to a job in the shell. The character % introduces a job specification (jobspec). Job number n may be referred to as %n. A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, %ce refers to a stopped ce job. If a prefix matches more than one job, bash reports an error. Using %?ce, on the other hand, refers to any job containing the string ce in its command line. If the substring matches more than one job, bash reports an error. The symbols %% and %+ refer to the shell’s notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using %-. If there is only a single job, %+ and %- can both be used to refer to that job. In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a +, and the previous job with a -. A single % (with no accompanying job specification) also refers to the current job.

Question

What are the meaning of the +, - and signs that precedes Done when a background process ends?

Examples of interest

Code 1

(sleep 4s; echo first)&
(sleep 2s; echo second)&
(sleep 1s; echo third)&

[1]   Done                    ( sleep 4s; echo first )
[2]-  Done                    ( sleep 2s; echo second )
[3]+  Done                    ( sleep 1s; echo third )

Code 2

(echo first)&
(echo second)&
(echo third)&

[1]+  Done                    ( echo first )
[2]+  Done                    ( echo second )
[3]+  Done                    ( echo third )

%+ : current job; last job stopped in foreground or started in background
%- : last/previous job
%% : same as +

%+ not only displays for the change of status in a backgrounded job, but also for a foreground job, e.g., that has been suspended with kill -STOP <pid>.

$ sleep 10 
[1]+  Stopped    sleep 10

# by running kill -STOP 28105  on another terminal

See Bash Job Control Basics for more detail.

Job number n may be referred to as ‘%n’. The symbols ‘%%’ and ‘%+’ refer to the shell’s notion of the current job, which is the last job stopped while it was in the foreground or started in the background. A single ‘%’ (with no accompanying job specification) also refers to the current job. The previous job may be referenced using ‘%-’. If there is only a single job, ‘%+’ and ‘%-’ can both be used to refer to that job. In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a ‘+’, and the previous job with a ‘-’.

With multiple jobs running in the background, bash shows you the job, when its status has been changed and when it hits another prompt. To see the job immediately when job changes its status/terminated, one can use set builtin.

$ set -b
$ sleep 10 &
[1] 27866
$ sleep 10 &
[2] 27868
$  [1]-  Done                    sleep 10

[2]+  Done                    sleep 10




parallel-processing