I use shell job control to kick longer running tasks in to the background so I can continue while they churn through their data.
I can see the jobs I've got running with
jobs, for example,
jobs - Running su - root -c "..." & (wd: /backup/rsnapshot) + Running sleep 60 &
and I can control any one of these using the
% jobspec syntax, such as
fg %su or
If I use
jobs -l I get the PIDs too:
jobs -l - 31736 Running su - root -c "..." & (wd: /backup/rsnapshot) + 2269 Running sleep 60 &
bash I've got
jobs -x, but this isn't POSIX. Is this the only way to translate these
% jobspec values into PIDs, or is there a sensible (better) alternative approach? What about for other shells?
jobs -x echo %3 2269
My target use case is for the
% jobspec to be expanded transparently on the command line into the corresponding PID, so that in a command such as this the
%2 would be seen by the command as
pidtree %2 # pidtree 31736
This can be handled as
jobs -x pidtree %2 but that's not as elegant or convenient.
I'd like at least one answer targeting
bash, but contributions for other shells with job control are welcome, particularly if you have a POSIX solution.