How do I use Monit to keep an R script running? - r

I have an R script that I want to have running continuously on Ubuntu 10.10. I'm trying to setup Monit to ensure that it doesn't go down. As the script starts, it creates a pid file with the lines:
pid <- max(system("pgrep -x R", intern = TRUE))
write(pid, "/var/run/myscript.pid")
Then I've set up Monit with the lines:
check process myscript with pidfile /var/run/myscript.pid
start program = "/usr/bin/R --vanilla < /home/me/myscript.R > /home/me/myscript.out 2>&1"
Monit starts fine, but when I kill the R process, the R process is not started up again. I'm obviously doing something wrong. Is it in the syntax for starting the process? I noticed that the documentation says Monit first tries to stop the programme and I don't know any commands for stopping an R process.
Perhaps of relevance is that the above line for starting the program works when it is in the crontab for the root user, but not when started from my user crontab.
Any guidance greatly appreciated.

I can't comment about Monit, but there is a good article by Andrew Robinson in R News about using linux/unix tools to monitor R . In particular, screen and mail might be useful for your application.

Related

Getting error "you must have a tty to run sudo" when we run through Pentaho

I am getting a you must have a tty to run sudo error when I run a shell script with a R script in it.
Weird thing is, it doesn't happen every time. I think it is because of RAM issue, but I am not sure.
This means that you are running on a Linux distribution with 'sudo' configured to require a tty.
Please refer this link for an easy fix:
https://www.shell-tips.com/2014/09/08/sudo-sorry-you-must-have-a-tty-to-run-sudo/

running a program and keep track of its file system changes on unix

I'm searching for a way on unix to run a program in a certain way, so I can see when it exits what changes it did to the file system, like "CREATED /home/foo/bar.txt" or "CHANGED /home/foo/bar.txt". When you know a library/module that does this I would prefer if it supports C++, Python or Shell.
One thing you can do - is to trace program system calls and then analyze them.
For example, running an application with strace and catch open and write syscalls:
$ strace -e trace=open,write ./$APP_NAME

How to get the daemon Rserve running as worker dyno on Heroku

This question is an obscure problem - sorry for the length. I'm trying to deploy an app to Heroku. The app runs Rserve - a daemon of the R language, for running statistical reports. This should in principle be no more difficult than getting any daemon, such as memcached, to run in Heroku.
In Mac OSX I just start the daemon in the command line and forget it - all works fine. I'm interfacing with Rserve from node.js, using https://github.com/albertosantini/node-rio (not a factor here though).
But in deploying to Heroku, not having much luck. I'm using a multipack of R and node. Installation runs fine, all build steps exit okay, R starts fine.
Now comes the job of starting the Rserve daemon on the worker dyno.
My procfile looks like this:
web: node server.js
worker: R CMD Rserve --no-save
When I run it, I get the following error in the logs (scroll to end of block):
Rserv started in daemon mode.
heroku[worker.1]: State changed from starting to crashed
The Rserve() config docs are here: http://www.rforge.net/Rserve/doc.html I am not expert at configuring it but perhaps there is something in there that I should be doing for it to work in this environment?
An oddity is that you can run this without error from the Heroku run console, but (see below), it does not seem to actually be running when I try to access it from node.js:
heroku run R CMD Rserve
[Previously saved workspace restored]
Rserv started in daemon mode.
>
In node.js (heroku run node), I try testing it thus:
var rio = require('rio');
rio.evaluate("pi / 2 * 2");
which gives the error "Rserve call failed".
This leads me to think something is fundamentally wrong with what I am trying to do or how I am trying to do it.
Rserve runs as a daemon by default, so use a script to execute it so it runs "in process".
E.g.
# example R script for executing Rserve
require('Rserve')
# get the port from environment (heroku)
port <- Sys.getenv('PORT')
# run Rserve in process
run.Rserve(debug = FALSE, port, args = NULL, config.file = "rserve.conf")
And then your Procfile will have an entry as follows:
rserve: R -f rserve.r --gui-none --no-save
So I tried a dozen ways to get it started on a worker dyno, but all would crash. I never got to the bottom of all environment issues - I am not very expert at Unix. However... I did get it work by spawning a child process to run Rserve at the end of my server.js initialization script on my web dyno. It works.
childProcess.exec('R CMD Rserve --no-save', function (error, stdout, stderr) {});
My plan is to implement it this way in the worker process and use Web Workers to communicate between the separate environments.

Getting 'command not found' when running daemon in unix

I am attempting to run the script Daemon.php from a unix prompt as a daemon using the code below as per the instructions given in this tutorial.
daemon Daemon.php --log=/var/log/Daemon.log
When I run this I get the output
daemon: command not found
Does know what might be going wrong?
Try daemonize instead. If that also isn't found on your system, simply start Daemon.php in the background and see how far you get.

Schedule R script using cron

I am trying to schedule my R script using cron, but it is not working. It seems R can not find packages in cron. Anyone can help me? Thanks.
The following is my bash script
# source my profile
. /home/winie/.profile
# script.R will load packages
R CMD BATCH /home/script.R
Consider these tips
Use Rscript (or littler) rather than R CMD BATCH
Make sure the cron job is running as you
Make sure the script runs by itself
Test it a few times in verbose mode
My box is running the somewhat visible CRANberries via a cronjob calling an R script
(which I execute via littler but Rscript
should work just as well). For this, the entry in /etc/crontab on my Ubuntu server is
# every few hours, run cranberries
16 */3 * * * edd cd /home/edd/cranberries && ./cranberries.r
so every sixteen minutes past every third hour, a shell command is being run with my id. It changes into the working directory, and call the R script (which has executable modes etc).
Looking at this, I could actually just run the script and have setwd() command in it....

Resources