I have a program right now that uses the "net use" command to access a shared drive on a network computer at my work. This works fabulously and I'm able to copy a file from the remote machine onto my own using the FileUtil library from apache. Now I'm wanting to run batch file located on the remote machine and I want it executed in that environment not my own local machine. At first I thought I could just use the "net use" command again with no problem but when I execute the batch file, it is run on my own machine not the remote one. However, this is not what I want to happen.
So, is anyone familiar with this process of executing batch files on remote machines. Better yet, from inside Java.
Thank you for any feed back I greatly appreciate it!
I don't know much about java but can you execute external programs from it? If the answer is "yes" you might try with PSEXEC from Sysinternals it runs every kind of things remotely.
Related
I'm writing a program in java and I was curious as to whether there was any command or code that could be added to make it always run on startup. For example once someone downloads the program it runs whenever they log into their computer. I'm not sure if this matters but I'm writing it in the Netbeans IDE.
You could give your application an option (user can choose it) that will create an entry in the startup/run-once part of the operating system. On windows you could write to the registry to set the option (maybe program need higher priv. to do that) or you could put a link to the startup folder. This might depend on the operating system.
Try Java Service Wrapper
http://wrapper.tanukisoftware.com/doc/english/introduction.html
This lets you run a Java application as a Windows Service or UNIX Daemon.
Ok, so I have a couple of Java programs that I'm running using a chron job on a linux server. These jobs run every ten minutes or so, take literally two minutes to run, and then exit. I need to add a way for the programs to detect, when they start up, if there is already an instance of themselves running, and if so to exit without going any further. I'm really not sure of the best way to handle this though and am hoping someone can offer some advice.
One approach I've considered is to run a command line argument from the java code that does some sort of PS command and looks through those to see if it's running. This seems pretty finicky and complex though for something so small. Plus, I'm not all that knowledgeable with linux and am not even sure the best way to do that. If anyone has some better thoughts, please let me know. Or if that is the best way, if you could provide the linux commands I'd need I'd appreciate it. Thanks.
If you have a writable /tmp directory you can use a lockfile.
When your Java program starts up, check for a file with a name unique to your application (e.g. "my-lock-file.lock") in the /tmp directory. If none exists, create one, and remove it when you're done. If one exists, just exit.
You can check the existence of a file with the .exists() method of the java.io.File class.
If your code needs to be portable, you can use System.getProperty("java.io.tmpdir")); to get an appropriate temporary directory for the platform your code is running on.
You could look at JMX and the Attach API to query for running JVMs.
Or, as Andrew logvinov mentioned, by using a lock file.
If you are using Java WebStart, there's already native support for this.
Many programs solve this by creating a temporary file that points to their PID (often referred to as a "lock" file). The filename should encode all relevant information to distinguish this process from other processes that could legitimately run in parallel.
For example, if the process is bound to a user, it should contain the user name. If the process is bound to a machine, it should (also) contain the hostname (if you put it in machine-bound temp. directory, this is debatable. If you put it in a home directory, think of the case of multiple machines sharing a home via NFS).
The location of these files is typically /tmp. This is a great location, as /tmp is typically wiped during system boot, so no orphan files are left in case of a system crash. Another solution employed by some programs is to put the lock file in the user settings directory, if it is related to the settings. E.g. mozilla thunderbird has a file called /home/<username>/.thunderbird/<profilename>.default/lock.
The file should contain the PID of the process. The idea is simple: If the file contains the PID, it is easy to check whether this process is indeed still running. So if the process crashes, the file gets orphaned. The new process instance will check the PID in the file, see that it is not running any more, and ignore the file (overwrite).
Putting it all together, you could create a file like this:
/tmp/myawesomeservice-username-hostname-lock
With the content:
12345
I need to create a small utility. Using Java, remotely (from another machine) I need to search for files (whether particular files exist or not). I would know exact names of files, or if not, I would know their partial names (so I will be using wildcards).
The problem is: I cannot install anything on that machine (it's a corporate box), but I am allowed FTP, Telnet, SSH login, etc. I would not want to use FTP because there are hundreds of directories and there's no way to know ahead of time where the files are each time (using ftp command LS on each directory every time is simply unproductive). So the best would be to use internal unix 'find' command. Or a similar approach.
I need the best solution to the problem: how can I search for files (from hundreds of thousands files in many different folders) remotely on another machine.
Jsch will allow you to connect quite easily to a remote machine and execute operations using SSH. There is not a lot of documentation, but these examples shows about everything you can do, from basic Shell operations to SFTP and much more.
You can locally exec ssh and have it execute the find command on another machine.
e.g.
ssh user#host "find /home/foo -name index.*"
You can exec that from within the JVM and read the output from the process.
Someone can tell me the best way, through a web Java application, how to run an application on a remote machine (linux) and how to know if this application has already completed. I know the application terminates automatically, so just wanted to wait for the answer.
any help or advice are very welcome.
I was confused by the wording, so I will assume the following: the web application and the remote application are running on two different machines, with the remote application on Linux.
If that is the case, then you would be better off using a SSH library for Java (there are several - JSch, sshj, Ganymed SSH-2, to connect to the remote machine and run commands on it. This is a better approach than using ProcessBuilder, for it abstracts you from the problem of providing your password to the terminal (which can get quite tricky).
I'm not sure what you meant by this statement: "I know the application terminates automatically, so just wanted to wait for the answer." so I'll make another assumption that you want to know if the remote process terminated successfully or not. I'm unsure if any of the SSH libraries posted above, will allow you to get the remote process status in a non-trivial way, so you are better off writing a shell wrapper to your remote command that will return a parseable message.
If you are running both on the same machine, use ProcessBuilder.
I'm creating a test which will perform some stress testing of the app which talks to a Postgres database. In order to make it replicable I'd like to recreate the database on every run. Startup method decorated with org.junit.Before annotation will drop the database (if it exists) and restore it from a dump file. Doing this form shell/command is trivial:
psql -f db.out newdb
or something along these lines. Since the test may be performed on a JVM that's not running on the same machine which hosts the database I'm wondering if it's possible to invoke shell/batch script on remote machine from Java programmaticaly.
Thanks
If you have knowledge of the machine details (hostname, login, password) the only thing I can think of at the moment is using a Java SSH library to login to the box and run that command.
Or you could write a script on your local machine which ran the script on the remote machine given the correct parameters.
That's the simplest way I can think of anyway!
If setting up SSH is too complex, you can write a small Java program which listens for connections on a socket and then runs the script. Install that on the Postgres server and connect to it in the test case.
That said, I suggest to install a copy of Postgres on every developer machine. That would make your tests run much faster and you wouldn't get spurious errors when two developers run the tests at the same time. Also, you won't have errors because of network problems, because someone does maintenance on the server, because developer X has changed the DB schema, etc. etc.