Karl.Li
Karl.Li

Reputation: 167

Is it possible to execute shell with parameterized remote ip?

I have a job like this:

  1. parameterized ${GIT_URL} and ${REMOTE_IP}.
  2. clone code by git url and package my project as jar
  3. scp jar file to remote ip, and then start it as server.`
  4. I am using Publish Over SSH Plugin.

The problem is, I have to add every server to my job configuration.

So is it possible to execute shell with parameterized remote ip like this?

    #!/bin/sh
    scp ${APP_NAME}.jar root@${REMOTE_IP}:/root/${APP_NAME}.jar
    ssh root@${REMOTE_IP}
    cd /root
    ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}' | xargs kill
    nohup java -jar ${APP_NAME}.jar &

Upvotes: 0

Views: 99

Answers (2)

Karl.Li
Karl.Li

Reputation: 167

I solved this in another way.

#!/bin/sh
scp ${APP_NAME}.jar root@${REMOTE_IP}:/root/${APP_NAME}.jar
ssh root@${REMOTE_IP} "sh -s" -- < /opt/jenkins/my.sh ${REMOTE_IP} ${APP_NAME}

So my.sh is a local shell file which define how to start jar as server with parameterized ip

Upvotes: 1

ben5556
ben5556

Reputation: 3018

Yes. Use "$REMOTE_IP" to resolve it to the parameter value.

#!/bin/sh
scp ${APP_NAME}.jar root@"$REMOTE_IP":/root/${APP_NAME}.jar
ssh root@"$REMOTE_IP"
cd /root
ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}' | xargs kill
nohup java -jar ${APP_NAME}.jar &

Upvotes: 1

Related Questions