DEV Community

Cover image for Using Windows Task Scheduler to Automate NodeJS Scripts
Joshua Tzucker
Joshua Tzucker

Posted on • Originally published at joshuatz.com

Using Windows Task Scheduler to Automate NodeJS Scripts

This is a post about using Windows Task Scheduler to automate the execution of NodeJS scripts, and other NPM / Yarn based tasks. If you don't use Windows, this post is probably not for you (but feel free to read anyways 🤷‍♀️)

Why? 🤔

Tons of reasons! Maybe you are trying to emulate a production environment that has NodeJS scripts as scheduled CRON tasks. Or, for your own productivity or fun you want to script things to happen based on Window events.

For example, you could write a NodeJS script that talks to your project tracker of choice via API and stops any running timers when you lock your computer to take a break.

Why not just use crontab under WSL?

Good question! If you have WSL (Windows Subsystem for Linux) installed, and you only want to trigger actions based on time, then you should totally give crontab under WSL a shot!

Although there used to be issues with it (in past versions, WSL used to kill background tasks when you closed the console), I just gave it a shot, and had success. If there is interest, I might make a separate post about how to setup crontab under WSL.

However, Task Scheduler still has value as a separate tool, since more than just time can be used as a trigger; you can execute tasks based on computer unlocks, power events, and more. You can't do that with crontab.

How? 🤓

Steps:

  1. Find where the binary / application you need to run is stored
    • You can use where npm or where yarn from the command line to find the path
      • Example: My yarn path is C:\Program Files (x86)\Yarn\bin\yarn.cmd
  2. Open Task Scheduler (search in programs, or WIN+R, taskschd.msc)
  3. Start the task creation process by clicking on "Create Basic Task" or "Create Task" in the sidebar
    • Windows Task Scheduler - Create Task Buttons
  4. Pick a trigger
    • "On a Schedule" (like CRON)
    • "At log on"
    • Etc.
  5. Add your Action: Action -> Start a Program
    • "Program/script":
      • Here is where you plug in the path to the application you found in step 1
    • "Add arguments" - You should put whatever you would put after npm or yarn normally.
      • If normally execute npm run myScheduledTask, you would want arguments to be run myScheduledTask
    • If you are calling a scripts entry in a package.json file, you need to tell scheduler to run this where your package.json file is located.
      • If using Yarn, you can pass the working directory through args, with cwd.
      • Otherwise, use the start in (optional) field to specify the directory

Setting

If you are concerned about keeping track of the results of what ran, you can also capture the result of anything spit out to the console by using >> task_log.txt or something like that.

👩‍🍳 - You can combine actions and triggers

One nice feature of task scheduler that I did not notice immediately is that it does not have to be a 1:1 mapping of task-trigger-action.

For example, you can group ten different actions under a single task with a shared trigger.

✨ - You can use Git Bash for more advanced scripting

Instead of targeting NPM, Yarn, or Windows CMD, if you have Git Bash (comes packaged with Git for Windows), you can it as the target "Program/Script", and then execute a more advanced command that uses some bash tools. For example, a sample task that performs some backups for a project might look like:

  • "Program/Script": C:\Program Files\Git\git-bash.exe
  • "Add Arguments": cd C:/projects/my-proj && node prep-dirs.js && npm run backup >> backup_log.txt

💥 - %1 is not a valid Win32 application

If you see this error, you have probably selected the wrong application as the Program/Script to execute. For example, using /yarn instead of yarn.cmd will result in this error.

⚙ - Stop the Cmd window from popping up

If the black windows Command prompt window keeps popping up whenever your task runs, you need to change one of the basic settings:

  • Change security options to: Run whether user is logged on or not
    • 🔐You will probably also want to check Do not store password

There is no harm in having it show up; but it might get annoying if your task is scheduled to run frequently.

⏰ - How to schedule more frequently than every 5 minutes

You might have already noticed that the smallest interval that shows up in the repeat task every duration picker, under trigger settings, is 5 minutes. Uh-oh!

Actually, this is an easy fix - you can actually type a custom interval in that box! So if you wanted an entry that was equivalent to a CRON of * * * * * (every minute), just type in the box 1 minute and set for a duration of to Indefinitely.

Here is what that looks like:

Windows Task Scheduler - Creating a time based entry that repeats every minute, like CRON of * * * * *

Comparison with CRON

Since this is likely to come up in the comments (I can already hear the annoying response; ''why don't you use a real operating system? lol!") - yes, Task Scheduler is not a perfect replacement for CRON on windows. But it is not really meant to be, and this post is not advocating as such either.

Plus, you can use crontab under WSL now (see my note under "why?").

Wrap Up

I hope this was helpful! This is a bit different from what I normally write about, but felt compelled to publish it as I had trouble finding existing resources on the topic.

Success!

Top comments (0)