Solved

Batch file to be run in task sheduler

Posted on 2013-01-12
22
1,051 Views
Last Modified: 2013-03-04
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

0
Comment
Question by:Gerhardpet
  • 11
  • 9
  • 2
22 Comments
 
LVL 18

Expert Comment

by:irweazelwallis
ID: 38769830
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38769836
I'm running as a full admin user
0
 
LVL 18

Expert Comment

by:irweazelwallis
ID: 38769845
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38769959
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38771494
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38771765
And the task's History does not show anything? You should see at least attempts to start the batch there.
0
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38771951
Yes the task history does show that the task is successfully finished every time
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38771972
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38772484
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38772743
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38774320
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 68

Expert Comment

by:Qlemo
ID: 38774363
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38774461
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38774562
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38775014
Here is a screenshot
task screenshot
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38775156
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38775192
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38775277
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
 
LVL 1

Author Comment

by:Gerhardpet
ID: 38775304
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 38776367
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
 
LVL 1

Accepted Solution

by:
Gerhardpet earned 0 total points
ID: 38810510
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
 
LVL 1

Author Closing Comment

by:Gerhardpet
ID: 38949133
I was not able to make this work.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now