Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue executing the next other commands after using "kill" in shell?

Tags:

linux

shell

kill

I want to merge two different script files into one script file which could do what the two different files do. And the script files is:

script file A:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}

script file B:

nohup ./temp_tool &

the merged script file:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
nohup ./temp_tool &

The whole merged script file would stop after executing kill command, and I have to modify it to be:

pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
out=`kill -9 ${pid}`
nohup ./temp_tool &

and it works well now, but I don't know why? Is there any difference?

like image 494
Mark Avatar asked Jan 28 '26 11:01

Mark


1 Answers

I would say $pid also contains the pid of your script. You can filter it out:

script_pid=$$
pid=$(ps -ef | grep temp_tool | grep -Ev "grep|$script_pid" | awk '{print $2}')

Though if you want the pids of the command temp_tool I would suggest this:

ps -C temp_tool -o pid

Instead of the ps -ef | grep ...

like image 148
ahilsend Avatar answered Jan 30 '26 02:01

ahilsend