0

I'm trying to kill a process, then verify if it's killed successfully. However I found that although it's killed, in the same script it still shows it's not killed. My script is like the following (running in a remote server by ssh first, that's why you see the remote parameter \$pid):

pid=`ps -ef | grep 'someprog' | grep sh | tr -s ' ' | cut -d ' ' -f2` 
if [ ! -z "\$pid" ]; then
    echo kill pid: \$pid
    kill -9 \$pid
    sleep 10
fi

pid1=`ps -ef | grep 'someprog' | grep sh | tr -s ' ' | cut -d ' ' -f2`
if [ ! -z "\$pid1" ]; then
    echo \$pid1 is not killed yet
fi

Although I have "sleep 10", and I checked in another terminal this pid is gone, The test of pid1 is still true, and print the line "$pid1 is not killed yet". Can anyone help me so that I won't get into printing that line when it's already killed?

Matt Zhang
  • 11
  • 3
  • All your `\`\`` command expansions happen on the client side – that other guy Jul 19 '16 at 19:31
  • Yes I'm ssh-ing to another machine and issue the scripts here. Any problem with this practice? – Matt Zhang Jul 19 '16 at 20:23
  • Yes. You have to make sure expansions happen on the machine you want. – that other guy Jul 19 '16 at 20:29
  • I have another terminal ssh to the same remote server, and I can verify there, that the scripts are executed, and the pid is killed successfully. – Matt Zhang Jul 19 '16 at 20:33
  • The code I posted above are within ssh Here document. – Matt Zhang Jul 19 '16 at 20:37
  • All your `\`\`` expansions happen on the client side. You have to make sure they happen on the machine you want. Most likely you will want to add ``\ `` in front of each backtick. This is the same reasoning that made you put it in front of your `$` – that other guy Jul 19 '16 at 20:41
  • Do you mean in my script replace all \$pid with \\$pid ? But it reports syntax error for me. Actually I think it got expanded in the remote server, as I printed out the pid and it is the same as the pid I'm looking for in the remote server. If the expansion is in my client side, there's no such pid and shouldn't print anything. – Matt Zhang Jul 19 '16 at 20:49
  • I'm not sure if you're seeing the markup properly or if you are misreading the description. Backticks are the apostrophes that tilt backwards. For example, you write this: ``pid=`ps -ef ....``. Right after the equals sign, there is a backtick character. You must escape the backtick characters. – that other guy Jul 19 '16 at 20:55
  • Aha, I misunderstood you earlier. Now it works. Thank you so much, saved my day! – Matt Zhang Jul 19 '16 at 21:05
  • Since you don't appear to be mixing local and remote execution in this example (it's hard to tell since you didn't include the vital ssh or heredoc parts), you can just remove the escaping and instead quote the heredoc word, e.g. `<< 'EOF'` instead of `<< EOF` – that other guy Jul 19 '16 at 21:07

0 Answers0