Run a TS file every X minutes

I have a code file in TS format which I can launch it from command prompt like this

npx ts-node maincodes.ts   

Lets say I dont have access to change anything inside maincodes.ts file and I want it to be launched every X minutes on my machine (It is windows btw and using vs code).

How can I do that?

Thanks

Assuming youre on a unix terminal:

You need to transpile that .ts file into .js to run it in node, alternatively download something like ts-node from npm:

npm install -g ts-node

Now you need to schedule the task to run every 15 minutes:

Open crontab:

crontab -e

Add a new line to schedule the script execution. To run the script every 15 minutes, you can add:

*/15 * * * * ts-node /path/to/your/file.ts

That should use ts-node to run your program every 15 minutes.

For windows – open “Task Scheduler” app and add a task to run the command you want whenever you want it to run.

In order to run maincodes.ts periodically without modifying the original file you can create .bat file to execute the program on a specific interval. Afterwards use Task Scheduler to schedule the script.

script.bat

cd /d "C:\pathtoproject" 
npx ts-node maincodes.ts

add @echo off if you want the commands to not be displayed on the terminal

To schedule the task follow and select our script:

https://sqlbackupandftp.com/blog/how-to-schedule-a-script-via-windows-task-scheduler/

Leave a Comment