【问题标题】:How to make an upstart service to Rserve?如何为 Rserve 提供新贵服务?
【发布时间】:2015-09-09 16:43:55
【问题描述】:

我需要从R 开始并继续运行rserve

我的rserve.r 文件中有这些行:

library(Rserve)
Rserve()

所以,我正在尝试在我的新贵脚本中做这样的事情:

description "Rserve service"
author      "Valter Silva"

start on filesystem or runlevel [2345]
stop on shutdown

respawn

script
    echo $$ > /var/run/rserve.pid
    exec /usr/bin/R /home/valter/R/rserve.R >> /home/valter/test2.log
end script


pre-start script
    echo "[`date`] Rserve Starting" >> /var/log/rserve.log
end script

pre-stop script
    rm /var/run/rserve.pid
    echo "[`date`] Rserve Stopping" >> /var/log/rserve.log
end script

我知道服务运行是因为我的文件test2.log 的输出。但它只运行一次。我应该怎么做才能让它继续运行?

【问题讨论】:

  • “它只运行一次”是什么意思?
  • @StevenBeaupré 如果我启动 R 命令行并给出命令 library(Rserve)Rserve(); Rserve 保留在 linux 进程列表中。但如果我试图在我的新贵身上做到这一点,什么都不会发生。
  • 尝试使用R CMD Rserve 来启动它? (见rforge.net/Rserve/doc.html#start
  • 我试过了,但它给了我以下错误:/usr/lib/R/bin/Rcmd: 62: exec: Rserve: not found
  • @ValterHenrique 检查这个问题:stackoverflow.com/questions/24370980/…

标签: r rserve


【解决方案1】:

您(和我)遇到这么多麻烦的原因是我们问错了问题。正确的问题是“我们如何从我们的客户端代码启动 Rserve?”Java(和大多数其他)客户端具有 StartRserve 的能力。

这个问题的答案在 java 库中:How to start Rserve automatically from Java?

或者这里是 C# 库: https://github.com/SurajGupta/RserveCLI2/blob/master/RServeCLI2.Test/Rservice.cs


另一种方法是向the best 学习。 Rocker 项目复制supervisor.conf/etc/supervisor/conf.d/ (见https://github.com/rocker-org/rocker/blob/master/rstudio/Dockerfile#L61

你的 supervisor.conf 可以添加类似的东西

[program:Rserve]
command=/usr/bin/Rserve.sh
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
user=myuser
startsecs=0
autorestart=false
exitcodes=0

但是... 我确实想通了。所以这就是答案。我很抱歉它有点笨拙和分散。对我来说,我最大的障碍是将我的启动/停止脚本放在 /usr/bin 文件夹中(参见下面的讨论)

/etc/init/Rserve.conf

description "Rserve service"
author "Victor I. Wood"
version "0.01"

# based on
# https://stackoverflow.com/questions/32485131/how-to-make-an-upstart-service-to-rserve

env STARTSCRIPT=/usr/bin/Rserve.sh
env STOPSCRIPT=/usr/bin/Rserve.stop
env LOGFILE=/var/log/Rserve.log


start on runlevel [2345]
stop on runlevel [!2345]

console output

respawn

# tell upstart we're creating a daemon
# upstart manages PID creation for you.
expect fork


pre-start script
    echo "[`date`] Rserve Starting" >> $LOGFILE
    echo $$ > /var/run/Rserve.pid
end script

pre-stop script
    rm /var/run/Rserve.pid
    echo "[`date`] Rserve Stopping" >> $LOGFILE
    exec $STOPSCRIPT >> $LOGFILE
end script


script
        # My startup script, plain old shell scripting here.
        exec $STARTSCRIPT >> $LOGFILE
#       echo $$ > /var/run/rserve.pid
end script

/usr/bin/Rserve.sh

#!/bin/sh
sudo --login --set-home --user=myuser bash -c '~/Rserve.sh >~/Rserve.log 2>&1'

/usr/bin/Rserve.stop

#!/bin/sh
pkill -U myuser Rserve

/home/myuser/Rserve.sh

#!/bin/sh

# launch new Rserve process
R CMD Rserve --RS-conf /home/myuser/Rserve.conf --no-save >~/Rserve.log 2>&1

https://askubuntu.com/questions/62812/why-isnt-my-upstart-service-starting-on-system-boot?lq=1

我从脚本开始 https://askubuntu.com/questions/62729/how-do-i-set-up-a-service?lq=1 并注意到initctl start 可能对调试有用。

https://askubuntu.com/questions/62812/why-isnt-my-upstart-service-starting-on-system-boot?lq=1 注意到脚本必须在/bin 中(但我使用了/usr/bin),并且必须归root 所有。有些人喜欢使用其他地方的脚本链接作为脚本 i /bin

作为How to make an upstart service to Rserve?的人们 已建议,我正在使用在 rServe 文档中找到的 Rserve 启动命令

R CMD Rserve

虽然我已经将我的扩展为

R CMD Rserve --RS-conf /home/myuser/Rserve.conf --no-save >~/Rserve.log 2>&1

我没有导出正确的 pid,所以我用 pkill 停止它。更优雅的方法是

RSshutdown(rsc)

How can I shut down Rserve gracefully?

stdout 和 stderr 混淆了新贵,所以用2>&1 重定向所有输出 How to redirect both stdout and stderr to a file

当我找到的时候我有点想通了 https://www.digitalocean.com/community/tutorials/the-upstart-event-system-what-it-is-and-how-to-use-ithttps://geeknme.wordpress.com/2009/10/15/getting-started-with-upstart-in-ubuntu/ http://blog.joshsoftware.com/2012/02/14/upstart-scripts-in-ubuntu/ 但是对于刚开始的人来说,这些是比官方文档更实用的起点。


更新

此解决方案假定 Upstart,这是最初的问题,但 Upstart 不再是默认的服务管理器。如果您想在 16.04 后的系统上执行此操作,请改回 Upstart with

sudo apt-get install upstart-sysv
sudo update-initramfs -u
sudo apt-get purge systemd

(来自http://notesofaprogrammer.blogspot.com/2016/09/running-upstart-on-ubuntu-1604-lts.html

【讨论】:

    【解决方案2】:

    正如 Rserve 的公共文档所述,您可以从 shell 脚本启动 Rserve 守护进程。

    只需创建一个执行以下命令的脚本:

    echo 'library(Rserve);Rserve(FALSE,args="--no-save --slave --RS-conf <your_own_path>/rserve.conf")'|<R_bin_path>/R --no-save --slave
    

    例如,在我的 MacOS 计算机中,我可以启动 Rserve 执行此行:

    /bin/sh -c "echo 'library(Rserve);Rserve(FALSE,args=\"--slave\")'|/Library/Frameworks/R.framework/Versions/3.2/Resources/bin/exec/R --no-save --slave"
    

    此命令输出如下内容:

    Starting Rserve:
      /Library/Frameworks/R.framework/Resources/bin/R CMD /Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rserve/libs//Rserve --slave 
    
    Rserv started in daemon mode.
    

    确保在启动 Rserve 时指定“--slave”参数。

    您可以创建一个 shell 脚本(或 windows 脚本)并告诉操作系统在启动过程中执行此脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多