Passing a variable to kill command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a variable to kill command
# 1  
Old 09-17-2012
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:
Code:
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!!!

Last edited by Corona688; 09-17-2012 at 12:27 PM..
# 2  
Old 09-17-2012
Could you please post your full script please?
# 3  
Old 09-17-2012
This code isn't quoted correctly:

Code:
PID_DUMP_TRAN=$PID_DUMP_TRAN" "$!

Code:
PID_DUMP_TRAN="$PID_DUMP_TRAN $!"

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-17-2012
xargs is nice command when you need to give some output to the next command arguments
Code:
some_command_output | xargs kill

Example kill apache processes
Code:
ps -ef | grep apache | grep -v grep | awk '{print $2}' | xargs kill

This User Gave Thanks to kshji For This Post:
# 5  
Old 09-17-2012
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.

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

# 6  
Old 09-18-2012
Still no joy and don't see that I can use awk

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.
Code:
# 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 ----------

Quote:
Originally Posted by kshji
xargs is nice command when you need to give some output to the next command arguments
Code:
some_command_output | xargs kill

Example kill apache processes
Code:
ps -ef | grep apache | grep -v grep | awk '{print $2}' | xargs kill

Coffee kicked in and I re-read this reply.
Script working swimmingly using
Code:
echo $PID_DUMP_TRAN | xargs kill -9

Thanks a million!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing Variable in -mtime command

Hi, As the process of log cleanup, Im using the below command find $DIR -mtime +3 -type f -exec gzip {} \; The problem is I want to pass +3 as variable in my unix shell. I have defined ZPDATE=+3 in my properties file and calling this property file in my script. If i try the... (6 Replies)
Discussion started by: Deena1984
6 Replies

2. Shell Programming and Scripting

Passing Shell variable from file to another command

Hi all, I have a file looks like AAAA 111 BBBB 222 CCCC 333 need to pass variable value like var1=AAAA and var2=111 to another command for three times with next values. stuck over here cat file | while read line do export var1=`awk '{print $1}'` echo $var1 export var2=`cat file... (3 Replies)
Discussion started by: rakeshtomar82
3 Replies

3. Shell Programming and Scripting

Passing perl variable to shell command

Can we pass perl variable to shell commands. If yes, please give some example. (2 Replies)
Discussion started by: Anjan1
2 Replies

4. Shell Programming and Scripting

passing variable to sed command not working

Hello All, I am trying to embed variable in sed command to fetch a portion of record between two pattern. This command is not working ...any suggestion on this how to place the variable in sed command to find a portion . I am using Sun OS (Solaris). Thanks JM (1 Reply)
Discussion started by: jambesh
1 Replies

5. UNIX for Dummies Questions & Answers

Passing a command in a variable

I need to set up a strange system through which an arbitrary command is sent to a number of different servers (well, actually, VPS accounts). We have a command "vpass" that "passes" a command from the root level to resident VPS accounts. Suppose I wanted each VPS to do some trivial thing, like... (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

Passing a variable to sed command

Hi guys, I wanted to pass a variable to the sed command which tells which line to be deleted. a=2; echo $a; sed '$ad' c.out it is throwing an error. sed: 0602-403 "$a"d is not a recognized function. I even tried "$a" and \$a.. but it is of no use. Can you please correct me... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

passing variable values to awk command

Hi, I have a situation where I have to specify a different value to an awk command, I beleive i have the gist of this done, however I am not able to get this correct. Here is what I have so far echo $id 065859555 This value occurs in a "pipe" delimited file in postition 8. Hence I would... (1 Reply)
Discussion started by: jerardfjay
1 Replies

8. Shell Programming and Scripting

Passing a variable to sudo sed command

hi, dataParse(){ line="$@" name="cat /etc/passwd | grep "$line": | cut -f6 -d':'" eval $name > sam.txt 2>&1 sudo -u $line sed -n 's/data-1/&/p' $name/test.xml >> sam1.txt } Here i getting the homedir of the accounts and is set in name variable.which returns "/home/raju" which i... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

9. Shell Programming and Scripting

Passing a variable in SED getting command garbled

I fairly new to SED. I have tried many different variations of this line of code and even breaking it down into its components and running them separately. They work individually without variables but when I place the $todbname variable it will either inserts the text "connect to $todbname"... (3 Replies)
Discussion started by: edewerth
3 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies
Login or Register to Ask a Question