signals - No command executed after performing kill command in shell script -
here shell script:
#!/bin/bash pids=$(ps -e | grep $1 |grep -v grep| awk '{print $1}') kill -s sigint $pids echo "done sendings signal"
i passing name of process command line argument.
echo command not getting executed, although target processes receiving sigint signal , exited.
any suggestions?
update:
changed code to:
#!/bin/bash pids=$(ps -e |grep $1 | grep -v grep | awk '{print $1}'|grep -v $$) echo $pids kill -s sigint $pids echo "done sendings signal" echo "the current process $$"
now noticing strange thing:
script working not expected. executing following command in command line outside scriptps -e|grep process-name|grep -v grep|awk '{print $1}'|grep -v $$
gives pid of process-name when execute same command inside shell script, assign variable pids , echo pids shows 1 more pid in addition pid of process-name. therefore when kill command executes gives error process second pid doesn't exist. echo remaining sentences in terminal. clue ?
your script works. reason can see echo not being executed value of $1 , script file name combine script pid gathered, thereby making script suicide.
the pids line spawns process running ps, grep, grep -- won't find in pids processes running grep, parent process itself?
try:
#!/bin/bash pids=$(ps -e | grep $1 |grep -v grep | awk '{print $1}' | grep -v "^$$\$" ) kill -s sigint $pids echo "done sendings signal"
or run pipes 1 after other suitable safety greps.
edit: evident "$1" selection selecting much. i'd rewrite script this:
#!/bin/bash # gather output of "ps -e". gather pids of # process , of ps process , subshell. pss=$( ps -e ) # extract pids, excluding 1 pid , excluding process called "ps". # don't need expunge 'grep' since no grep running when getting pss. pids=$( echo "$pss" | grep -v "\<ps\>" | grep "$1" | awk '{print $1}' | grep -v "^$$\$" ) if [ -n "$pids" ]; kill -s sigint $pids else echo "no process found matching $1" fi echo "done sending signal."
Comments
Post a Comment