0

I wrote a shell script to resume all suspended processes. After a research on the commands, this is what I have by now:

#!/bin/bash
for i in `jobs -sp`; do
  kill -CONT $i
done

But nothing seems to happend... I created a dummy shell script that loops forever, runned and stopped it. And after runned my shell script it won't resume. Why is this happening?

S.H.
  • 183
  • 9

2 Answers2

0

You need to use set -m to use job control in a shell script, See Why can't I use job control in a bash script?

Here is my solution:

set -m

while read i
do
  kill -CONT $i
done < <(jobs -sp)

I am using process substitution instead of backticks.

Community
  • 1
  • 1
cdarke
  • 42,728
  • 8
  • 80
  • 84
0

Running a script starts a subshell which obviously does not have any stopped jobs because you just started it.

Try putting your code in a function instead, and run it in the shell where you are looking to continue those jobs.

cont () {
    local i
    for i in $(jobs -sp); do
      kill -CONT $i
    done
}

(Notice the preference for $(...) over `...` -- it doesn't make a lot of difference here, but you should learn to prefer the modern syntax for when it does matter.)

The code is so simple that it might not make sense to put it in a function, though. You can simplify further with xargs:

jobs -sp | xargs kill -CONT
tripleee
  • 175,061
  • 34
  • 275
  • 318