I want to write a java code that executes Linux command to copy a directory from server to another server.I got a directory which contains sub directories and files. So I need to copy all these contents to another server. How can I use Linux command for that purpose in java.
Thanks a lot!
You could look at these good post's here
The only thing that would change is command. there user was asking for diff, here you are asking to copy between two machines. I am assuming you could do passwordless ssh from your source machine to destination and if that's the case you could use scp command to copy the directory like below:
scp -r /home/test/blah imauser#thathost:/home/test/destination/directory
-r will recursively copy directories as well to your existing directory as above on thathost.
Related
I am trying to use JMeter to perform an end to end test. The test involves writing to SFTP folder and reading a file generated as a result of write operation from another SFTP folder.
I am able to connect to SFTP folder using JMeter SSH SFTP plugin and able to successfully write / read SFTP folder contents.
The application under test, creates an output file based on the input file (put by JMeter). The challenge now I have is to read the contents / file which is created on the SFTP folder.
The application under test writes a file with an date-time string which JMeter may not know hence I am trying to read the latest file.
The JMeter SSH SFTP plugin provides a number of options i.e. ls, rm, rmdir, etc however, I have chosen the edit option (${sftp username#servername 'ls -ltr /server/path | tail -n 1'}) and tried to use the following in order to read the file however, I neither see error nor response.
I would appreciate any pointers and if you can think of a better solution. Please also let me know if you would like me to share more information.
Thanks in advance.
You're using the wrong sampler, if you want to run a command you need (surprisingly) SSH Command Sampler
This pipe symbol | is not a parameter for ls command, it's a part of Unix shell, in majority of cases it would be bash so you need to amend your command to look like:
/bin/bash -c "ls -ltr /server/path | tail -n 1"
example SSH Command Sampler configuration:
and example output:
More information: How to Run External Commands and Programs Locally and Remotely from JMeter
copy a file to Unix server from windows shared folder using unix command
Example:- A shared drive on windows is- hostname\folderName and it contains a file name Test.txt inside it. Now I want to copy it on Unix server to process further.
How can I do it using Unix shell script or some coding concept of scala/java/python ?
Any suggestion will be highly appreciated.
You could try scp command, if you're using any shell on your windows try:
scp local/path your-name#ip.to.your.server:/path/to/send
You could mount the shared drive and then copy the file to the server. Using commands in the Unix shell,
mount -t cifs //hostname/folderName /mnt/pathformount
cp /mnt/pathformount /pathforlocalcopy
Don't forget to unmount the drive when you are done.
umount /mnt/pathformount
I'm trying to run a Vert.x Java based application on a Docker container. My application runs few verticles which it initiates from within itself.
I've put the jar file on a folder and created a Dockerfile with the following content:
FROM vertx/vertx3
ENV VERTICLE_FILE Medical-1.0-SNAPSHOT.jar
ENV VERTICLE_HOME /performit/web/vertx/verticles/
COPY $VERTICLE_FILE $VERTICLE_HOME/
WORKDIR $VERTICLE_HOME
ENTRYPOINT ["sh", "-c"]
EXPOSE 8080
CMD ["java -jar $VERTICLE_FILE"]
USER daemon
I create an image with the command
$ sudo docker build -t medical-main .
I then attempt to create a container with the following line:
sudo docker run --name medical-main -p 8080:8080 -d medical-main
This fails and the log shows the following:
java.lang.IllegalStateException: Failed to create cache dir
at io.vertx.core.impl.FileResolver.setupCacheDir(FileResolver.java:257)
at io.vertx.core.impl.FileResolver.<init>(FileResolver.java:79)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:138)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:114)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:110)
at io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:34)
at io.vertx.core.Vertx.vertx(Vertx.java:79)
What am I missing?
Izhar
Judging by FileResolver.java, vert.x tries to create a ".vertx" directory in the current working directory by default. You have configured a user called "daemon", are you sure that this user has write access to the working dir in the docker image? If not, change the permissions as outlined in docker-image-author-guidance, or revert to using the root user.
This directory is used to serve files contained in jar files (for example web assets packaged in a fat jar). If you are not using this feature, you can disable the creation of this directory by setting the vertx.disableFileCPResolving system property to true. You can also change the location using the vertx.cacheDirBase system property.
Reference:
https://groups.google.com/forum/#!topic/vertx/7cBbKrjYfeI
This exception is caused when Vert.x tries to create .vertx (cache dir) so that it can copy and read a file from the classpath or file that's on the classpath. It's possible, the $user doesn't have permission to create the cache directory.
The reason behind cache dir is simple: reading a file from a jar or from an input stream is blocking. So to avoid to pay the price every time, Vert.x copies the file to its cache directory and reads it from there every subsequent read. This behavior can be configured.
vertx run my.Verticle -Dvertx.cacheDirBase=/tmp/vertx-cache
# or
java -jar my-fat.jar -Dvertx.cacheDirBase=/tmp/vertx-cache
Otherwise, you can completely avoid this behavior, launch your application with -Dvertx.disableFileCaching=true. With this setting, Vert.x still uses the cache, but always refresh the version stored in the cache with the original source. So if you edit a file served from the classpath and refresh your browser, Vert.x reads it from the classpath, copies it to the cache directory and serves it from there. Do not use this setting in production, it can kill your performances.
link to documentation
For me, this same issue was coming for while trying to run a jar file. It started coming suddenly and then I was forced to run the jar file as ROOT for sometime until I finally got fed up and started looking for reason thoroughly.
It happened because I accidentally ran jar file once in SUDO
privileges and the .vertx folder was create as ROOT account.
I could not figure this out initially as I was trying ll alias
command in amazon linux and sadly it does not display hidden folders
So when I was thoroughly investigating the issue next time, I also tried ls -al which showed in .vertx folder and I figured out that issue was it being created as SUDO user.
Deleted .vertx folder and jar file started working normally again as
normal user.
vert.x tries to create a cache-dir (.vertx/file-cache-someuuid) in the current directory. The given exception will be thrown if the mkdirs() call fails.
Has the user daemon sufficient rights in the workdir?
I want a list of .tar.gz files from a specified directory. For this I am running "ls directory_path/*.tar.gz" using getRuntime.exec() in Java. But it is not giving any output rather it is saying No such file or directory..
But the same command is running on command prompt..
I am running java in unix.
Thanks in advance
This is because exec won't launch a shell just to run your program. It just starts a process. On Unix-like systems the shell is responsible for expanding wildcards to lists of files. So you would need to run ls through a shell to get the desired behaviour.
However, why do you use ls at all? This answer shows how to get a list of files with Java.
1. You will be able to fire processes using the exec() not the command promt..
2. Better use list() to get the names of all the files in a directory, and then use FileFilter to get the .tar.gz files.
Let's say I make a Java project in Eclipse that has 3-10 classes and one of which has a main(String[] args) method that starts the whole program and takes 4 arguments at the command line. Let's also say that this project has 6-10 .jar files in src/lib that it needs to run.
If I have ssh access to another computer (UNIX on both ends) and I want to run this program, how exactly do I go about doing so?
I ask because I have been doing some distributed computing projects and I need to run my program on multiple machines but I am a total command line noob and I don't have physical access to all of the machines.
Edit:
Seems I need to SCP the files over. Can someone show me the particular command that makes the Java program run? Including what directory I should run it from and how to include the JAR dependencies.
to run
java -jar thejarfile.jar "arg1" "arg2" "ectect.."
if you want to run it in the background java -jar thejarfile.jar "arg1" "arg2" &
to kill it if its in the background ps -aux to get the id and then kill (id number)
In general, you use ssh to log into a remote computer, and then you run programs from that machine's storage, using that machine's resources.
So if you want to run your Java program on a different machine, you'd need to copy the required files there, then ssh to that machine and run the program from the remote command line.
You write a tiny bootstrap program that will be your MicroKernel. You SCP that MicroKernel to the remote machine. This program will use a custom ClassLoader to pull the rest of the dependencies for your real application down into memory or some storage onto the remote machine. Look into the URL ClassLoader for some help as it can load JAR files from HTTP addresses.
Or you can just zip up your whole program, SCP it to the remote machine, unzip it then run 'java' like normal. If you have SSH access you should have access to scp files onto that machine. If not you can always SSH into the remove machine then scp them from your machine.
Example:
ssh myname#myremotemachine
> mkdir /location/to/program
> scp myname#mydevmachine:/location/to/program/* /location/to/program
This all works really well if you have your SSH keys setup properly and don't have to put in a password.