Kenny McCormack
2024-10-03 23:08:52 UTC
Note: This is a "How do things really work - in the real world?", rather
than a "What does the manual say about how things work?" sort of thread.
The manual says the answer is "The job most recently started in the
background or most recently stopped." This is not always the case.
Observe (this is bash version 5.2.15) (and "j" is aliased to "jobs -l"):
$ j
[1]+ 20914 Stopped (signal) sudo bash
$ sleep 100 & j
[2] 12914
[1]+ 20914 Stopped (signal) sudo bash
[2]- 12914 Running sleep 100 &
$ fg
sudo bash
# suspend
[1]+ Stopped sudo bash
Status: 147
$ %2
Note that I start with one background job (the "sudo"). I launch a second
one, but, according to the "jobs" listing, job #1 is still the "current"
job (denoted with the "+"). Further, when I do "fg", I get back to job #1.
Two comments:
1) You generally would like it to work the way it should work, since
you generally want to manipulate the most recent job (the sleep in the
above example, not the sudo). Getting the job id from the pid ($!) is
possible and is my chosen workaround, but it is not trivial.
2) I could not find mention of what exactly the output of the jobs
command means; i.e., what the + and - mean - in "man bash".
Also note: I googled this question and found something on unix.stackexchange.
There is a post there by our own "Stephan Chaz...", that basically just
quotes the manual. As I said, this info is incorrect (as seen above).
than a "What does the manual say about how things work?" sort of thread.
The manual says the answer is "The job most recently started in the
background or most recently stopped." This is not always the case.
Observe (this is bash version 5.2.15) (and "j" is aliased to "jobs -l"):
$ j
[1]+ 20914 Stopped (signal) sudo bash
$ sleep 100 & j
[2] 12914
[1]+ 20914 Stopped (signal) sudo bash
[2]- 12914 Running sleep 100 &
$ fg
sudo bash
# suspend
[1]+ Stopped sudo bash
Status: 147
$ %2
Note that I start with one background job (the "sudo"). I launch a second
one, but, according to the "jobs" listing, job #1 is still the "current"
job (denoted with the "+"). Further, when I do "fg", I get back to job #1.
Two comments:
1) You generally would like it to work the way it should work, since
you generally want to manipulate the most recent job (the sleep in the
above example, not the sudo). Getting the job id from the pid ($!) is
possible and is my chosen workaround, but it is not trivial.
2) I could not find mention of what exactly the output of the jobs
command means; i.e., what the + and - mean - in "man bash".
Also note: I googled this question and found something on unix.stackexchange.
There is a post there by our own "Stephan Chaz...", that basically just
quotes the manual. As I said, this info is incorrect (as seen above).
--
That's the Trump playbook. Every action by Trump or his supporters can
be categorized as one (or more) of:
outrageous, incompetent, or mentally ill.
That's the Trump playbook. Every action by Trump or his supporters can
be categorized as one (or more) of:
outrageous, incompetent, or mentally ill.