Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2010
    Posts
    15

    Unanswered: Creating a Scheduled Task

    I have a query I'd like to run every 5 minutes and write to a text file. Unfortunately, since the database isn't configured for TCP/IP (and won't be) I can't use the Task Center.

    I thought instead I would use a Scheduled Task in windows for something quick and simple. However, I am unable to get it to work correctly.

    This is what I have:

    Code:
    @ECHO OFF
    G:\
    cd dbScripts
    db2cmd
    db2 connect to dbMyDatabase
    dbscript.bat >>C:\scriptresults.txt
    Unfortunately, when it gets to the "db2cmd" command, it opens a new window. The next commands get run in the windows command window and are not executed properly.

    Any idea how I can get this to work?

    Windows 2008 64-bit
    DB2 v9.7.400.501

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1

  3. #3
    Join Date
    Jun 2010
    Posts
    15
    Hmm, I'm close, but still not all the way there.

    If I use this:
    Code:
    @ECHO OFF
    db2cmd /c /w /i 
    db2 connect to dbMyDatabase
    g:\dbScripts\dbscript.bat >>C:\scriptresults.txt
    I get this:

    Code:
    DB21061E  Command line environment not initialized.
    So I think the db2cmd window is closing before it can connect and run the script.

    I tried this:

    Code:
    @ECHO OFF
    db2cmd /c /w /i db2 connect to dbMyDatabase g:\dbScripts\dbscript.bat >>C:\scriptresults.txt
    And got:

    Code:
    SQL0104N  An unexpected token "g:\dbScripts\dbscript.bat" was found 
    
    following "<identifier>".  Expected tokens may include:  "USER".  
    
    SQLSTATE=42601
    Which makes me think it was trying to run the connect to command and the script at the same time.

    The script doesn't work if I remove the "connect to" portion. I also tried adding a semicolon after "connect to dbMyDatabase;" but that didn't fix it.

    I feel like I'm almost there, I just need to separate the connect to part from the script part. without closing the db2cmd window.

  4. #4
    Join Date
    Jun 2010
    Posts
    15
    I fixed it by adding "db2 connect to dbMyDatabase" into the dbscript.bat file, so now it looks like this:

    Code:
    @ECHO OFF
    db2cmd /c /w /i g:\dbScripts\dbscript.bat >>C:\scriptresults.txt
    Seems to be working fine now!

  5. #5
    Join Date
    Feb 2012
    Posts
    22

    Set (Windows) environment variable DB2CLP to string **$$**

    Be sure to Set (Windows) environment variable DB2CLP to string **$$**

    This trick will :
    "auto" detect the PID of the instance ( actually the process/instance variable DB2INSTANCE is pointing to )

    When issuing DB2 commands in a batch script, f.i. in task schedular it will prevent orphaned CMD.EXE processes hanging around.... !!!

    (it actually prevents a new CLP command shell being opened, that isn't closed after the script has ended)
    **$$**

    Therefore: YOU DO NOT NEED DB2CMD.EXE when using DB2CLP set to

    By the way:
    It can also be used in PowerShell
    see :

    Run DB2CMD commands through PowerShell
    Run DB2CMD commands through PowerShell
    Set environment variable DB2CLP to string **$$**

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •