Run a Node script with Windows task scheduler30 Oct 2016
In this tutorial, we are going to learn how to run a node script with windows task scheduler.
To run a node script with windows task scheduler we need:
- A machine running windows
- A script file to run
- Node installed
- Git bash installed
This can be any scripts that can run in node. We could have a script that takes a backup of a database and uploads it to dropbox, or a script that moves old log files to a Logs folder.
In this tutorial we’ll use an example script - script.js. script.js prints ‘Bedtime!’ to the console:
scripts.js is saved in C:\Users\Edd\Scripts (more on that later).
If it’s not installed already, download it and install.
To check node installed successfully enter node -v into git bash, or another shell. If the command is not found, node did not install successfully.
We’re going to use git bash shell to run the command that runs our script. (Because who wants to work with CMD.EXE?)
If you don’t have git bash installed already, you can download it here.
Got it installed? Great. Now we need to open up Windows Task Scheduler.
Windows task scheduler
Windows task scheduler lets you run tasks automatically. You can set commands to run in response to events, like starting up windows or logging in as an admin. You can also schedule them to run at regular intervals.
To set up a task to run in Task Scheduler, we first need to open start task scheduler. For Windows 8+ search task scheduler in the start menu search bar and open it from the results.
When Task Scheduler’s open we’re ready to add our task.
Adding the task
Now we have git bash installed and task scheduler open. Let’s add the task!
Creating a task
We need to create a task that will run our script.
In Task Scheduler, find the Task Scheduler Library folder tree. This should contain some folders like Microsoft, or WPD - these are known as** Task Folders**. Task Folders are used to store tasks (duh!).
For this example, we’ll add a new folder - Scripts. Adding a new Task Folder is easy:
- in the actions panel, click New Folder and enter the folder name (Scripts). (If that was too vague, Click here for more detailed instructions on adding a Task Folder).
- After you’ve added the new folder, click on it in the folder tree.
- In the actions panel, click Create Task. This will open the create task wizard.
- Give your task a a name and a description - to describe what your task is doing.
- In Security options, make sure** Run whether user is logged on or not **is selected.
- Select Run with highest privileges. This will avoid any permissions issues node might have running file system operations.
- Open the Triggers tab
- Click New… to add a new trigger. A trigger is an action or event that begins your task. In this example, we’re going to have it trigger on a schedule. So it will run daily at a specified time.
- In the Begin the task dropdown, select On a schedule
- In the Settings panel, select Daily
- In the start data panel, select the date for the trigger to begin and the time that it should run. (22:00 in our case)
- Click OK
- Switch to the Actions tab
- Click New…
- Now, we add the action. This is the meat of the tutorial and it’s a bit more complicated. So we’re going to go into this in more detail below.
The action is the thing that is run when the trigger occurs.
Our action is going to be - start git bash, navigate to the Scripts folder, run script.js in node.
We can start git bash using the Start a program action. The rest will be done using arguments.
- In the Action drop down select Start a program
- Click Browse…
- Find git-bash.exe in file explorer, select it and click Open. It should be located in C:\Program Files or Program Files (x86)
Now git bash will start when our trigger is ehum triggered. The next thing is to add the arguments.
This is the meat of the tutorial. The arguments are entered when git bash is opened. They change directory to our Scripts folder, execute our script in node and close git bash when the process is complete.
Copy and paste the code below into the Add arguments input field.
--a -i -c "cd Scripts; node script.js"
This might look cryptic to you, but it’s just a list of commands for the computer to carry out.
-i sets the shell to run in interactive shell mode.
-c sets it to execute the first non option argument passed to it, then exit. So in our example it’ll execute the node command.
-l is short for login.
cd changes directory into the Scripts folder (cd - change directory). This is located in the user folder (C:\Users\Edd in my case). Git bash opens in the ~ directory, which is normally the directory of the logged in user.
node script.js runs script.js in node.
Now our **New Action **dialog should look similar to the image below.
If it does, we’re ready to go!
Click Ok to save the new action, and Ok to save the new Task.
Woop! The new task is saved and will run at 10PM everyday!