0

I have a bash script on a server that contains a command which is not on the default path.

If I use a terminal to ssh to the server and execute the script, it works fine (because the directory where the command is located is added to the path). However, if I try to execute the command via JSch's CommandExec, I got a "command not found" error.

Is there anyway to add the additional directory to the path when executing the script via JSch?

Note that I cannot modify the script on the server. It has to run as is.

myscript.sh

...
mycommand
...

JSch code:

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
channelExec.setCommand("/path/to/myscript.sh");
channelExec.connect();

Thank you

Ares
  • 1,411
  • 1
  • 19
  • 35

1 Answers1

1

I think the easiest option would be to change the script

myscript.sh

.
.
/full/path/to/mycommand
.
.

When you execute the script directly from the shell, your login causes PATH to take effect which is not the case when you're executing it from JSch.


A quick google gave me this [ article ] worth reading.

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • Unfortunately, as I noted in my questions, I'm not able to modify the script. – Ares Jul 31 '16 at 03:44
  • Can you do something like this in `JSch`? 1. load myscript.sh 2. in the buffer modify `command` to `/path/to/command`. 3. Then do `channelExec.setCommand(modified_buffer);` – sjsam Jul 31 '16 at 03:52