1

Simply kill $( jobs -p ) not work in dash.
For example in dash:

$ sleep 999 &
$ kill $(jobs -p)
dash: 2: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
$
Paulo Tomé
  • 3,782
  • 1
    Are you really using dash as an interactive shell, or did you happen upon this when trying to debug some script? Notice that that kill $(jobs -p) will not kill all the background jobs, but only the job/program group leaders: even in bash, if you run (sleep 3600; echo DONE) &, and then kill $(jobs -p), the sleep process will not be killed. –  Dec 25 '19 at 23:35
  • 1
    Anyways, you can run jobs -p > tmpfile; kill $(cat tmpfile) -- and that's the very best Q you can get while not questioning any of the dubious assumptions ;-) –  Dec 25 '19 at 23:44
  • Note that the hyperlinked problem is specific to the Debian Almquist shell. It is not exhibited by the FreeBSD Almquist shell. – JdeBP Dec 26 '19 at 01:03
  • @mosvy Yes, I'm using dash as an interactive shell(i.e. both the "interactive" and the "monitor" options are enabled(i.e. on)). – illiterate Dec 26 '19 at 18:22
  • 1
    Then you'll have to make do with copy&paste, a temporary file or a named pipe. Just in case it wasn't clear from my 1st comment, the linked stackoverflow answer is incorrect, as kill $(jobs -p) will not kill all the background processes or jobs. –  Dec 26 '19 at 23:31
  • 1
    If you want to kill all the processes from the current session, use pkill -s0; but if your interractive shell you call that from is not the session leader, that may also kill processes not started from it. –  Dec 26 '19 at 23:35
  • @mosvy how can I write to named pipe without change the jobs -p result? jobs -p > pipe; kill $(cat pipe) just hanging, even I try jobs -p & I just get empty when read the pipe(cat pipe). – illiterate Dec 27 '19 at 14:38
  • 1
    Do it backwards, run the kill asynchronously. (kill -TERM $(sed s/^/-/ fifo)&); jobs -p > fifo –  Dec 28 '19 at 00:03

1 Answers1

-1

Make a bug report to the maintainers of dash.

It seems that dash does return empty output in case that jobs is run in a subshell.

Do you really need to use dash? It is know for not being POSIX compliant for several reasons.

schily
  • 19,173