Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 ) 
like image 915
Remi.b Avatar asked Feb 17 '18 03:02

Remi.b


People also ask

Which of following character used to send a process to run in the background?

Starting Processes You can start a background process by appending an ampersand character ( & ) to the end of your commands.

Which command will take a foreground process and suspend it and then place it in the background as a stopped job?

Press 'CTRL+Z' which will suspend the current foreground job. Execute bg to make that command to execute in background.

How do I set a background job in Linux?

To bring a process running in the background to the foreground, use the fg command followed by the job id. To put in the background again, press CTRL + Z followed by the bg command.


1 Answers

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.

like image 136
RavinderSingh13 Avatar answered Nov 01 '22 13:11

RavinderSingh13