12 Replies Latest reply: Apr 1, 2010 10:13 AM by fsze88hoho RSS

    linux crontab

    fsze88hoho
      Hi,

      my question is .... I created a java program and run it by crontab on linux (centos 5.1).
      It is work fine of terminal on linux, but not on crontab on linux system.

      I guess the parameter directory=backupdir may be blank.
      expdp kennam/kennam007@kennam directory=backupdir dumpfile=kennam27-21-2010-0-10-2.dmp logfile=kennam27-21-2010-0-10-2.log
      Is anyone willing to follow up this question?

      Thanks

      the log file
      SHELL=/bin/sh
      USER=oracle
      PATH=/usr/bin:/bin:/home/oracle/bin:/opt/app/oracle/product/11.1.0/db_1/bin
      PWD=/home/oracle
      HOME=/home/oracle
      SHLVL=2
      LOGNAME=oracle
      _=/usr/bin/env
      begin Backup
      expdp kennam/kennam007@kennam directory=backupdir dumpfile=kennam27-21-2010-0-10-2.dmp logfile=kennam27-21-2010-0-10-2.log
      java.io.FileNotFoundException: /opt/app/oracle/backup/kennam27-21-2010-0-10-2.dmp (No such file or directory)
      Schema : kennam DONE!
      expdp koonhey/koonhey007@koonhey directory=backupdir dumpfile=koonhey27-21-2010-0-10-2.dmp logfile=koonhey27-21-2010-0-10-2.log
      java.io.FileNotFoundException: /opt/app/oracle/backup/koonhey27-21-2010-0-10-2.dmp (No such file or directory)
      Schema : koonhey DONE!
      expdp stock/stock007@stock directory=backupdir dumpfile=stock27-21-2010-0-10-2.dmp logfile=stock27-21-2010-0-10-2.log
      java.io.FileNotFoundException: /opt/app/oracle/backup/stock27-21-2010-0-10-2.dmp (No such file or directory)
      Schema : stock DONE!
      end Backup
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
      
      import java.util.Calendar;
      
      import sun.net.*;
      import sun.net.ftp.*;
      
      
      public class backupDatabase {
          String strDateTime = "";
          String fileStrDateTime = "";
          Calendar cal = Calendar.getInstance();
          
          String schemas   = "kennam,koonhey,stock";
          int sleepPeriod  = 900000;
          String ftpServer = "fsze88.dnsalias.com";
          String ftpUser   = "hihi";
          String ftpPwd    = "hihi";
          String ftpPath   = "/shares/USB_Storage/kennam/oracle/ftpbackup/";
          String localDumpPath = "/opt/app/oracle/backup/";
          String mountPath = "/mnt/usb_drive/kennam/oracle/backup/";
          
          public backupDatabase(){
              strDateTime += cal.get(Calendar.DATE );
              strDateTime += "/" + cal.get(Calendar.MONTH )+1;
              strDateTime += "/" + cal.get(Calendar.YEAR );
              strDateTime += " " + cal.get(Calendar.HOUR );
              strDateTime += ":" + cal.get(Calendar.MINUTE );
              strDateTime += ":" + cal.get(Calendar.SECOND );
              fileStrDateTime = strDateTime.replaceAll("(/| |:)", "-");
      
      //        System.out.println(strDateTime);
      //        System.out.println(fileStrDateTime);
          }
          
          public static void main(String[] args) {
              backupDatabase backupDatabase = new backupDatabase();
      
              for (int i = 0 ; i < args.length ;i++){
      //            System.out.println(args.toString());
      String[] splitArg = args[i].split(":");
      // System.out.println("splitArg[0] : " + splitArg[0]);
      if (splitArg[0].equals("schemas")){
      backupDatabase.schemas = splitArg[1];
      }else if (splitArg[0].equals("sleepPeriod")){
      backupDatabase.sleepPeriod = Integer.valueOf(splitArg[1]);
      }else if (splitArg[0].equals("ftpServer")){
      backupDatabase.ftpServer = splitArg[1];
      }else if (splitArg[0].equals("ftpUser")){
      backupDatabase.ftpUser = splitArg[1];
      }else if (splitArg[0].equals("ftpPwd")){
      backupDatabase.ftpPwd = splitArg[1];
      }else if (splitArg[0].equals("ftpPath")){
      backupDatabase.ftpPath = splitArg[1];
      }else if (splitArg[0].equals("localDumpPath")){
      backupDatabase.localDumpPath = splitArg[1];
      }else if (splitArg[0].equals("mountPath")){
      backupDatabase.mountPath = splitArg[1];
      }
      }
      /*
      System.out.println("backupDatabase.schemas : " + backupDatabase.schemas );
      System.out.println("backupDatabase.sleepPeriod : " + backupDatabase.sleepPeriod );
      System.out.println("backupDatabase.ftpServer : " + backupDatabase.ftpServer );
      System.out.println("backupDatabase.ftpUser : " + backupDatabase.ftpUser );
      System.out.println("backupDatabase.ftpPwd : " + backupDatabase.ftpPwd );
      System.out.println("backupDatabase.ftpPath : " + backupDatabase.ftpPath );
      System.out.println("backupDatabase.localDumpPath : " + backupDatabase.localDumpPath );
      System.out.println("backupDatabase.mountPath : " + backupDatabase.mountPath );
      */
      backupDatabase.expdpDumpFile();
      }

      public void expdpDumpFile() {
      Process p ;
      String[] schemaArray = schemas.split(",");
      int noOfSchemas = schemaArray.length;
      String strexpdpcmd ;
      String strcpcmd ;
      String strDumpFileName;


      try { 
      for (int i = 0; i < noOfSchemas ; i++){
      strDumpFileName = schemaArray[i] + fileStrDateTime;

      strexpdpcmd = "expdp " + schemaArray[i] + "/" + schemaArray[i] + "007@" + schemaArray[i] + " ";
      strexpdpcmd += "directory=backupdir dumpfile=" + strDumpFileName + ".dmp ";
      strexpdpcmd += "logfile=" + strDumpFileName + ".log";
      // System.out.println(strexpdpcmd );
      strcpcmd = "cp " + localDumpPath + strDumpFileName + ".dmp " + mountPath;
      // System.out.println(strcpcmd );

      synchronized(this) {
      p = Runtime.getRuntime().exec(strexpdpcmd);
      try {
      Thread.sleep(sleepPeriod);
      }catch (Exception e) {
      System.out.println(e.toString());
      }
      }

      // copy to nas server
      copy2nas(strcpcmd);

      // copy to ftp server
      uploadDumpFiles2Server(strDumpFileName);
      System.out.println("Schema : " + schemaArray[i] + " DONE!");
      }
      } catch (IOException e) { 
      e.printStackTrace();
      }
      }

      public synchronized void copy2nas(String cpcmd){
      try {
      Runtime.getRuntime().exec(cpcmd);
      } catch (IOException e) {
      System.out.println(e.toString());
      }
      }

      public synchronized void uploadDumpFiles2Server(String filename){
      String strFullDumpFileName;
      try {
      strFullDumpFileName = filename + ".dmp";
      FtpClient ftpClient=new FtpClient();
      ftpClient.openServer(ftpServer);
      ftpClient.login(ftpUser, ftpPwd);
      if (ftpPath.length()!=0) ftpClient.cd(ftpPath);
      ftpClient.binary();

      TelnetOutputStream os=ftpClient.put(strFullDumpFileName);
      File file_in;
      file_in = new File(localDumpPath, strFullDumpFileName);
      FileInputStream is=new FileInputStream(file_in);

      byte[] bytes=new byte[1024];
      int c;
      while ((c=is.read(bytes))!=-1){
      os.write(bytes,0,c);}
      is.close();
      os.close();
      ftpClient.closeServer();
      } catch (IOException e) {;
      System.out.println(e.toString());
      }
      }
      }
      Edited by: Devotee on Mar 31, 2010 12:21 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: linux crontab
          sb92075
          From which system (hostname) is java being invoked?
          expdp kennam/kennam007@kennam
          On which system (hostname) does @kennam reside
          expdp koonhey/koonhey007@koonhey
          On which system (hostname) does @koohey reside

          expdp stock/stock007@stock
          On which system (hostname) does @stock reside
          • 2. Re: linux crontab
            sb92075
            accidental duplicate

            Edited by: sb92075 on Mar 27, 2010 8:18 PM
            • 3. Re: linux crontab
              fsze88hoho
              there are three schema.... I want to expdb three of them together on one java program
              • 4. Re: linux crontab
                sb92075
                Since you don't/won't answer my questions, I can't/won't answer yours.
                • 5. Re: linux crontab
                  fsze88hoho
                  On which system (hostname) does @koohey reside
                  your question is hostname right? I forget, I need to check on monday Hong Kong Time
                  I sure they are same linux system with same hostname there...
                  • 6. Re: linux crontab
                    sb92075
                    CUT & PASTE following SQL and results

                    select * from dba_directories;
                    • 7. Re: linux crontab
                      fsze88hoho
                      ok,
                      I will do it on monday Hong Kong time
                      • 8. Re: linux crontab
                        fsze88hoho
                        hostname? means computer on network? ORACLESERVER or HOSTNAME=localhost.localdomain

                        from terminal select * from dba_directories;
                        OWNER                DIRECTORY_NAME            DIRECTORY_PATH
                        -------------------- ------------------------- -------------------------
                        SYS                  BACKUPDIR                 /opt/app/oracle/backup/
                        SYS                  IDR_DIR                   /opt/app/oracle/diag/rdbms/kennam/kennam/ir
                        
                        SYS                  AUDIT_DIR                 /tmp/
                        SYS                  ORACLE_OCM_CONFIG_DIR     /opt/app/oracle/product/11.1.0/db_1/ccr/state
                        I not sure will having same result run from crontab of statement select * from dba_directories;
                        • 9. Re: linux crontab
                          sb92075
                          post results from following commands

                          ls -ld /opt/app/oracle/backup/
                          ls -ld /opt/app/oracle/
                          • 10. Re: linux crontab
                            fsze88hoho
                            I solved it.....
                            Thank you for try your best to solve it for me....
                            • 11. Re: linux crontab
                              EdStevens
                              Devotee wrote:
                              I solved it.....
                              Thank you for try your best to solve it for me....
                              And the solution was ..... what?

                              Even if you end up working out the solution outside of any help offered here, at least post the final resolution/solution so as to help that rare individual who actually searches the forum in search of solutions to their own problems.

                              Then flag the thread as "answered" even if you can't honestly award "helpful" points.
                              • 12. Re: linux crontab
                                fsze88hoho
                                yes, sure
                                previously, I run it as follow
                                /home/fsze88/jdeveloper/backupDatabase/Project1/backupDatabase.jar  schemas:kennam,stock sleepPeriod:10000 > /home/fsze88/Desktop/backup.log
                                afterward, I discovered missed setting of
                                ORACLE_BASE=/opt/app/oracle; export ORACLE_BASE
                                ORACLE_HOME=$ORACLE_BASE/product/11.1.0/db_1; export ORACLE_HOME
                                PATH=$PATH:$ORACLE_HOME/bin; export PATH
                                and run them on one of .sh file as follow
                                ORACLE_BASE=/opt/app/oracle; export ORACLE_BASE
                                ORACLE_HOME=$ORACLE_BASE/product/11.1.0/db_1; export ORACLE_HOME
                                PATH=$PATH:$ORACLE_HOME/bin; export PATH
                                /home/fsze88/jdeveloper/backupDatabase/Project1/backupDatabase.jar  schemas:kennam,stock sleepPeriod:10000 > /home/fsze88/Desktop/backup.log
                                it works
                                please understand why is no award "helpful" points