Announcement

Collapse
No announcement yet.

scheduled tasks cmd files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • scheduled tasks cmd files

    I have a simple .cmd file that I was running to zip and move logs from certain servers.

    cd "c:/program files/winzip"
    winzip32 -min -a -p c:/logs/test.zip c:/logs/x*.log
    cd "c:/logs"
    del x*.log
    move *.zip \\fileserver\logs

    The logs grow to be several gigs per day and can take a few mins to zip. If I click the cmd file it runs perfectly. If I schedule the cmd file as a task however, it simply deletes all of the files and quits. What's the difference between a scheduled task running a command file and clicking the file?

  • #2
    Re: scheduled tasks cmd files

    A scheduled task that is running-as a local account instead of a domain account could be refused by the other computer to connect or, the user might have no sufficient permissions to make changes in the shared network resource on the server.

    When the Task Scheduler service runs a job by using a specific account, the Task Scheduler service tries to determine whether this account is also the account of the current logged-on user. Only if the account is that of the current logged-on user, the security context of the scheduled task is mapped to the security context of that user. This process directs scheduled jobs to that user's desktop.

    If the Task Scheduler service runs a job by using the built-in System account of the computer (when using the AT.exe command), it will be the computer (the client) that makes the connection to the other computer. Domain clients are member of the Authenticated Users group (that group contains both user and computer objects). Check the permissions for that group or else for the group Domain Computers on the share.


    A few comments about the batch,
    - To ensure the batch will wait until Winzip is finished, use the Start command with the /wait switch to execute winzip32.exe.
    - Use the /Y switch for the Move command, because when running the batch as a scheduled task it cannot wait for user action.

    \Rems


    FYI
    sample how to create unique filenames for the zipfile
    Code:
    @echo off
    SetLocal
    
    :: Do not display "Add Complete" dialog box
    Set "sRegkey=HKCU\Software\Nico Mak Computing\WinZip\caution"
    Set "sValueName=AddComplete"
    >nul,2>&1 (Reg.exe query "%sRegkey%" /v "%sValueName%" &&Set "sOverwrite=/F")
    reg add "%sRegkey%" %sOverwrite% /v "%sValueName%" /d 0
    
    
    rem ## OPTIONAL ##  - - - - - - - - - - - - - - -
     :: Get date and time parts
     :: (to create the DateTime stamp for the file name)
    
    for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
       for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
          (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set YYYY=%%p)
       )
    )
    For /F "tokens=1-4 delims=:., " %%i in ('echo.%time%') Do (
       (Set HH=0%%i)&(set NN=%%j)&(set SS=%%k)&(Set Sd=%%l)
       
    )
    Set "HH=%HH:~-2%"
    rem - - - - - - - - - - - - - - - - - - - - - - -
    
    
    Set "winzip=%programfiles%\winzip\winzip32.exe"
    Set "newzipfile=%YYYY%%MM%%DD%.%HH%%NN%%SS%-Test.zip"
    
    :: Action
    PushD "C:\test" &&(
       "%winzip%" -min -a -p "%newzipfile%" "x*.log"
       del /A /F "x*.log"
       move /Y "%newzipfile%" "\\fileserver\logs"
    )
    PopD
    
    If NOT Defined sOverwrite (
    reg delete "%sRegkey%" /f /v "%sValueName%"
    )
    
    EndLocal
    
    goto :EOF
    Last edited by Rems; 21st October 2008, 18:53.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: scheduled tasks cmd files

      Thanks for the reply Rems. Unfortunately I also tried running the task under my own domain account (enterprise admin w/ full rights on the local machine) and tried logging in as myself when running the task, but the scheduled task still acts the same.

      I had originally run it as a two separately scheduled batchs to solve the wait issue (also tried ping 127.0.0.1 -n 1 -w 60000 >null but had trouble getting the timing right). I had combined the two for testing because the first zip part started failing on a few servers, and the del/move portion would still run, after the first task did nothing, and delete files (which is what is still happening when I run the scheduled task.) I would add some kind of error checking ie checking to see if there is a zip file created before deleting the logs, but my scripting skills are horrible.

      If I leave the second part off (zip only, no del and move) and run the task (versus clicking the batch), it sits the "running" indefinitely, but doesn't seem to be doing anything. Not sure if there's a way to check for errors on the scheduled task?

      Sorry, one other important point I forgot to mention. These scripts were working on a group of servers for several weeks. Just recently a few servers started to fail and all on the same day. All other tasks run fine, just not this particular task. These servers are all identical to one another.

      ren test.zip %computername%-%date:~12,2%%date:~4,2%%date:~7,2%.zip is what I used for the timestamps since it only runs once per day. I posted the above for simplicity's sake.

      Comment


      • #4
        Re: scheduled tasks cmd files

        Originally posted by f21 View Post
        (also tried ping 127.0.0.1 -n 1 -w 60000 >null but had trouble getting the timing right)
        If you use a pingable address here, the ping -w switch value doesn't do anything, the pause time will then just depending on the amount of ping repeats (try -n 5 for about a 4/5 seconds pause when pinging %computername% or localhost). Redirect the output to >nul not null.

        I just discovered that the command Start ""/w /b doesn't wait onreturn of the winzip program, because of the /b switch. Try this instead:
        Start "Packing logfiles"/min /w "%winzip%" -min -a -p "%newzipfile%" "x*.log"
        (I have corrected the sample batch in my previous reply)

        Originally posted by f21 View Post
        All other tasks run fine, just not this particular task
        Just to be sure, you don't make use of mapped drives in the batch??
        And, the local folder permissions are OK for the user that is programmed to Run the task, non of the logfiles in the folder are 'in-use' by an other program?. And, the share and ntfs permissions for the shared folder allow changes made by this user. And the 'runas' user's password is not expired or has been changed. Check also if the 'Secondary Logon' service is running on the computer.
        You are sure winzip installed on each server?


        Originally posted by f21 View Post
        %computername%-%date:~12,2%%date:~4,2%%date:~7,2%.zip
        Note that the values for the Environment variable substitution is depending on the OS's Language.


        Originally posted by f21 View Post
        Not sure if there's a way to check for errors on the scheduled task?
        You can try by writing all the batch output (including errors) to a file.
        Code:
        @echo off
        SetLocal
        
        :: Get date and time parts
        :: (to create the DateTime stamp for the file name)
        
        for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
           for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
              (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set YYYY=%%p)
           )
        )
        For /F "tokens=1-4 delims=:., " %%i in ('echo.%time%') Do (
           (Set HH=0%%i)&(set NN=%%j)&(set SS=%%k)&(Set Sd=%%l)
           
        )
        Set "HH=%HH:~-2%"
        
        
        Set "winzip=%programfiles%\winzip\winzip32.exe"
        Set "newzipfile=%YYYY%%MM%%DD%.%HH%%NN%%SS%-Test.zip"
        
        :: Write output from all commands in this batch to a test logfile
        Set "tstfldr=c:\testlogs"
        If not exist "%tstfldr%" MkDir "%tstfldr%"
        >"%tstfldr%\%newzipfile%.LOG" 2>&1 (
           PushD "C:\test" &&(
              Start "Packing logfiles"/min /b "%winzip%" -min -a -p "%newzipfile%" "x*.log"
              (ping 127.0.0.1 -n 5)>nul
              del /A /F "x*.log"
              move /Y "%newzipfile%" "\\fileserver\logs"
           )
           PopD
        )
        
        EndLocal
        EDIT 2:
        I had to change switches -> Start /w to Start /b in this batch, otherwise the output of winzip32.exe will not appear in the test logfile. Therefore I now added a Timer after the command to pause the batch for about 5 seconds (the time could be not sufficient though, or the timer could not be nesserary, I don't know that for winzip32.exe)



        \Rems
        Last edited by Rems; 21st October 2008, 18:28. Reason: Changed switches Start /w to Start /B for the log writing!!!

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: scheduled tasks cmd files

          Woops... sent a long reply on friday, but looks like it never saved. The script still just deletes the existing logs without ever zipping the files (and if I remove the deletion/move part, it just sits there running but does nothing).

          I'm still a bit curious as to why it works when run manually (clicking on the batch), but not when run as a task. Secondary logon is running, but even when I run it under my own account while logged in as myself (this should be the same as manually clicking it at this point while logged in as myself, right?) the task just hangs.

          As far as making sure the runas account is working, I'm sure it is, since the task shows status running and the "second" part of the script executes. Permissions should not be an issue either since I've tested this with my own account (that I've logged onto the server with as well) which runs with full permissions.

          And yes, winzip is installed

          Thanks for the help on the delayed ping. I knew I had written something wrong. That part is working now, but it still doesn't seem to resolve the issue. I'm testing just with the zip portion of the script to eliminate the possibility of action conflicts.

          Comment


          • #6
            Re: scheduled tasks cmd files

            I have modified my first script a bit.
            installed WinZip and tested the script http://forums.petri.com/showpost.php...12&postcount=2

            It worked fine when running the batch manually
            And there were no problems when running it as a scheduled task under my domain\account neighter when running the tast as the local\administrator.
            I did not used the WinZip Command Line Support Add-on, do you?

            You are sure you don't use mapped drive letters in your batch?
            If the problems started on all severs the same time it is somehow related to the RunAs account. (?)
            Do you use a evaluation version of Winzip?


            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: scheduled tasks cmd files

              Thanks Rems! The new script seems to be working. Just curious, what are those registry values doing?

              I did not used the WinZip Command Line Support Add-on, do you?

              You are sure you don't use mapped drive letters in your batch?
              If the problems started on all severs the same time it is somehow related to the RunAs account. (?)
              Do you use a evaluation version of Winzip?
              We are using a full paid version of WinZip 9.0 SR-1 without add-ons. There were no mapped drive letters in the batch.

              The problem started on only "half" of the servers at the same time... the other half is still running fine on the original script using the same runas credentials.

              When I first created the batch, there were one or two issues where the task would stop working for only a single day. I'd run the batch manually, then they started working again via task. Shortly before my original post, half of the servers stopped working all at once and they have failed to run the task since.

              I can't think of any differences that might have caused this. These servers are also pretty much identical to one another (redundant commerce servers). The only thing that I can think of that occurred the day the scripts broke, was an IIS reset.

              Comment


              • #8
                Re: scheduled tasks cmd files

                Originally posted by f21 View Post
                The new script seems to be working. Just curious, what are those registry values doing?
                I have tested the script with the PRO version 12. The registry adds disable a dialogbox that pops-up when the zipping is finished.
                I don't know whether the version you are running (9.0 SR-1) has this dialog box to?

                The dialog box is showing the "Add Complete" message, the user must click a button ro close the window.
                The user has the option to check "don't display this dialog box in the future". But when the job is started by using alternate credentials and not running 'interactive' (like task scheduler tasks) - it waits for user action INVISIBLE for the current user, so the batch hangs and the scheduled job waiting for its time out.

                The second thing I had to change in the batch was removing the %comspec% /c I added before. It starts a command box but then it was not able to expand the full path to the Winzip executable.


                \Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: scheduled tasks cmd files

                  Hrmm maybe it was a dialogue box that was breaking the script. It wasn't until this addition that the script started working again. I don't recall "seeing" a box though on any of the tests. In any case, many thanks! And I even named the batch after you on the live servers haha (rems.bat)

                  EDIT: I found out what the issue was. It was the error dialog boxes that were breaking the script. Under the configuration settings in the winzip program, caution messages were enabled on the servers that were breaking. They were disabled on the servers that were working. Not sure why they broke several weeks in on their own though.
                  Last edited by f21; 21st October 2008, 21:08. Reason: Update

                  Comment


                  • #10
                    Re: scheduled tasks cmd files

                    Originally posted by f21 View Post
                    And I even named the batch after you on the live servers haha (rems.bat)
                    Cool I’m honoured



                    If the pop-up "Add Complete"-summary is not with version 9.0 SR-1, then
                    you can still use your batch ( f21.cmd ) like this,
                    Code:
                    cd /d "c:\program files\winzip"
                    winzip32 -min -a -p "c:\logs\test.zip" "c:\logs\x*.log"
                    cd /d "c:\logs"
                    del /A /F "x*.log"
                    move /Y "*.zip" "\\fileserver\logs"
                    - Added quotes around all paths.
                    - turn slashes used in the paths to slash-backw
                    - Change Directory (CD) using the /d switch, to change drive in addition to change directory.
                    - switches to make the Del and Move commands run silently in all cases

                    \Rems


                    Originally posted by f21 View Post
                    EDIT: I found out what the issue was. It was the error dialog boxes that were breaking the script. Under the configuration settings in the winzip program, caution messages were enabled on the servers that were breaking. They were disabled on the servers that were working. Not sure why they broke several weeks in on their own though.
                    Excellent, glad you got it sorted!
                    Note that this is a per user setting, it should be configured for the 'Runas user' - that is what the registry edits in the script tries to accomplish.
                    Last edited by Rems; 22nd October 2008, 12:27.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment

                    Working...
                    X