Mac Shell script to kill all background jobs

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
Edinburgh, UK
Hello, I've come up with a command which will list all the background processes running in my current shell and am trying to run it from a shell script but am experiencing some difficulty. The script looks as follows:
Code:
#! /bin/sh                                                                      
jobs -l | grep -o '[0-9]* ' | xargs -E eofstr kill -9
If I run this line directly in my terminal it works fine but if I try to execute from the script it doesn't seem to do anything.

Any ideas?

Thanks.
 

willieva

macrumors 6502
Mar 12, 2010
274
0
The jobs command shows the processes that have been started in the current shell. When you run it in a script it is in a new shell and not aware of the jobs in the parent shell.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
im not sure what you mean by 'create an alias'. And how could I use this to solve the problem. Thanks for responding!
As willieva mentioned, your script will run in a new shell. By creating an alias you can substitute the line you have with one identifier. Since this is just an alias, it will run in your current shell.

Code:
$ alias greetings='echo "hello"'
$ greetings
hello
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.