Batch file to be run in task sheduler

I have the following batch file which works if I run it manually. But when I schedule it run in task scheduler it does not run. When I manually run the batch file it works fine.

Any idea why? I'm trying to run it on Win 2008 R2

@ECHO OFF
SET HOUR=%time:~0,2%
SET DATE12=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
SET DATE24=%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if "%HOUR:~0,1%" == " " (SET DATE=%DATE12%) else (SET DATE=%DATE24%)
REM xTuple/PostgreSQL Information
SET PRODDB=databasename
SET PGUSER=username
SET PGHOST=server_name
SET PGPORT=5434
SET PGBIN="C:\Program Files\PostgreSQL\9.1\bin"
SET BACKUPEXT=backup
REM formats can be custom/plain/tar
SET FORMAT=custom
SET GLOBALS=globals-%DATE%.sql
SET BACKUPFILENAME=%PRODDB%.%DATE%.%BACKUPEXT%
REM for %%a in (%PGPASS%) do (>"%PGPassFile%" echo %PG_HOST%:%PG_PORT%:%PRODDB%:%PGUSER%:%%~a)
@ECHO Backing up globals to %GLOBALS%...
%pgbin%\pg_dumpall -h 127.0.0.1 -U %PGUSER% -p %PGPORT% -g > %GLOBALS%
@ECHO Backing up %PRODDB% to %BACKUPFILENAME%...
%pgbin%\pg_dump -h 127.0.0.1 -U %PGUSER% -p %PGPORT% --format=%FORMAT% -C %PRODDB% > %BACKUPFILENAME%
REM del "%PGPassFile%"

Open in new window

LVL 1
GerhardpetAsked:
Who is Participating?
 
GerhardpetConnect With a Mentor Author Commented:
I have not also setup the bat file in my Win 7 machine and it does not work either. Same results then on the server and I'm a full local admin on my Windows 7

I will try the process monitor
0
 
ChrisCommented:
what credentials are you running it under

often when running on 2008R2 you need it to run as admin to get round UAC issues
0
 
GerhardpetAuthor Commented:
I'm running as a full admin user
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
ChrisCommented:
what security options are used on the task

should be "run whether user is logged on or not" (if you use this option then you need to have the security option set in the local policy that doesn't block storing them)
"run with the highest privileges"
the configure for option should probably be set to windows 7 or 2008R2.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Does the batch file not run at all (then you should see an error in the task scheduler log), or are the files not created as you expect them? You can e.g. create a file in a particular path to see if the batch is called at all, like
  copy nul c:\batchrun
at the very beginning.

The folder the backup files should be created is not provided, so it depends on where the batch file is called in. Maybe the files are created somewhere else?

Are the users the same for manual and scheduled start of the batch? I have seen issues with passwords etc. when providing a different user (see http://www.experts-exchange.com/Q_27155144.html for details).
0
 
GerhardpetAuthor Commented:
The batch file does not run at all and files are not created. The files are created in the same folder as the batch file which is what it supposed to do and does when I run it manually.

User is the same for both manual and task shedule
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
And the task's History does not show anything? You should see at least attempts to start the batch there.
0
 
GerhardpetAuthor Commented:
Yes the task history does show that the task is successfully finished every time
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Then the action is (a) triggered and (b) executed successful. That leaves the following potential issues:
1) Wrong action. The call of the batch file does not call it at all, but does anything else successfully.
2) The batch is called, but doing nothing. Did you really check that as I recommended?
3) pg_dump does not work. Don't think we are that far yet, as the dump files should be created empty in that case at least.
0
 
GerhardpetAuthor Commented:
if I manually double click the batch file it does work and exactly what it suppose to do.

Yes I have looked for everything as you suggested.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I'm clear about that the batch runs fine on its own. But that is no proof that the scheduled task is configured correctly, or gives any hint what goes wrong here.
When you say "I have looked for everything", does that include creating the "C:\batchrun" file in your batch? And it is still not created? That is something I can not imagine at all. Put the COPY command in the first line - if the task's action is to call the batch file, at least the C:\batchrun file needs to be there. Otherwise you have to check the action (again).
0
 
GerhardpetAuthor Commented:
Now I have tried this which calls the batch file and still manually it works but when I save the following as a batch file on the scheduled task it does not work

"C:\Users\gpadmin\Dropbox\xTuple Tiger Backups\xTuple_Tiger.bat"

I'm by no means an export at batch files as I did not create the batch file in the first place.

Not sure what you mean by creating the "C:\batchrun"

Can you give me more details?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Test 1:
Create a batch file c:\test.bat with content
@echo off
copy nul c:\batchrun

Open in new window

and put this batch file into your scheduled task action. Then trigger the task manually, and check for a file c:\batchrun.
If that works, we are a step further.

Test 2:
Delete c:\batchrun, and move the batch file into C:\Users\gpadmin\Dropbox\xTuple Tiger Backups. Change the task action accordingly, and trigger it manually again. The file c:\batchrun should be created.
0
 
GerhardpetAuthor Commented:
Test1 I get the expected results but then when I move the test.bat file in to C:\Users\gpadmin\Dropbox\xTuple Tiger Backups and manually trigger the task it fails. It does not create the batchrun file.

In the task scheduler for last run results I get: (0x1) but I don't see any errors in the history. The level are all "information"
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Can you show me exactly what you entered as action? You can e.g. call
  schtasks /query /fo list /v /tn YourTasknameHere
and copy paste the result.
0
 
GerhardpetAuthor Commented:
Here is a screenshot
task screenshot
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Try to use
   cmd /c "C:\Users\gpadmin\Dropbox\xTuple Tiger Backups\test.bat"
This will make sure the double quotes will be honoured.
0
 
GerhardpetAuthor Commented:
Here is what I got and it is still not working. When adding the cmd /c I got the following screen to which I clicked yes
task
Now the action looks like this and when manually triggering the task I still get (0x1)
task
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Result 0x1 when calling DOS batches does not necessarily mean an error. Does C:\Batchrun exist?

If not, the path to the batch file is still wrong. Just tested that - if the batch file is not found, result is 0x1, but you do not see anything in the history log.
Since you are certain the path is correct, only a privilege issue remains. Are you gpadmin if you run the batch file manually?
0
 
GerhardpetAuthor Commented:
No the C:\Batchrun does not exist

Yes I'm gpadmin for both manually running the batch and in task manager

I'm a full administrator on the domain and the server itself
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There is some obscure issue with the path used then. You can either:

(1) try to go from C:\ down to the final path, one by one folder, with the batch file, and see at which level the batch isn't executed anymore;

(2) Download Process Monitor from www.sysinternals.com, start it (make sure to press the magnifier glass immediately to stop capturing), set up a filter
  "Process Name", "is", "taskeng.exe", "Include"
  "Process Name", "is", "cmdagent.exe", "Include"
  "Process Name", "is", "cmd.exe", "Include"
  "Path", "containts", "test.bat"
then press the magnifier glass again to start capturing. Then trigger the task calling the test.bat. Stop capturing as soon as you are certain the task has finished. Now you should see a lot of stuff, but in particular we are searching for the test.bat call and/or results: "Name not found" or "Access Denied".
0
 
GerhardpetAuthor Commented:
I was not able to make this work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.