2

I'm trying to setup a simple beakr service in Windows that implements the example at https://github.com/MazamaScience/beakr. I'm able to run the script from the command line successfully and I've been able to add the service in Windows using NSSM, but I am unable to start the service.

When I dig into the service error logs I see that Rscript.exe cannot be executed due to a non-specific permissions problem.

My Rscript.exe is running out of C:\Program Files\R<Version>\bin and my beakr.R script is running out of my User home directory.

If anyone has had success implementing a similar service (Web page based REST endpoint) using R in Windows, I would love to know how you did it.

lagerratrobe
  • 151
  • 4

1 Answers1

0

This is what I did to run an R script as Service using latest pre-release version of NSSM on Windows 10:

  1. Create a directory to store the files
    In this example : C:\R\ServiceTest

  2. Create in this directory a never ending script : ServiceTest.R

library(beepr)

# Test script : beeps every 10 seconds
while (T) {
  beepr::beep(1)
  if (interactive()) {
    # Shows spin cursor to facilitate test in interactive mode
    for (i in 1:10) {
      if (i%%4==0) {cursor <- '/'}
      if (i%%4==1) {cursor <- '-'}
      if (i%%4==2) {cursor <- '\\'}
      if (i%%4==3) {cursor <- '|'}
      cat('\r',cursor)
      flush.console() 
      Sys.sleep(1)
    } 
  } else {
    Sys.sleep(10)
  }
}

I used to let this kind of script run in a console open on my desktop to check various alarms regularly.

  1. Create a batch file to run the script : ServiceTest.bat
Rscript ServiceTest.R
  1. Open an Admin console and make sure the batch file runs correctly:
C:\R\ServiceTest>ServiceTest.bat

C:\R\ServiceTest>Rscript ServiceTest.R
 |

Cancel the batch (Ctrl+C)

  1. Using the Admin console, install the batch file as service using NSSM :
nssm install

enter image description here

  • Set Service name : ServiceTest
  • Set Application path : C:\R\ServiceTest\ServiceTest.bat
  • Set Working directory : C:\R\ServiceTest\
  • Set Logon : Windows User + Password
  • Install Service
  1. Open Windows Services Manager, find ServiceTest and start it : if everything went well, that's it!

  2. If you get an error message, check Windows Event Log / Services : you can find there hints on the cause of the problem. Most common problems I encountered :

  • error on path
  • used local user instead of own user account / password to run the service
  1. If you want to remove the service :
nssm remove ServiceTest

This replaced very nicely the many R consoles I left running in the background on my Desktop.
I see no reasons it wouldn't work with a REST endpoint.

Waldi
  • 39,242
  • 6
  • 30
  • 78