0

Using heredoc to execute multi-line commands on a remote machine. Trying to get pid of a server to kill that server located in the remote machine

#!/bin/bash

HOST_IP="10.180.5.23"
read -p "For HOST RESTART press 1" num
if [ "$num" == "1" ]
then
ssh -t -t $HOST_IP << 'EOSSH'
line=$(pgrep -f host_server1)
echo $line
arr=($line)
sudo kill -9 "${arr[1]}"
EOSSH
fi

Error : kill: (15015) - Operation not permitted

1
  • 1
    Assigning the value to an array and then fetching the array's first element is a completely useless diversion. Simply kill "$line". You should probably not be using kill -9, either. Commented Jul 26, 2016 at 6:50

1 Answer 1

1

There heredoc works fine but sudo is expecting you to type in a password and there is no terminal connected to the ssh executing the commands on the server. You have two options, neither is entirely secure:

  1. Allow the user on the server to sudo without a password
  2. Put the lines from the heredoc into a script on the server and make it setui (sticky) so that it executes as root.

These apply generally to running scripts as root. Your other option that is specific to this case is to log in as the user running the server process so that you do not get operation not permitted from the kill.

Sign up to request clarification or add additional context in comments.

1 Comment

I logged in as ssh username@HOST_IP and it worked fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.