This is the command I want to run:
su - postgres -c "pg_dump ....."
to backup the postgres database.
If I am now in linux shell, as a root, it works great.
But now, I want to run it from a java application, as:
String cmd = "su - postgres -c \"pg_dump --port 5432 .....\""
Process p = Runtime.getRuntime().exec(cmd);
// read the error stream and input stream
p.waitFor();
It throws an error:
su: unknown option "--port"
please try "su --help" to get more information
Now I change the code as:
Process p = Runtime.getRuntime().exec(new String[0]{cmd});
// read the error stream and input stream
p.waitFor();
Cannot run program "su - postgres -c \"pg_dump .....\"": java.io.IOException: error=2, no that file or directory
What should I do now?
Besides the answers given above (and it's good to understand your problem) remember that you always can make your life easier by just putting your command inside a shell script file. Eg, a dumppg.sh file which just contains two lines:
#!/bin/sh
su - postgres -c "pg_dump ....."
just make it executable, and (after testing it) call it from java.
exec(new String[]{"sh", "-c", "su - postgres ..."});
Related
I am trying to copy a file via ssh from remote server to client. When executing my command on cli, the file is being copied to the correct location. But if I am executing the exactly same command via Java, the file is being copied to the remote machines directory instead of the clients one.
This is the command I am using (I am using a key s.t. no password has to be entered):
ssh -i /home/user/.ssh/id_rsa -C -p 2201 test#localhost 'cat /opt/test/file.txt' > /home/user/file.txt
And this is the code, quite simple:
Process p;
p = Runtime.getRuntime().exec("ssh -i /home/user/.ssh/id_rsa -C -p 2201 test#localhost 'cat /opt/test/file.txt' > /home/user/file.txt");
Any idea why the file is being copied to the wrong machines directory and how to get it copied to the clients directory instead, when executing the command using java?
Don't use > in the command. Read the data via an InputStream retrieved from the resulting Process object. As with ~ mentioned in an earlier version of the question, > is a shell specific feature, and a shell isn't involved in exec().
Process p = Runtime.getRuntime().exec("your command");
p.waitFor();
InputStream commandOutput = p.getInputStream();
Read from this stream and write it to a file.
Alternative:
Try
p = Runtime.getRuntime().exec("sh -c ssh -i /home/user/.ssh/id_rsa -C -p 2201 test#localhost 'cat /opt/test/file.txt' > /home/user/file.txt");
in my Java Application i start a postgres process to backup my database.
Thread thread = new Thread(() ->{
Process p;
try{
p = Runtime.getRuntime().exec("cmd /c pg_dump -v -a -d "+database+" -h "+server+" -p "+port+" -U "+user+" -n public > " + file.getPath());
p.waitFor();
}catch(Exception e){
return false;
}
});
This works and the backup File was created. But the File size is 0KB while my Application is running. After i close the Java App - the backup file have its normal size.
I dont get it where the Problem is
I am sorry but its hard to me to explain this right.
in my application i try to create a Database dumpfile using pg_dump from PostgreSQL. If i start the dump process then the file will be create after i close the whole application.
i hope i explain it understandable
i tried to predefine the path to file. but it was the same error. I also used the ProcessBuilder intead.
The Problem was that i used the -v Option in the pg_dump command. Without this Option it works fine and the file will be created immediately.
It is option for verbose output. Here is my final Method which works fine:
Process process = new ProcessBuilder("pg_dump", "-a", "-d", database, "-h", server, "-p", port, "-U", user, "-f", file.getPath()).start();
process.waitFor();
I am guessing that it probably has something to do with the file and the way you handle it (my guess is based on file.getPath() in your code).
However, you haven't posted the whole code ... So, The the only thing I can do is guess.
Try the same code with predefined file name (lets say c:\\myfilename.dmp).
e.g.
p = Runtime.getRuntime().exec("cmd /c pg_dump -v -a -d "+database+" -h "+server+" -p "+port+" -U "+user+" -n public > c:\\myfilename.dmp"
How does it behave ?
I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.
Here is my attempt:
String Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);
The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?
Java will not magically select bash as your executable. You probably want to do something like
"bash -c <your command>"
See this question:
How to run unix / shell commands with wildcards using Java?
(Also the | is a bash-thing. Java won't magically create pipes between processes.)
When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.
a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.
There are two things to note right off the bat....
The shell script runs fine manually
A simple shell script (echo hello) that I wrote runs fine through java
So I have a shell script that I'm attempting to run through a Java process.
File sqlF = new File("path to deploy script");
Process proc = rt.exec(sqlF + "/deploy.sh");
proc.waitFor();
System.out.println(proc.exitValue());
When I run this code I get an ambiguous return value of "1".
Here's the shell script (because I imagine the issue may stem from here):
#!/bin/bash
mysql -u XXXX -h XXXXX < XXXXX.sql
mysql -u XXXX -h XXXXX database < DEPLOY-HELPER.sql
Any ideas as to why this would not execute properly from Java?
If you want to run a shell script you must explicitly invoke the shell and pass it the name of the script as an argument, as in
bash /path/to/script/deploy.sh
Neither Runtime.exec() nor ProcessBuilder know how to execute shell scripts themselves, they only know how to execute binary executables.