34

While reading documentation [1], the term "jobspec" appears a few times.

What is a jobspec?

[1] https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html

american-ninja-warrior
  • 7,397
  • 11
  • 46
  • 80
  • 2
    A concept that has nothing to do with programming [and for which questions thus belong on SuperUser], since it's part of functionality that isn't enabled in noninteractive use. :) – Charles Duffy Jan 27 '16 at 00:19
  • @CharlesDuffy Whether this belongs on superuser is an open question. The job control builtins _are_ available to a script (e.g. you can do `wait %1`). An example usage: a script that will video transcode several thousand files. The script runs on an 8 core machine. The script uses job control to manage 8 worker processes, starting a new one when an old one completes. I usually do this sort of thing in perl, but it could be done in bash. – Craig Estey Jan 27 '16 at 01:33
  • @CraigEstey, you can only do `wait %1` if `[[ $- = *m* ]]` ("monitor mode"), which is false by default in noninteractive shells (and incompatible with some other useful functionality, such as `shopt -s lastpipe`). Otherwise, you need to wait by PID to collect exit status, which is the usual way for scripts such as the one you suggest above to do so (`start_encoder & encoder_pids+=( $! ); ...; for pid in "${encoder_pids[@]}"; do wait "$pid" || echo "Encoder failed"; done` or such). – Charles Duffy Jan 27 '16 at 01:38
  • 1
    @CharlesDuffy I just did a `wait %1` in a script with a `$-` value of `hB` and it worked. In the transcoding example, you'd be doing a loop, watching for changes in the `jobs` output, rather than waiting [as you want to start a new job when _any_ of the pending ones complete, not when all 8 are done]. There may be more usual ways to do it, but it is a valid way. – Craig Estey Jan 27 '16 at 01:57
  • I stand corrected on the monitor-mode requirement. – Charles Duffy Jan 27 '16 at 02:05
  • @CraigEstey, ...but how are you blocking until something exits? If you're just polling in your loop, the conventional approach is `kill -0` targeted at PIDs... but really, I tend to call that kind of situation a job for `xargs -P 8`, or anything else that can wait for a SIGCHLD rather than needing to poll. – Charles Duffy Jan 27 '16 at 02:07
  • 1
    @CharlesDuffy sleep in loop. My actual script outputs progress window for jobs with elapsed + ETA (based on input file position from /proc/pid/fd/...), so it needs to wake periodically (i.e. poll). Also, it monitors a directory for new "job requests", cancellations, higher priority jobs. Easy in perl/python, harder in bash, but will work. So loop would do: check for new jobs, check `jobs` for status change, output progress, retire completed file/job, start worker with next file, sleep. It might have to suspend a lower prior job and start a higher prior one. Also, may run on diff machine, etc – Craig Estey Jan 27 '16 at 06:56

1 Answers1

35

The job control section of Greg's Bash Guide describes this as follows:

A job specification or "jobspec" is a way of referring to the processes that make up a job. A jobspec may be:

  • %n to refer to job number n.
  • %str to refer to a job which was started by a command beginning with str. It is an error if there is more than one such job.
  • %?str to refer to a job which was started by a command containing str. It is an error if there is more than one such job.
  • %% or %+ to refer to the current job: the one most recently started in the background, or suspended from the foreground. fg and bg will operate on this job if no jobspec is given.
  • %- for the previous job (the job that was %% before the current one).
Community
  • 1
  • 1
Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
  • When you didn't write something yourself, it should be attributed -- not just with a link, but with prose making it clear which content is and is not an original part of your answer. – Charles Duffy Jan 27 '16 at 00:23
  • @Charles Duffy Got it. I'll edit accordingly. Thank you. – Rany Albeg Wein Jan 27 '16 at 00:24
  • I'm not sure I would call the Bash Guide "the documentation" though sometimes I think it should be. =) – Etan Reisner Jan 27 '16 at 02:39
  • @EtanReisner, heh, my fault -- I didn't look closely at where the link was going and assumed it was a different piece of the manual. :) – Charles Duffy Jan 27 '16 at 04:16
  • In bash, `%` itself refers to the current job too. By the way, the bash manual is sufficently good but is not somewhat comprehensive, plus little examples. – rosshjb Apr 07 '21 at 09:39
  • Can someone add an example how to set a "jobspec" and find a process by its "jobspec"? – mgutt Aug 28 '22 at 20:35