When I ssh into a server, I can see that the PATH variable is set as required with some custom folders added required.
When I try to connect to the same server using com.jcraft.jsch.JSch library in Java, the PATH is set to something else.
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This seems like a out of the box value and doesn't include the custom folders as required. None of the commands that I need to work when I connect through Java unless I give the full path to the executable. I need to connect to different servers and they might be set up with executables in different locations so this is not an option.
I was hoping someone could help me figure out why the ./bashrc file doesn't run when I connect using Java. Should I have to run it manually?
Thanks in advance.
When you connect using command-line SSH, your "default command" is whatever shell you have set up for your username on the remote machine (probably bash, which you can change with chsh), and then bash imports your .bashrc file.
When you connect using JSch, the type of Channel that you open will determine what will be done with your connection. From what I understand about how JSch works, chances are that JSch is not opening bash when it opens the SSH session.
As a general rule of thumb, try using ~/.ssh/environment to set up your PATH instead of .bashrc if the SSH daemon is configured to allow it. This will always be imported before SSH runs any commands, including the shell, so it's more reliable for setting up your PATH.
More info can be found in this Unix StackExchange post about .bashrc, PATH, and SSH, including some alternatives to my answer.
Related
I have some question when using jenkins.
the scenario:
I have installed a tomcat on machine A,and config all necessary environment including JDK, and they work well.
I wrote a shell script named restart.sh to kill the tomcat process,and restart.
I execute the script and it works well.When I check the tomcat process using ps -ef,and it shows the exact JDK path as I already config.The JDK path is /usr/local/java/JDK7
When I execute the restart.sh script via jenkins,problem comes. I can see the tomcat process using the wrong JDK path,/usr/bin/java,not the path as i config. I use SSH Publishers plugin to run the remote script.see as the image below:
enter image description here
I don't know how this problem happen,please help,Thanks all
This might be the problem of path variables, just to cross verify , find JAVA_HOME on your server 1. By logging onto server manually 2. From Exec command of your ssh section of your jenkins job , most probably you will find the answer.
i'm assuming you're jenkins is configured to use jdk which is defined at its root level.
if this didn't solve , we need to look into this problem from another angle.
I am currently working on trying to deploy my java web application onto Openshift. My web application imports Gurobi library which requires a Gurobi License Environment to run my Linear Programming model. As such i've been going around trying everything i can to get the license installed to the openshift linux server etc
I've managed to get a few things going.
I've SSH into my application and registered the HostName of the openshift linux platform to the license.
I've managed to change the .bash_profile file which sets up the custom environments. below are the input i have placed into the file.
export GUROBI_HOME="/var/lib/openshift/54d4e281e0b8cd3a1400015c/jbossews/gurobi600/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"
export GRB_LICENSE_FILE="/var/lib/openshift/54d4e281e0b8cd3a1400015c/jbossews/gurobi.lic"
The problem with the input above is that I believe for PATH & LD_LIBRARY_PATH, openshift automatically reinstates them everytime I access my app through the shell.
I think this has resulted in my application being unable to instanciate a GRBENV object which is a Gurobi Environment object which checks the gurobi.lic file and use the LD_LIBRARY_FILE.
Has anyone did something similar to this before? Really need help in this.
Brandon
The way openshift instantiates your app and the ssh shell seems to be different. The correct place to set the environment variables are one of the start action_hooks. For instance, if you edit the .openshift/action_hooks/start file and add:
export PATH="${PATH}:${GUROBI_HOME}/bin"
this will work for the instance that runs your APP, but not for the SSH Shell. I did the following test:
echo $PATH > $OPENSHIFT_DATA_DIR/PATH
to confirm that the path running the APP can be set by the start hook.
I´m not sure if this solves your question (because you mention SSH shell, and I still don´t know how to set the PATH for that).
I would like to run a jar file extracted from my java project to be run on a Linux server I connect through SSH Tunneling. There are few problems, first there is something wrong with the Display: I get the error
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:173)
at java.awt.Window.<init>(Window.java:437)
at java.awt.Frame.<init>(Frame.java:419)
at java.awt.Frame.<init>(Frame.java:384)
at javax.swing.JFrame.<init>(JFrame.java:174)
at random_v2.v2Frame.<init>(v2Frame.java:127)
at random_v2.Wrapper.main(Wrapper.java:25)
and second is that I am not sure if I have to install other applications as well. In my code, the java program needs to get run other applications like weka, do I have to install weka with the same directory name and specs that is in my mac?
I appreciate your help in advance.
Best wishes
Assuming you're tunneling into this unix box using PuTTY:
Make sure X11 forwarding is enabled in your PuTTY settings.
Connect via command line using this command:
ssh -X user#server
The -X option enables X11 forwarding.
When I run code like this in Mac os:
Runtime.getRuntime.exec("open testFile.pdf");
the Mac OS will run Acrobat to open the local PDF file.
How can I do it, when the file is on a remote machine?
\\remoteHost\share\testFile.pdf
I try to do that like this:
Runtime.getRuntime.exec("open \\\\remoteHost\\share\\testFile.pdf");
but I failed.
Thanks!
Runtime.getRuntime().exec(String) just passes the String to the OS for it to handle it. So, if you want to run the command
open \\remoteHost\share\testFile.pdf
You have to pass it to exec(). Remember to escape the \ (replace every \ by \\)
Runtime.getRuntime().exec("open \\\\remoteHost\\share\\testFile.pdf");
Of course, the user running the program should have permissions in the remote machine. If you need to set another user, use the open program command line parameters.
I don't think it's possible to directly specify the path of an SMB share while using the open command. If you want to use open, you would have to mount that share as a local directory. See http://osxdaily.com/2009/09/24/access-and-mount-an-smb-share-via-command-line/ for an explanation.
A solution like that would be pretty fragile though. If you want a more reliable solution, I'd suggest using JCIFS (http://jcifs.samba.org/) to download the file locally and then use
Desktop.getDesktop().open(pdfFile);
to open the downloaded file.
I'm trying to write a piece of code that uses a ProcessBuilder to transfer a file on a remote UNIX machine onto the local Windows machine. On a brief bit of research I've found that either RCP or FTP should be a suitable thing to use.
Having done some research on the RCP command, I found instructions for copying files from a UNIX to windows machine, but they don't seem to work. The command I was told to use was:
rcp -r unixhost.user:/example/directory C:\Directory
However using this told me that C: was not a host. I tried it with the IP address, localhost, the alias of the windows pc in the hosts file but none of these worked, it either said permission denied or it could not connect to the host. Having looked up ftp it seems that would be another viable option. I'm not sure if I can execute a command using ProcessBuilder to successfully achieve this via FTP.
Would rcp or ftp be more suitable for this task? And how would I go about using them?
EDIT : To clarify, the script/batch file will be running on the Windows machine and pulling the files from the UNIX machine to windows.
It may be possible to escape the colon in the destination part. Have you tried quoting the destination?
rcp -r unixhost.user:/example/directory "C:\Directory"
It's been a while since I've done any command-line stuff on windows, but I remember the backslash character always being problematic. You may need to use forward slashes in the destination, since the rcp command is consuming the command line. You may also be able to use the backslash as an escape character, so you might try the following:
rcp -r unixhost.user:/example/directory C\:/Directory
If that won't work, you can explicitly set the current drive letter before calling the rcp command. If you're using a batch file, try the following two lines:
c:
rcp -r unixhost.user:/example/directory \Directory