0

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?

Ram
  • 1,225
  • 2
  • 24
  • 48
  • 1
    Java `Runtime.exec` is **not a shell**. It does not do glob expansion, or redirection, or pipes, or multiple programs in one command. And you're not looking at the stderr from the child (`process.getErrorStream()`) which would have given you several clues what is wrong. Dupe https://stackoverflow.com/questions/2088917/ https://stackoverflow.com/questions/31776546/ https://stackoverflow.com/questions/44500899/ and many more. – dave_thompson_085 Aug 31 '19 at 12:22
  • Possible duplicate of [Java exec() does not return expected result of pipes' connected commands](https://stackoverflow.com/questions/2088917/java-exec-does-not-return-expected-result-of-pipes-connected-commands) – Kaan Oct 14 '19 at 01:46

0 Answers0