I am trying to run following command on AIX to backup files occasionally.
tar cvf - /tmp/emflextmp/* |gzip > /APP/emflex/scada/Db/backup/dbBackup_31_Aug_2019__15_51_04.tar.gz
If I run this command on terminal through same login it works perfectly. Now I want to do it through the java app. My code looks like this:
Process process = Runtime.getRuntime().exec("tar cvf - "+tempFolder+"* |gzip > " + strBackupFolder + File.separator + "dbBackup_" + new SimpleDateFormat("dd_MMM_yyyy__HH_mm_ss").format(new Date()) + ".tar.gz" );
int exitVal = process.waitFor();
I get exit code 2 here.
From logs the command looks ok
2019-08-31 15:51:04:565 [INFO.] [Thread-16:ID=76]:[GarbageCollector.java:332] Running H2 Database Backup Command On AIX =[tar cvf - /tmp/emflextmp/* |gzip > /APP/emflex/scada/Db/backup/dbBackup_31_Aug_2019__15_51_04.tar.gz]
None of the output files are created. What is the problem here?
Related
I am trying to change out my hosts file based on whichever environment I run my script in. It looks like what I'm needing to do is run the commands as admin (elevated privileges), I just cannot seem to find out how to do that in AutoIT.
Here is an example of the call from Selenium:
Runtime.getRuntime().exec("C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");
Here is my code in AutoIT:
$filePath = $CmdLine[1]
$env = $CmdLine[2]
Run(#comspec & " /c DEL %WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS")
if $env = "Dev" then
Run(#comspec & " /c COPY "&$filePath&"hostFiles\DevHost.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")
endif
if $env = "QA2" then
Run(#comspec & " /c COPY "&$filePath&"hostFiles\QA2Host.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")
endif
How would I tell AutoIT to run as admin?
As a starting note, your account should have elevated privileges.
As far as I know you have options here:
the most desired and safe one
Run your Java application (IntelliJ, Eclipse) as Administrator. This way, any processes spawn or executed by it would be ran with elevated privileges (this works as a chain reaction in Windows and not only).
Run it with elevated privileges
Runtime.getRuntime().exec("runas /profile /user:Administrator \"C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");
I'm trying to move a file from Java Application Server to DB Server. For that I'm using a shell script. To run the shell script, I'm using Process in Java File. While trying as a stand alone java(class file) in application server using putty, it's moving to DB server. But while trying from application, its not working and process.waitFor() is returning 1.. Need Help.???
Code:
Process p = Runtime.getRuntime().exec("sh "+asyncFilePath+"/ManualAdjFileTransfer.sh "+asyncFilePath+ " "+destPath+" "+ destUserId + " " + destIp + " " + asyncFilePath + " ManAdj_File_Transfer.Log");
p.waitFor()
It could be a problem of permission:
Process p = Runtime.getRuntime().exec("chmod u+x "+yourscript.sh);
Related Question: Write an executable .sh file with Java for OSX
I am trying to download a XML file from a FTP server with wget in my Java programm.
I have to wait until it finishes the download.
String command = "WGET -O "
+props.getProperty("xmlFolder")+""+
+ rs.getString("software")
+ ".xml ftp://"+props.getProperty("ftpUser")
+":"+props.getProperty("ftpPasswort")+"#"+rs.getString("xmlPfad");
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("downloaded!");
Without the waitfor() it works perfectly, but with this function it stucks after 2-3 MB are downloaded. Any suggestions?
Have you tried to use the --quiet option for wget?
EDIT 1:
The pipe's write side (child process) might be full.
EDIT 2:
From openjdk-6-src-b20-21_jun_2010
In jdk/src/solaris/native/java/lang/UNIXProcess_md.c (at least for a UNIX system) we can see how Java launches a new child process and how it is using pipe to redirect stdout and stderr from child (wget) to parent process (Java)
I have a problem with a Java program. I uses a tool "ugpc" to get the assembly structure and write it in a textfile. When i start the program under netbeans it writes these files without problem. but when i compile it to a jar file and run this jar, i get the error "Error: Fatal system Error 1020005" written in the proc.getErrorStream(); and no .txt file ist written in the ouput. Also in the ErrorStream appears that the textfile can not be found, but he should write it and not open it. My PC is new installed and before that formatting the tool was running good. I use Java 7. When I manually start the command via cmd it works also fine. We startet the tool under another user account and the same error appears.
Process proc = Runtime.getRuntime().exec("cmd /c " + "c:\\ugs\\nx6\\UGII\\ugpc -a -s2 " +"S:\\Geraete\\L\\0154\\E2\\6169-001000-201_Rev-E2_DWG.prt" + " > " + "S:\\LAUF5\\1\\6169-001000-201_Rev-E2_DWG.txt");
proc.waitFor();
When I run the same command from Java via Runtime.getRuntime I get the return code 6. The same command works fine from command line:
process = Runtime.getRuntime().exec(mysqldumpCommand);
int processComplete = process.waitFor();
For those 2 commands I get the return code 6 when run from java and no dump. The work fine from command line(I don't have a password on the local env.)
mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql
mysqldump --user=root --password="" --host=localhost dbname > c:\temp\dumpfile.sql
When deliberately put wrong password I get return code 2 in java, and a connection error in command line:
mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql
The return codes as I found them here:
Taken from client/mysqldump.c in MySQL 5.1.59:
#define EX_USAGE 1
#define EX_MYSQLERR 2
#define EX_CONSCHECK 3
#define EX_EOM 4
#define EX_EOF 5 /* ferror for output file was got */
#define EX_ILLEGAL_TABLE 6
How come I get (error) return code 6 when running the same command in java and works fine from command line?
Later Edit: I try it from Windows.
Runtime.exec is not a shell, so redirections with > and < won't work. Currently the command is passing > to mysqldump, which interprets it as the name for the table you want to export. (Hence return code 6, "illegal table".)
There are two solutions:
Run a shell. Use this command instead of the one you have:
cmd.exe /c "mysqldump --user=root --password= --host=localhost dbname > c:\temp\dumpfile.sql"
Write the output from the command to a file yourself, with Process.getInputStream().
The mysqldump now supports --result-file=
So it would look like this.
mysqldump --user=root --password= --host=localhost dbname --result-file=c:\temp\dumpfile.sql