I am trying to create a scheduled task to run a batch file. I know that my batch file runs fine, because I have no problem running it manually. However, when the task calls it, it says that it's running, but it's not. The reason I know that it's not running is because it calls a python script, and the python script sends an email saying that the process has started. And I'm not receiving that email.The python process doesn't take too long (maybe 5 minutes at most), and the task keeps saying that it's "Running" after an hour.
I have the current settings with "Run whether user is logged on or not" (doesn't seem to work at all if I have it as "Run only when user us logged on", because the status never changes from "Ready" even if I tell it to run). I also have the setting with "Run with highest privileges", and just the name of the batch file under "Program/script" and the path to the batch file under "Start in". I also want to note, that I have the user account as "DOMAIN\Administrator".
However, I've tried other ways of calling it. I've tried putting the entire path with the batch file under "Program/script" (G:\GOM3_Update\FeatureServices\copies\test.bat), or putting the path to the python program, and then putting the path to the python script as an argument, but that doesn't seem to work either.
I'm not sure if this issue is caused by some major security settings with windows 10, or something minor in the task scheduler settings.
Here are my current settings:
Full path of the Start in is: "G:\GOM3_Update\FeatureServices\copies\"
The batch file:
"C:\Users\Administrator.DOMAIN\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe" "G:\GOM3_Update\FeatureServices\copies\database.py"
I would include a full path to batch file here:
From my personal experience it is usually the environment problem, aka things like current working directory etc..
Also, make sure to click the refresh button in the right pane of Task Scheduler because it is known for not updating the status of task unless you manually refresh. You may see it change from 'running' to 'ready' sooner if you do that.
The reason why I feel like your screen is not refreshing is because normally Task Scheduler does not allow any scheduled task to execute for longer that 1 hour and you said yours was still saying 'running' after an hour.
What does the exit code and messages say under tasks history?
Exit code 0 means no errors captured by Task Scheduler.
Another idea is to log the start of batch script (inside batch) to a log file before you do anything. And do the same for python file. This will help you narrow down the problem.
I struggled with this one also a lot, changing the User Account
AND Group. User Account alone was not enough for me.
This helped for me:
And then clicking on the Locations...you'll have to choose "WINSTADM"
Related
I have a SAS code which works flawlessly when I first open the program and run the code interactively. However, if I try to run it as a batch file or execute it from the command prompt using the -sysin option, it doesn't run and I don't know why.
Part of the reason might be that when I run SAS, I receive this dialog box which I need to manually close:
Screencap 1
When I try to choose the option "Do not show this dialog box again" it doesn't work, and it shows the dialog every time I open SAS. This may be because my registry and profile files are corrupted:
Screencap2
Would this be the reason why? If I run the program interactively after closing the dialog box, the program run just fine. I tried to follow some steps to repair the registry and profile files, but it didn't work. Is that what I should focus on, or is there likely another reason it won't run in batch?
Thanks for your input!
edit: I was able to get the program to run from the batch file, but I get the following errors about permissions in the log file:
ERROR: Insufficient authorization to access //destinationonselecteddrive
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
I do not have these issues with insufficient authorization when I run the program interactively, only in batch mode. Could it be a metadata issue?
You have to check with which profile your batch is running. When run interactively it may (and it will normally) use a different profile than the one you are using while running interactively. That means different user and password. You can check in the management console (or ask your administrator to check).
It sounds most likely like there is a problem with the user profile. At my old job I ran into problem with the profile when I ran concurrent SAS sessions. I had to set my user profile to read only using the RSASUSER option (SAS instruction here). If that is not the issue try creating a new profile catalog in a new location and then adding the -SASUSER option with the new location (SAS info on how to do that).
My EXE is executing perfectly fine when I am executing it by double click on it, but it is not executing when I am trying to run it via Schedule Task.
I am running schedule task on a local machine as administrator. I have already set the following settings into the "Security Options" of the Schedule Task.
Run only when user is logged in (I am logged in when schedule task is running)
Run with highest privileges check box is checked
In my case, it didn't worked because of the start in location of the program.
Set the [Start in] (optional) properties of the scheduled task with the path where the exe file is exist.
The default [Start in] value is C:/Windows/System32
Depending on which Windows OS you're running this on, your EXE may have in fact started and is running in the background, with the user interface completely hidden. Depending on the EXE you are trying to run, it may be sitting there, hidden, waiting for user input that it will never get. If your EXE doesn't require any user input (something that just runs and then closes when it completes), then you might just check to see if the job is actually done.
A trick I have used to verify this is to create a small batch program like this:
#echo off
echo myEXE Scheduled Task Started %DATE% %TIME% >>c:\myEXE.log
myEXE.exe
echo myEXE Scheduled Task Completed %DATE% %TIME% >>c:\myEXE.log
Have your scheduled task call this batch script instead of myEXE directly. This will generate a text file (myEXE.log) that you can check to verify when the scheduled task kicked off, and then when (and whether) the EXE finished.
Just for kicks (and to test what I'm talking about) you can add these lines at the end of the batch script.
pause
echo Batch Script Finished %DATE% %TIME% >>c:\myEXE.log
If you never see the cmd window waiting for you to Press any key to continue... then you'll also never see the last line in your log file (myEXE.log)
Windows Task Scheduler is a strange beast of a program. It's not really a CRON like task scheduler and it's not a Quartz based program, other than relying on internal clock system.datetime, which has been known to have "issues" of its own.
Nevertheless, it can sometimes trip over itself (unproven, but from personal observations), when it comes to permissions of a task and who created it vs what account is used to run it.
I found the following steps gives me a "clean" task schedule every time, and the task runs every time:
Always run Task Scheduler as Administrator. If you don't have admin rights to do this, then you should even be here!
Don't create a Basic Task. Go straight to Create Task, and under your own admin account (doesn't have to be God Admin!).
When filling in the task wizard, don't provide a trigger UNTIL you've tested the task first. Also, make sure you've allowed the task to run whether you're logged in or not! That catches me sometimes.
Don't worry about Settings, for now. Accept the default
Save/OK
Close Task Scheduler
Restart it again, and again as Admin
Run the task you've created.
If all goes well, it ran! Do a CMD run of the EXE using #Wes's suggestion to be sure.
Now, place a Trigger of your choice
Change the Account to your proper task admin account, or a generic account with admin rights specifically created to run tasks. We call ours admin.tasks
Save everything and you should be ok from here.
I have a batch file which is set to run as a Scheduled Task on Windows Server 2012.
When I run the batch file by hand from the command line, it works. When I right-click on my task in Task Scheduler and manually run it, it still works fine.
But, if I let the task run according to the schedule set for it... then it seems to work sometimes, but not others.
I have already set the task to run as a given user; I have set its 'Start in' directory correctly; I have tried giving it highest privileges. None of that helps.
The basic answer is that the batch file for the task is running, and that the last step of the task is returning 0x0. If the task is apparently 'not doing anything', it is because some earlier step of the task is failing silently
Why? In my case, and I think this could easily effect other people, the answer is that the batch file for a scheduled task sees different environment variables depending on whether the user it runs as is currently logged in or not.
More Detail:
In particular, if the task is set to run as Administrator, then while Administrator is logged in the task sees one set of variables (whether it is run manually or on the schedule), but when Administrator is not logged in, it sees a different set of variables.
This can be very hard to debug - basically, you need to put in a lot of logging!
When you are running a batch file as a Scheduled Task on Windows Server 2012, it only sees shared environment variables. It does not see the user specific environment variables for the user you have set it to run as, unless the user in question is currently logged in.
You can see the problem in action by putting SET > test.txt into a batch file on its own, and running it as a task in different circumstances (manually; on a schedule when logged in; on a schedule when not logged in).
UPDATE:
From more detailed testing, it seems that when the user which the task is set to run as is not logged in, the variables USERDNSDOMAIN,
USERDOMAIN and USERNAME do get set correctly for that user anyway. The variable USERPROFILE gets set incorrectly to the value for the Windows default user (i.e. C:\Users\Default). Everything else gets set incorrectly to the set of shared environment variables only (note that this is obviously not the correct set for the specified user, and is also not even the correct set for the Windows default user, which should get its user specific environment variables from HKEY_USERS\.Default\Environment).
Note:
This is not the same issue as windows 7 task scheduler doesn't use updated path , and in fact changes to any shared environment variables, including PATH, do get seen straight away (on the tests I did, on Windows 2012 R2), with no restart of any process.
For those who don't know what SCCM is, a little information, to understand better what i want to know. SCCM is an application, with you can deploy software packages. It is also possible to create a so called "Task Sequence". A Task sequence can contain multiple packages, which will be installed one after the other.
The Task Sequence execution occurs in Session 0. Of course the packages query some processes, if they are running. If they are, a window will pop up, to ask the user to close the application.
Here comes the problem. If an administrator deploys packages using task sequences (and they do), the users won't see the window, and won't close the required process. If the process is not closed, the script execution aborts.
I found this Link, and created a simple exe, according to the description. This simple exe is able to start a process from session 0 in session 1(or above), where the user is logged on(i know the security risks). So far so good, but how do i get the packages to display their windows? Obviously i could change the command line, so my exe will start the installation of every package, but this is not an option. There would be to much work.
The ideal solution would be if my exe would be the first in the task sequence, it would do "something" so the windows could be visible.
And that's where i am stuck.
Does anyone has any idea how i could achieve what i want?
Thanks in advance!
OS: Windows Server 2008 R2 Enterprise
I am unable to get the Task Scheduler to run .bat files while I am logged off. I have a a production .bat file I want to use, but for my troubleshooting purposes I am using C:\Users\Administrator\Desktop\test.bat
test.bat is very simple. All it contains is:
taskkill /im notepad.exe
It runs successfully on its own. If I have Notepad open and I double-click test.bat then the cmd window flashes and Notepad dies. The .bat file is not the problem.
The task I created in Task Scheduler is "Test taskkill" (without the quotes). I can see its location in Windows Explorer: C:\Windows\System32\Tasks\Test taskkill
If the task is set to run only when the user is logged on then the task works. With this setting, I can right-click on it > run and I get the same behavior as if I double-click the .bat file. That means the task is configured correctly.
If I set it to run whether or not the user is logged on then when I right-click > run nothing obvious happens. The job is reported as having been run successfully in the history, but Notepad survives. I know that I am using the proper credentials for the account that I am configuring to run the task. That account is the local administrator.
This issue appears similar to issues other have had in the past:
Running a .bat file in Scheduled Task
http://social.technet.microsoft.com/Forums/windowsserver/en-US/d47d116e-10b9-44f0-9a30-7406c86c2fbe/scheduled-task-wont-run-bat-file?forum=winservermanager
The thread in the second link seemed to resolve an identical issue for many people with the suggestion that the account used to run the task requires explicit permission to the .bat file and all files that the .bat file modifies. This was very promising (if an annoying requirement). However, I have confirmed or assigned explicit permissions for the local administrator to the following areas, and I am still not successful:
C:\Users\Administrator\Desktop\
C:\Users\Administrator\Desktop\test.bat
C:\Windows\System32\Tasks\
C:\Windows\System32\Tasks\Test taskkill
Additionally, I have confirmed that the local administrator account is in the local administrators group.
Am I missing some other permissions that needs to be set? Is there something else I should be looking at? Thanks!
I have resolved this problem with help from a contractor who was doing some other work for my institution.
In the properties window for the task, on the Action tab, when creating or editing the action that opens the batch file (in my case test.bat), there is the "Program/script:" field and there is also a "Start in (optional):" field. I previously had the "Program/script:" field containing the full path to the file, ending with the file name, and I had the "Start in (optional):" field null. This configuration appears to work without issue when the task is set to "Run only when user is logged on" in the General tab of the task's properties window. However, this configuration does not work when the task is set to "Run whether user is logged on or not".
In order to resolve the problem, I changed the action so that the "Program/script:" field contained only the file name, not the file path. I put the file path in the "Start in (optional):" field. This configuration works when the task is set to "Run only when user is logged on"! I have tested with the task running on a trigger and also with running it manually.
Not sure if this helps. My issue was using a interactive program (in particular Excel) as well.
Creating the 2 empty folders (Desktop) solved it. Create both even if you are running on x64.
C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop
I debugged my code and realised the code exited at this line
ExcelFile excelFile = new ExcelFile(directory, filename);
I read this solution somewhere and I cannot find the URL now.
Try this as well..
Resolved the "Run whether user is logged on or not" by setting the “Log on as Batch Job” security policy.
Here is details on the setting… https://danblee.com/log-on-as-batch-job-rights-for-task-scheduler/
You may take another approach, but here is the steps…
1) I updated the default domain group policy by adding my admin account (that I fire the task with) to the “Log on as Batch Job” security policy. (Even though ADMIN group is listed, and I am using an admin account)
2) I forced GPUPDATE on the client
3) Rebooted the client
4) Looked at the policy on the client to make sure it made it over from the DC
5) Set the task "Run whether user is logged on or not"
6) Ran the task on demand and it worked creating a PDF file.
7) Logged out and ran the task at scheduled time and it worked creating a PDF file.
I had similar problem, but it was related to the fact that there was a space in the path of my executable batch file. I have removed the space and now it works fine.
What work for me was make sure that the path is available.
i used net use Z: \computername\folder /persistent:yes
then the xxcopy /s /c /d /e /i /y D:*.* z:\BackupBat
I notice all my other .bat work fine only the one with the reference to a map folder was the one no running, so that need to be the problem. with these change the bat file runs good.
Verifying the following has worked for me:
1) 'Run if user is not logged in' option in Task Scheduler does not work for programs that is interactive mode. Example notepad, clock. Tasks will not run interactively. Make sure your program does not requires any interactive actions.
2)Check the “Start in (optional) “ If your program path is not one of the system paths. You have better to add you program path in it. On the "Program/Script" enter only the name of the program, and enter the path of your script in "Start in".
3)The user account you setup for your program must have permission not only to run your program itself and but also to run all other program that related to your program.
Here is a sample program:
On C:\myprogram\folderOne\test.cmd
date /t >> c:\Temp\testAuto.log
date /t >> c:\temp\testAuto.log
echo ----->> c:\temp\testAuto.log
cls
exit
-Create a Tasks, selecting on General>SecurityOptions "Run whether user is logged on or not"
-Enter on Action>Program/script: test.cmd
on "Start in" C:\myprogram\folderOne
-Run, you might see the cmd windows open and closes very quickly. The testAuto.log file should be in c:\tmp folder.
It might be because you don't have permission. I was facing the same problem and i found the solution like this -
Open Task Scheduler right click on your task and than select properties option.
In properties click on General tab and then click on 'User Group or User' and select the appropriate user.