You have to have the script running in the background or make it as a service.
Step 1: Make a schedule script
There are many packages for scheduling, most are cron based.
This is a sample with cron
package,
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second', Date.now());
}, null, true, 'America/Los_Angeles');
When you run this, does it log a new line every second?

Great!
Step 2: Make it run on background
So how can you run it on background or as a service? There are lots of options here too, forever
, pm2
, systemctl
etc. I'll use pm2
.
Go over and install pm2
,
npm i -g pm2
Start the script you want to run on background,
pm2 start index.js --name=run-every-second
Check the logs,
pm2 logs run-every-second
And it will continue to run on background,

You can even make it run on startup and so on using,
pm2 startup
pm2 save
Peace!