简体   繁体   中英

How can I change the working directory when I run a script with ant sshexec in Jenkins?

I have a ant target where I successfully use a sshexec-task to install a server using tomcat on OpenSuse 11.

My problem comes when I try to run a script that start the newly installed server. I need to do this as root, so I use a task as this:

<sshexec host="${tss.hostname}"
    username="${username}"
    password="${password}"
    trust="true"
    Command="/usr/local/bin/foo/bar/tomcat/bin/startup.sh"
    failonerror="false"/>

The problem with this is that I need to run the script from the folder where it is located, as it finds CATALINA_HOME and JAVA_HOME from that location. And the path "foo/bar" will change over time so I do not want to set it permanently. I just want to change my running path to the correct one.

As I failed to start the server like that, I wanted to investigate a bit and changed the script so I used "CommandResource="cmd.txt" instead of a direct command. I thought I could use this to set CATALINA_HOME before I started the server and go around the problem that way.

This is the outcome when using cmd.txt:

  [sshexec] Connecting to foo-bar:22
  [sshexec] cmd : 
  [sshexec] cmd : cd /usr/local/bin/foo/bar/tomcat/bin
  [sshexec] cmd : pwd
  [sshexec] /root
  [sshexec] cmd : echo $PATH
  [sshexec] /usr/java/jdk1.6.0_27/bin:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
  [sshexec] cmd : export CATALINA_HOME=$PATH:/usr/local/bin/tibco/tss/4.5.0/tomcat
  [sshexec] cmd : echo $PATH
  [sshexec] /usr/java/jdk1.6.0_27/bin:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin

This shows that neither the cd nor export command have any effect.

I suspect I need to change the rights for ssh. I have read that I may need to change in .ssh/config. But I do not know how or where...

So, to sum this long question up short:

What do I need to do to be able to run my startup.sh as root from the correct tomcat/bin folder with a ant script that is run in Jenkins?

I've found that "cd" works when done on the same line...

<sshexec host="${tss.hostname}"
username="${username}"
password="${password}"
trust="true"
Command="(cd /usr/local/bin/foo/bar/tomcat/bin; startup.sh)"
failonerror="false"/>

I haven't actually used this to run a shell script but I'll often have ant change directories and then run GIT commands.

Yeah I also faced the same problem. The cd command doesnot work from sshexec. Alternative is to put all the commands in some install.sh file and scp it to the remote machine. And then use sshexec commandresource to execute this file on the remote machine. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM