Passing a variable to kill command

I have a script that kicks off several processes in the background and stored their pids in a variable as follows:

PID_DUMP_TRAN=$PID_DUMP_TRAN" "$!

so I then have a list of pids
If I echo $PID_DUMP_TRAN I get back a list of pids e.g. 8210 8211 8212

However I then want to kill all these processes at the end of the script. I have tried:
kill $PID_DUMP_TRAN
kill `echo $PID_DUMP_TRAN `
and kill `echo "$PID_DUMP_TRAN"`

I know this is me mis-understanding how to use the variable rather than the kill command and its a fairly basic misunderstanding at that but can anybody point me in the right direction,
I just keep getting an error saying the Arguments must be jobs or PIDS!!!

Could you please post your full script please?

This code isn't quoted correctly:

PID_DUMP_TRAN=$PID_DUMP_TRAN" "$!
PID_DUMP_TRAN="$PID_DUMP_TRAN $!"
1 Like

xargs is nice command when you need to give some output to the next command arguments

some_command_output | xargs kill

Example kill apache processes

ps -ef | grep apache | grep -v grep | awk '{print $2}' | xargs kill
1 Like

Whenever you have cut | grep | awk | sed | grep -v | kitchen | sink, you should replace that with awk. It can do it all at once, it's not a glorified cut.

ps -ef | awk '/[a]pache/ { print $2 }' | xargs kill

Hi all,
thanks for taking the time to reply. Still can't get this to work so I have included the script and results below.
"## some other stuff goes on here" represents a load of stuff that I have commented out while I get the kill bit working.I have included it to just explain that the script needs to do more than just kick off some jobs and kill them....it needs to kill them when all else is done.

# set the Field Separator to pipe
IFS='|'
set -o xtrace

unset PIDS
unset PID_DUMP_TRAN
CONFIG=/export/home/user/release.config
#DBLIST=/export/home/user/dbdump.config
ISQL_INFILE1=/export/home/user/dump_tran1.sql
ISQL_INFILE2=/export/home/user/dump_tran2.sql
ISQL_INFILE3=/export/home/user/dump_tran3.sql


$SYBASE/$SYBASE_OCS/bin/isql -Uuser -Ppassword  -Sserver1 -i$ISQL_INFILE1 >tran_dump1.log &
PID_DUMP_TRAN=$!
$SYBASE/$SYBASE_OCS/bin/isql -Uuser -Ppassword  -Sserver1-i$ISQL_INFILE2 >tran_dump2.log &
PID_DUMP_TRAN="$PID_DUMP_TRAN $!"
$SYBASE/$SYBASE_OCS/bin/isql -Uuser -Ppassword  -Sserver1 -i$ISQL_INFILE3 >tran_dump3.log &
PID_DUMP_TRAN="$PID_DUMP_TRAN $!"


## some other stuff goes on here

kill `echo "$PID_DUMP_TRAN"`
~

OUTPUT:
+ unset PIDS
+ unset PID_DUMP_TRAN
+ CONFIG=/export/home/user/release.config
+ ISQL_INFILE1=/export/home/user/dump_tran1.sql
+ ISQL_INFILE2=/export/home/user/dump_tran2.sql
+ ISQL_INFILE3=/export/home/user/dump_tran3.sql
[4] 22896
+ PID_DUMP_TRAN=22896
+ /export/home/sybase/ASE//bin/isql -Uuser -Ppassword -Sserver1 -i/export/home/user/dump_tran1.sql
[5] 22897
+ PID_DUMP_TRAN=22896 22897
+ 1> tran_dump1.log
+ /export/home/sybase/ASE//bin/isql -Uuser -Ppassword -Sserver2 -i/export/home/user/dump_tran2.sql
+ 1> tran_dump2.log
[6] 22898
+ PID_DUMP_TRAN=22896 22897 22898
+ /export/home/sybase/ASE//bin/isql -Uuser -Ppassword -Sserver3 -i/export/home/user/dump_tran3.sql
+ 1> tran_dump3.log
+ echo 22896 22897 22898
+ kill 22896 22897 22898
ksh: 22896 22897 22898: Arguments must be %job or process ids

Interestingly enough if I just put:
kill %1 %2 %3 in the end of the script this works but I need to be sure I am killing the right processes!!!

Any further help would be greatly appreciated.

---------- Post updated at 08:40 AM ---------- Previous update was at 08:13 AM ----------

Coffee kicked in and I re-read this reply.
Script working swimmingly using

echo $PID_DUMP_TRAN | xargs kill -9

Thanks a million!