batch-filecmdminecraftautoitwindows-task-scheduler

I can't run autoit script through the task scheduler while being logged in. Not as a bat nor as an exe. They do run on their own


I am trying to make a task that runs at 1:00am that runs a autoit script that stops my minecraft server. When I click run through the task scheduler, it does nothing (but I notice the cmd does highlit in yellow on the task bar).

I had already tried compiling it as a exe and also tried to make a bat (The bat just said "cd [location]" and below it had the au3. I also tried making the bat run the exe)

Task scheduler does work on other tasks and bats (I have a bat to start the server after logging in), but for some reason it just can't run the au3 or the exe

Here's the code

    If WinExists("C:\Windows\system32\cmd.exe") Then
WinActivate("C:\Windows\system32\cmd.exe")
Send("sr save")
Send("Say El servidor se cerrara en 10 minutos. Ultimo backup")
Sleep(1000000)
Send("say El servidor cerrara en 1 minuto, ultimo guardado pronto")
Send("{Enter}")
Sleep(30000)
Send("say El servidor cerra en 30 segundos")
Sleep(20000)
Send("say El servidor cierra en 10 segundos.")
Sleep(10000)
Send("stop")
Send("{Enter}")
Sleep(10000)
Send("A")
EndIf

Edit: My bad

The task scheduler is set to open the autoit script (or batch or exe as I tried once) (C:/Users/Username/Downloads/Server/Shutdown server.au3 (or batch or exe)) Clicking the run button won't work, so the time I set is irrelevant (1:00am). I tried running the task scheduler on highest privileges also making task scheduler run it in windows 10 mode.

I may try to lower the amount of ram the server is taking to see if this is a issue with ram overloading. But I doubt it. The computer even runs smoothly if I use microsoft edge while the server is open (The java parameters are set to take 3GB from my computer for the server. My computer is 4GB. I notice that most of the times it takes less than 2GB since it's just a small creative server between three friends). Note: It's a computer I am going to use for nothing other than my minecraft server.

Edit2: Anothe edit, it's not my code. I took it from someone on reddit that was offering people to use it. But I do understand it (I don't know much about coding though)


Solution

  • So firstly how to create a task properly:

    Task scheduler->Create task->

    Name: AutoitServer

    Configure for: < Your OS >

    then set what should trigger it (at your case time 01:00 AM)

    Click Triggers->New Trigger

    Begin the task: On schedule

    Choose: Daily-> Start 7/31/2019 01:00:00 AM

    Finally what should that task do(you must compile that au3 script to EXE)

    Click ACTIONS-> New

    Action: Start a program

    Program/script: < Path to COMPILED autoit >
    example: C:\Users\Bob\Desktop\myAutoitServer.exe

    ...and finally CREATE.

    I have also made some changes to your code:

    $cmdPath= "C:\Windows\system32\cmd.exe"
    
    if(Not WinExists($cmdPath)) Then
       Run($cmdPath)
    Else
          WinActivate($cmdPath)
    EndIf
    
    WinWaitActive("[CLASS:ConsoleWindowClass]", "", 20)
    If Not @error Then
       Send("sr save")
       Send("Say El servidor se cerrara en 10 minutos. Ultimo backup")
       Sleep(1000000)
       Send("say El servidor cerrara en 1 minuto, ultimo guardado pronto")
       Send("{Enter}")
       Sleep(30000)
       Send("say El servidor cerra en 30 segundos")
       Sleep(20000)
       Send("say El servidor cierra en 10 segundos.")
       Sleep(10000)
       Send("stop")
       Send("{Enter}")
       Sleep(10000)
       Send("A")
    EndIf
    

    PS: Why that script didnt work as .au3 ?! I think that task scheduler doesnt recognize this type of scripts even autoit is installed so it is the best to compile your au3 to exe and use that EXE.