Task Scheduler

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
parisi
Posts: 10
Joined: 15 Jul 2022, 15:08

Task Scheduler

Post by parisi » 10 Aug 2022, 11:41

I know this is an off discussed topic but...

I have a six scripts that I want to run about every hour. I have one Task in Task Scheduler running all six of my scripts... as I explain this here, I am realizing that they may all be attempting to run at the same time. Is that a problem. I am going to go down the path of splitting them out into separate Tasks.

Second, would it be better to have one script to rule them all thought about putting a schedule in the script itself. Maybe using the SetTimer? Would I need to create one script with all of my script code or just include them by reference (if that is possible).

Then, how do I kick off the main script? Should I use Task Scheduler to do that?

Or - if you have any advice, it would be very helpful.

Thanks,
Paul.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Task Scheduler

Post by mikeyww » 10 Aug 2022, 19:22

Windows Task Scheduler can be convenient for starting a script at a specific time.

Scripts can often be combined into one, depending on what they do.

Running multiple different scripts at the same time may or may not be a problem, depending on what they do. If you are always running the scripts concurrently, and if the scripts can be combined without any loss of function, then that would seem like a logical thing to do, and it can sometimes avoid problems with software conflicts.

parisi
Posts: 10
Joined: 15 Jul 2022, 15:08

Re: Task Scheduler

Post by parisi » 11 Aug 2022, 10:06

Thanks.

I broke each of my scripts into their own Task. Each of the five is slated to run 1 min after the previous. They don't seem to be running. If I manually invoke the script by right clicking and choosing Run Script, it runs perfectly.

Is there a way to write to log file from a script?

Here is the XML of the Task. Can you see any issues that might be preventing this from running?

Code: Select all

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2022-07-27T13:49:27.4789632</Date>
    <Author>DASH220220\User</Author>
    <URI>\AutoHotKey Every Hour</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT10M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2022-07-27T13:43:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-1003809905-315846614-1947300054-1001</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Program Files\AutoHotkey\AutoHotkey.exe"</Command>
      <Arguments>C:\Tools\BrowserAutomation\aDattoRMMLogIn.ahk</Arguments>
    </Exec>
  </Actions>
</Task>
Any thoughts would be very much appreciated!
Thanks,
Paul.
Last edited by parisi on 11 Aug 2022, 13:16, edited 1 time in total.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Task Scheduler

Post by BoBo » 11 Aug 2022, 10:28

Is your screen locked while these tasks are executed (probably "out of office"-time)?
Do your separate tasks need admin privileges, and if yes, are those incorporated into your scripts?

parisi
Posts: 10
Joined: 15 Jul 2022, 15:08

Re: Task Scheduler

Post by parisi » 11 Aug 2022, 13:15

The computer is not locked...
I presume that the scripts, when manually run, are run as the current user.
The Tasks are NOT set to run as Admin... I think they use the current user?

I just discovered that if I manually "run" the Task from within the Task Scheduler it works just fine... it seems like the scheduled ongoing tasks do not run.

Oh the humanity!

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Task Scheduler

Post by mikeyww » 11 Aug 2022, 20:03

Instead of working with six different scripts, test with a single script that contains a single line: a MsgBox display. After you have that working, you can expand. Ensure that your target program is the full path to AutoHotkey.exe, and your argument is the full path to your script. Getting the user account right is critical; you may need to enter user & password there, too. Both the task and its triggers must be enabled. The scheduler's display window shows you the last run time as well as the result. Check the "Conditions" tab carefully.

parisi
Posts: 10
Joined: 15 Jul 2022, 15:08

Re: Task Scheduler

Post by parisi » 23 Aug 2022, 08:26

I think I may be on to something....

I enabled History on the task and I get the following:

Task Scheduler did not launch task "\AutotaskRefreshStart" because instance "{2c889f05-ea59-4187-89aa-6376576158a5}" of the same task is already running.

Here is my script.
  • Am I not exiting it properly?
  • Or is there something that it might be getting stuck on?
  • The script runs fine when invoked by right click.

Code: Select all

#SingleInstance Force
#include C:\Program Files\AutoHotkey\UIAutomation-main\Lib\UIA_Interface.ahk
UIA := UIA_Interface()
windowName := "Autotask - Company - Session Expired"
WinActivate, %windowName%
WinWaitActive, %windowName%
browser := UIA.ElementFromHandle(WinExist(windowName))
browser.FindFirstByName("Login again").Click()
sleep, 1000
browser.FindFirstByName("Continue").Click()
sleep, 1000
;browser.WaitElementExistByNameAndType("Password", "Edit").SetValue("password")
browser.WaitElementExistByName("Login").Click()
Return
;https://auth.datto.com/[email protected]
Any thoughts would be very much appreciated!

Thanks,
Paul.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Task Scheduler

Post by mikeyww » 24 Aug 2022, 10:08

I cannot debug your script, but I can offer two ideas. They may or may not be helpful.
1. WinWaitActive without a timeout will wait infinitely if the WinTitle does not become active. You can easily check that condition and also change the script to include a timeout.
2. When I want a script to exit, I usually use ExitApp. I would recommend it.

It's easy to see if your script remains running: check Windows Task Manager for AutoHotkey.exe (if uncompiled), or look for the tray icon. Task Manager would always show the listing.

If your task remains running but your script has exited-- not usually the case-- it might mean that you have set up your scheduled task to do additional things. Best of luck.

parisi
Posts: 10
Joined: 15 Jul 2022, 15:08

Re: Task Scheduler

Post by parisi » 24 Aug 2022, 12:21

Thank you - very helpful...

I did add the timeout and an if block and an ExitApp... which is all working well.

I have it running via a scheduled task every ten minutes. However, the "task" is always running... it never seems to exit. Perhaps this means that the task keeps running but only triggers the program every ten minutes...

Thoughts?

Paul.

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Task Scheduler

Post by mikeyww » 26 Aug 2022, 07:42

I'm not sure, but you can test it easily by making a new script that displays a MsgBox followed by ExitApp. You can run that every 10 minutes and see if the task stops. From what I have seen, when the program specified in Windows Task Manager exits, the task ends. I have several scheduled tasks that run AHK scripts.

Persistent scripts are usually easy to identify, but here is the information about them.

https://www.autohotkey.com/docs/commands/_Persistent.htm

https://www.autohotkey.com/docs/commands/_InstallKeybdHook.htm

https://www.autohotkey.com/docs/commands/_InstallMouseHook.htm

Post Reply

Return to “Ask for Help (v1)”