-
Notifications
You must be signed in to change notification settings - Fork 0
How to schedule your bot with Task Scheduler
Windows has a little known feature called Task Scheduler for... scheduling tasks in Windows! If that sounds ridiculous to you, consider the following scenario: You have a piece of software, and you want to set it up to run on startup, without your intervention. Task Scheduler is the answer, and that's exactly what we're doing! So, follow these steps and get to scheduling the bot to start up with your machine.
-
Open Task Scheduler by searching for it in the Start Menu.
-
When it opens, click "Create Task..." in the right-hand pane. A window will appear asking for some information.
-
In the window that pops up, fill out the Name and Description fields and make sure "Run only when user is logged on" is selected.
-
Next, click "Triggers", select "New..." and select "At log on" under "Begin the task:" in the window that appears [1]. Select "Specific user:" and make sure your username is present (in modern Windows 11 machines connected to a Microsoft account, this is usually the first 5 letters of your Microsoft email address) [2]. Finally, select "Stop task if it runs longer than: 1 hour" [3]. Click OK to move back to the Create Task window.
-
Click "Actions", then "New...". Make sure the Action is to "Start a program" [1], then fill out the window as follows:
- Program/Script: invis.vbs (NOTE: This will make the console window that normally shows up disappear, you can alter this by simply putting main.bat here if you WANT the console window to always appear. However, do also note that this will make the program terminate completely if you close said window, so it's recommended to use invis.vbs to hide it) [2].
- Add arguments (optional): main.bat [3].
- Start in (optional): The path to where you saved the code for this repository (aka the folder that houses main.bat and everything else) [4]. Click OK to save your changes.
-
Click "Conditions", then check "Start only if the following network connection is available: Any connection" [1]. This makes sure that you have internet access before trying to start the bot, since it is required to send the Discord webhook message.
-
Then, click "Settings", and check "Run task as soon as possible after a scheduled start is missed" [1], and "If the task fails, restart every: 5 minutes; Attempt to restart up to: 3 times" [2]. Click OK and your new task will appear in the main window!
-
Finally, in the right pane of the main window, run, wait 15-20 seconds, then end, the task. Make sure to wait a little bit of time to end the task so that it has enough time to spawn and get going before you end it. We can safely end the task without any trouble because we use
invis.vbs
to spawn the task initially, so the process that "runs" the task is already dead and gone, and it's safely running away from the task.
And you're done! Congratulations, the task will now run autonomously without your intervention at startup.