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?
Runtime.execis 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 stackoverflow.com/questions/2088917 stackoverflow.com/questions/31776546 stackoverflow.com/questions/44500899 and many more.