"at" scheduled job not working

Discussion in 'Scripting' started by John A Grandy, Aug 21, 2008.

  1. Windows Server 2003 R2 SP2

    I created a scheduled job with that "at" command that runs every morning at
    2:00 AM :

    "cmd /c C:\MyFolder\MyBatFile.bat"

    In Scheduled Tasks , I see this job listed and "Next Run Time" and "Last Run
    Time" are always correct ( every morning , they have advanced 1 day ).
    "Last Result" is always 0x0

    However, nothing is accomplished by the job.

    When I run MyBatFile.bat manually it works fine.

    Could there be security issus ? How to diagnose why the .bat file doesn't
    work when run as a scheduled job ?

    What other options are available for scheduling this .bat file ?
     
    John A Grandy, Aug 21, 2008
    #1
    1. Advertisements

  2. A couple of comments:
    - You don't need to invoke a separate command processor.
    Launching MyBatFile.bat will suffice.
    - Make the first line in your batch file like so:
    echo %date% %time% >> c:\test.txt

    Now run the job again under at.exe and report the contents of
    c:\test.txt. Next, post the full contents of MyBatFile.bat.
     
    Pegasus \(MVP\), Aug 21, 2008
    #2
    1. Advertisements

  3. Ok, I re-configured the scheduled job, losing the "cmd /c" , and adding the
    logging of the timestamp to the .bat file :

    at 17:07 /every:M,T,W,Th,F,S,Su "C:\svn_repository\svn-backup.bat"

    The log file was created and contains : Thu 08/21/2008 17:07:00.05

    So ... at least the first line of the .bat file was run.

    However, none of the other lines were run.

    Here is the contents of the .bat file :

    echo %date% %time% >> c:\svn_repository\svn-backup.log

    c:\python25\python.exe c:\svn_repository\hot-backup.py --archive-type=zip
    c:\svn_repository\myrepository c:\svn-backup



    Thu 08/21/2008 17:07:00.05
     
    John A Grandy, Aug 22, 2008
    #3
  4. I suspected all along that the scheduled job ran, in spite of
    you thinking that it did not. You now claim that only the first
    line runs. You have to change your way of thinking: In a batch
    file ALL lines run until you hit an error. You must add further
    logging code in order do see what's going on, e.g. like so:

    @echo off
    set Log=c:\svn_repository\svn-backup.log
    echo %date% %time% >> %Log%
    echo User=%UserName% %path% >> %Log%
    c:\python25\python.exe
    c:\svn_repository\hot-backup.py
    --archive-type=zip c:\svn_repository\myrepository
    c:\svn-backup 1>>%Log% 2>>&1

    You will probably see that the account you use has no access
    rights to one of the resources in your command. This raises the
    question: Why do you use at.exe to schedule the job? Such jobs
    will run under the System account. Why not use the inbuilt Task
    Scheduler? It lets you specify the account to use!
     
    Pegasus \(MVP\), Aug 22, 2008
    #4
  5. John A Grandy

    Al Dunbar Guest

    While debugging these kinds of problems I'd suggest not turning echo off.
    Also, it can sometimes be simpler to turn the whole batch into a single
    compound command and redirect its output once, i.e.:


    @echo off
    setlocal enabledelayedexpansion
    set Log=c:\svn_repository\svn-backup.log

    ECHO ON
    (
    echo !date! !time!
    echo User=!UserName!
    echo Path=!path!
    c:\python25\python.exe
    c:\svn_repository\hot-backup.py --archive-type=zip
    echo/errorlevel = !errorlevel!
    c:\svn_repository\myrepository c:\svn-backup
    echo/errorlevel = !errorlevel!
    ) 1>>%Log% 2>>&1

    The setlocal command and using "!" instead of "%" is required to avoid
    problems discussed at length elsethread. You might also consider explicitly
    setting the default directory, as this may not be the same when the batch is
    run as a scheduled task. To set it to where the batch file is located:

    pushd "%~dp0"

    If you want the batch file and its log file to be in the same location and
    with the same base name, I'd suggest this:

    set Log="%~dpn0.log"

    Finally, is this: "c:\svn_repository\myrepository" a batch file or an
    executable? I generally recommend specifying the file type as .exe, .cmd, or
    ..bat to avoid any ambiguity.

    /Al
     
    Al Dunbar, Aug 22, 2008
    #5
  6. Hi John,
    at is outdated and should no longer be used.
    As mentioned, it will run in a user context which has not the necessary
    rights to access the ressources needed.
    Replacement for the AT command is SCHTASKS.exe.
    Best greetings from Germany
    Olaf
     
    Olaf Engelke [MVP], Aug 24, 2008
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.