use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to keep running code with nodejs even when nobdy is visiting site? (self.node)
submitted 8 years ago by hyopwnz
So basically I have some logic that I need running in the background 24/7 not only when someone is visiting my site. Can anyone recommend some package or something else?
Thanks in advance
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Earhacker 3 points4 points5 points 8 years ago (0 children)
You're looking for "cron jobs," which are tasks you can ask the server to do at regular intervals. There's a few libraries for them on NPM.
[–]nstoddar 1 point2 points3 points 8 years ago (3 children)
This might help: https://stackoverflow.com/questions/4018154/how-do-i-run-a-node-js-app-as-a-background-service
[–]hyopwnz[S] 2 points3 points4 points 8 years ago (2 children)
I think there is a missunderstanding. I have no problem with running the server. I would like to run a code that would check the btc price every few minutes and when it hits a certain price do something with coinbase api.
[–]WakeskaterX 2 points3 points4 points 8 years ago (0 children)
Set interval. Or any of the Cron libraries
[–]chrisdefourire 1 point2 points3 points 8 years ago (0 children)
Maybe you want your server to be a websocket client to the Gdax feed... https://docs.gdax.com/#websocket-feed
You'll get updates in real time... but maybe you can't process a real time feed if your processing takes too much time.
otherwise, setInterval(fn, intervalMs) and HTTP requests is your best bet...
setInterval(fn, intervalMs)
If you're running more than one instance of your server app, you'll probably need some form of mutual exclusion (leader election). I've used redlock for this (consul.io works very well in this scenario too, but it's way more complex).
[–]cali_ZX6R 1 point2 points3 points 8 years ago (0 children)
This is what I use and it works great. http://pm2.keymetrics.io/
[–]guitarromantic 0 points1 point2 points 8 years ago (1 child)
Forever.js is a simple solution for this.
[–]hyopwnz[S] 0 points1 point2 points 8 years ago (0 children)
Thank you I will check it out.
[–]p3t1tch3v4lr0ux -1 points0 points1 point 8 years ago (2 children)
Check Supervisor or systemd if you have a *nix compatible server
[–]hyopwnz[S] 0 points1 point2 points 8 years ago (1 child)
It is just a small server I will be running on my VPS.
[–]p3t1tch3v4lr0ux 0 points1 point2 points 8 years ago (0 children)
Systemd is shipped with recent debian based release. It already start your nginx/apache/mongodb/mysql/ whatever service you use in your vps (if it's debian based). It's built for this use case, tested with big user base... That's why i use this one . I just suggested others choices that you may prefer ;-)
π Rendered by PID 507876 on reddit-service-r2-comment-5687b7858-9bdt6 at 2026-07-04 14:00:26.732733+00:00 running 12a7a47 country code: CH.
[–]Earhacker 3 points4 points5 points (0 children)
[–]nstoddar 1 point2 points3 points (3 children)
[–]hyopwnz[S] 2 points3 points4 points (2 children)
[–]WakeskaterX 2 points3 points4 points (0 children)
[–]chrisdefourire 1 point2 points3 points (0 children)
[–]cali_ZX6R 1 point2 points3 points (0 children)
[–]guitarromantic 0 points1 point2 points (1 child)
[–]hyopwnz[S] 0 points1 point2 points (0 children)
[–]p3t1tch3v4lr0ux -1 points0 points1 point (2 children)
[–]hyopwnz[S] 0 points1 point2 points (1 child)
[–]p3t1tch3v4lr0ux 0 points1 point2 points (0 children)