Run R script in background forever

Hi, I want to run an infinite while loop which checks for a condition in MongoDB and performs a task if the condition is met.

Can I run a script forever which checks for that condition and executes a task at all times the condition is met?

Can you give us more information about your use case? Depending on what the task actually is, this could by handled on different ways, for example if you only want to do data manipulation, this could be done by a trigger on the database itself bypassing R.

Yeah so, I want to run a prediction model for spare parts in a native R script. There could be over 1000 spare parts.

So I built an API that would start a "forecast" instance in the DB and endless while loop in the native script would repeatedly check for a new "forecast" instance and once the value of the key is "start" through the API hit, it will forecast in backend while the single-thread API is free to take other requests.
I just would like to know how to keep this script running the while loop, what sort of container (don't know what would be the appropriate technology here) could be used to host this script

I'm not familiar with MongoDB so I don't know what capabilities has, but other data bases (e.g. PostgreSQL) can run R scripts on reaction to trigger events, maybe you want to check if that is an option for you, since it would be a better workflow. If the former is not possible, have you considered scheduling a job to run the R script repeatedly instead of running an infinite loop?

1 Like

I will explore both options. Thanks a lot for your help! :grinning:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.