Strange behavior when running an MS-DOS Batch file as a scheduled task
April 4, 2008 9:30 AM   Subscribe

How can I fix an MS-DOS batch script that works when run manually, but exhibits strange behavior when run as a scheduled task?

Introduction:

We have a batch file that backs up a directory from a server to a network share using robocopy in an MS-DOS batch file. When I manually run the batch file, it works fine. When I run it as a scheduled task, it runs to completion with an exit code of 0 but puts the folder in the wrong place. In the file, I break up the date string
from date /t into three variables (dropping the alpha day of the week), then use those variables to form the path where the folder is to be stored (shown below):

REM Get system date to use for directory and put in 3 environment variables

for /f "tokens=1-2 delims= " %%a in ('date /t') do (
set mdate=%%b)
for /f "tokens=1-3 delims=/ " %%a in ("%mdate%") do (
set mm=%%a
set dd=%%b
set yyyy=%%c)

REM Error checking in NET USE: errorlevel=2 [CAN'T LOGON]

net use x: \\SHARE\FOLDER /USER:DOMAIN\USER PASSWORD ***redacted for obvious reasons***
If errorlevel 2 Goto END

REM COPIES the content of the BACKUP DIR to the share

C:\BACKUP_SCRIPTS\robocopy d:\FOLDER\Backup X:\FOLDER\%yyyy%%mm%%dd%\Database /E /NP /Z /LOG+:X:\FOLDER\%yyyy%%mm%%dd%\logs\Database.log ***also redacted***


Troubleshooting So Far:

I experimented with the original file and test scripts based on the file running as a scheduled task and manually. Running manually works OK. When run as a scheduled task, the result of date /t isn't getting passed to the variables. When robocopy starts to copy the files it sees X:\FOLDER\\Database, so it copies the folder to the next level up from where I want it to go.

Background:

OS: Windows server 2003 SP1
CPU: A fairly fast Dell rack mounted server that I don't have physical access to (I use terminal service)
The CPU is used in production
I am an administrator on the CPU
The scheduled task is run under a different account that has admin privileges
The sceduled task log shows successful completion

Caveats:

Using an MS-DOS Batch file is the only option open to me for this task. I hate it, but other options like perl, python or COTS backup software are not allowed on this machine.

If you do know the answer, but it is cryptic, please try to explain it so that I can get it through code review.

If anyone knows how to fix this, I would really appeciate it. Thank you.
posted by SteveTheRed to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
I see that you are running the task as an admin equiv. Have you tested using the same exact account?

Here's how I would troubleshoot it. I would put a PAUSE in the batch file in strategic spots. Then I would login to the server console (not RDP, unless you are using the /CONSOLE switch) and make sure the scheduler service is allowed to interact with the desktop. I would then set the job to run immediately and watch the console. A command prompt should appear and you should see the script run. You could also next the batch file inside onther batch file (have one call the other) and pipe the output to a text file.

Does the account you are running the a script as have "run as a service" and "run as a part of the os" rights?

Hope this makes sense as I am short on time.
posted by internal at 9:47 AM on April 4, 2008


I can't think of what is causing the problem, but

X:\folder\\database should be the same as x:\folder\database unless robocopy does it's own special, wrong, path parsing. This is basically so you can append together paths without having to worry about this.

Something else is most likely the problem.
posted by aubilenon at 9:47 AM on April 4, 2008


Best answer: I don't know if this is it or not, but I've been in the same situation. It turned out that the date was returned in one format when I was logged in (e.g., 1980-01-01) but in another when run by the AT scheduler (e.g., 01/01/1980). Try putting these lines in your batch file at the beginning:
ECHO %DATE%
PAUSE
This will display the date format it is expecting (press ^C to at the pause). Then type ECHO %DATE% at a command prompt to see what format you are getting when you run it interactively, and compare the two.
posted by davcoo at 10:58 AM on April 4, 2008


(I think in a batch file you have to use two percent signs, so it should be ECHO %%DATE%%)
posted by davcoo at 11:10 AM on April 4, 2008


Response by poster: davcoo:

You led me in the right direction. Regional settings were right for my personal account, where I wrote the script, but they were wrong for the admin account that runs the scheduled task. I fixed it up in regional settings and all is good. Thanks everybody!
posted by SteveTheRed at 11:14 AM on April 4, 2008


« Older I'm experiencing auditory hallucinations when...   |   Misbehaving Ubuntu box, normally well behaved and... Newer »
This thread is closed to new comments.