0

I can start a background process like this:

>user@host ~ $ sleep 2m &
>[1] 123456

The job number and pid [1] 123456 are displayed immediately after starting the background job.

After starting the job, I can check its status using jobs -l.

>user@host ~ $ jobs -l
>[1] 123456 Running sleep 2m &

I would like to see the full output of jobs -l when I start my background process like this:

>user@host ~ $ sleep 2m &
>[1] 123456 Running sleep 2m &

I would also be happy with just the command, like this:

>user@host ~ $ sleep 2m &
>[1] 123456 sleep 2m &

I have looked in man pages for some environment variable that controls what is displayed but haven't found it yet. Is this possible and if so, how can it be done?

moss
  • 1
  • 2
  • You say you want to see the full output of `jobs -l`. Does that include other background jobs that are already running, or just the one you just started? – Barmar Aug 09 '18 at 21:24
  • 1
    When is this useful? I would have thought the command was obvious because it's the line you just hit enter on – that other guy Aug 09 '18 at 21:27
  • @thatotherguy, I see your point in the case of entering commands one-by-one at the command prompt - in that case this would be kind of silly. This is for an alias or shared by a team. They didn't write the alias so they don't know for sure what process it has started, only what PID it has, which is not as meaningful. They could go look at the alias definition, or call jobs -l themselves, but it is nice to hand this info to them directly. – moss Aug 10 '18 at 21:10
  • @Barmar, not other jobs, just the one I started - good clarification. – moss Aug 10 '18 at 21:11
  • You could simply do `sleep 30 & jobs -l %%` though it would print the pid twice – that other guy Aug 10 '18 at 22:17

2 Answers2

0

You can define a function that prints the command and then executes it in the background.

mybg() {
    echo "Backgrounding: $@"
    eval "$@ &"
}

Then you can do:

mybg sleep 60
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • this is a good idea and could work, too bad it's not all on one line, but maybe it's good enough. I think it would yield: `>Backgrounding sleep 60` `>[1] 12345` – moss Aug 10 '18 at 21:13
0

You can suppress the initial [1] 123456 output by putting using a group command for starting the background execution and redirecting stderr of the group command. You can follow this by an immediate jobs -l % to print the output you want about the last command placed in the background.

{ sleep 2m &} 2>/dev/null; jobs -l %

If you care about the stderr of the job you're running, you can extend this like so:

{ sleep 2m 2>&3 &} 3>&2 2>/dev/null; jobs -l %

[Technique is from this answer]

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53