在启动时运行 R 脚本
首先,这可能需要移至超级用户。我无法决定哪个场地更好。
我正在尝试编写一个将在启动/重新启动时运行的 R 脚本,并将该计算机添加到 doRedis 工作人员。 (doRedis 是一个 foreach 后端)。
这是我的 R 脚本“~/Rworker.R”
#Define Parameters
require(multicore)
Host <- 'ip_of_doRedis_Server'
cores <- multicore:::detectCores()
TO <- 24*3600
#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)
我可以使用以下命令从命令行运行此脚本 sudo R CMD BATCH ~/Rworker.R ~/RLog
。
接下来,我编写了一个名为“/etc/init.d/StartWorkers.sh”的 shell 脚本来运行 R 脚本,
#!/bin/sh
sudo echo "Starting R workers"
sudo R CMD BATCH ~/Rworker.R ~/RLog
我使用 chmod +x StartWorkers.sh 使该 shell 脚本可执行。当我运行 ./StartWorkers.sh
时,一切都运行良好,R 会话启动,并且工作线程被添加到池中。
现在,我需要在启动/重新启动机器时运行这个 shell 脚本,所以我输入 update-rc.d StartWorkers.sh 默认值
。该命令似乎有效,但我收到以下警告: 'update-rc.d:警告:/etc/init.d/StartWorkers.sh 缺少 LSB 信息'
但是,通过 rcconf 检查确认“StartWorkers.R”位于启动列表中。
但是,当我重新启动计算机时,脚本无法运行。我做错了什么? shell 脚本从命令行运行良好,但当我尝试在启动时运行它时失败。
/编辑:好的,根据德克的回答,我安装了更少的内容,并将“StartWorkers.sh”更改为以下内容:
#! /usr/bin/r
#Define Parameters
require(multicore)
Host <- 'zachec2.dyndns.org'
cores <- multicore:::detectCores()
TO <- 24*3600
#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)
但是当我运行它时,我得到以下输出:
Loading required package: utils
Loading required package: multicore
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'multicore'
Error in loadNamespace(name) : there is no package called 'multicore'
Calls: ::: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
我知道我的系统上安装了多核!
/EDIT2:我必须将所有 R 包移动到 cd /usr/lib/R/site-library ,现在较小的 shell 脚本可以工作了。我将脚本添加到 /etc/rc.local
并且它完美启动!
First of all, this may need to be moved to superuser. I couldn't decide which venue was better.
I am trying to write an R script that will run at boot/reboot and add that machine to a pool of doRedis workers. (doRedis is a foreach backend).
Here is my R script, "~/Rworker.R"
#Define Parameters
require(multicore)
Host <- 'ip_of_doRedis_Server'
cores <- multicore:::detectCores()
TO <- 24*3600
#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)
I can run this script from the command line, using the commandsudo R CMD BATCH ~/Rworker.R ~/RLog
.
Next, I wrote a shell script to run the R script, titled "/etc/init.d/StartWorkers.sh"
#!/bin/sh
sudo echo "Starting R workers"
sudo R CMD BATCH ~/Rworker.R ~/RLog
I made this shell script executable, using chmod +x StartWorkers.sh
. When I run ./StartWorkers.sh
everything works great and the R session starts up and the workers get added to the pool.
Now, I need this shell script to run when I boot/reboot the machine, so I typeupdate-rc.d StartWorkers.sh defaults
. This command appears to work, but I get the following warning:
'update-rc.d: warning: /etc/init.d/StartWorkers.sh missing LSB information'
However, a check with rcconf confirms that "StartWorkers.R" is on the startuplist.
However, when I reboot the machine, the script fails to run. What am I doing wrong? The shell script runs fine from the command line, but fails when I try to run it at startup.
/EDIT: ok, per Dirk's answer, I installed littler, and changed 'StartWorkers.sh' to the following:
#! /usr/bin/r
#Define Parameters
require(multicore)
Host <- 'zachec2.dyndns.org'
cores <- multicore:::detectCores()
TO <- 24*3600
#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)
But when I run it, I get the following output:
Loading required package: utils
Loading required package: multicore
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'multicore'
Error in loadNamespace(name) : there is no package called 'multicore'
Calls: ::: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
I know I have multicore installed on my system!
/EDIT2: I had to move all my R packages to cd /usr/lib/R/site-library
and now the littler shell script works. I added the script to /etc/rc.local
and it starts up perfectly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 R 问题,也是一个 Ubuntu 系统管理问题。这里有几点:
对于简单的启动任务,我建议仅使用
/etc/rc.local
,您可以在其中附加作业。我只是不喜欢
R CMD BATCH
这就是为什么 Jeff Horner 和我写了 littler 为您提供了/usr/bin/r
以及更简单的 R 脚本。 R 本身还为您提供了 Rscript;任何一个都比R CMD BATCH
更好。要测试脚本,只需以 root 身份运行它们即可。一旦它们工作,将它们添加到
/etc/rc.local
。希望这有帮助。
r-sig-debian
列表也是 Ubuntu / Debian 技巧的一个很好的来源。This is a bit of an R question, and a bit on an Ubuntu sysadmining question. here are a few points:
For simple start-up tasks, I recommned just using
/etc/rc.local
where you can append you jobs.I just don't like
R CMD BATCH
which is why Jeff Horner and I wrote littler which gives you/usr/bin/r
and much easier R scripting. R itself also gives youRscript
; either one is preferable overR CMD BATCH
.To test scripts, just run them as root. Once theyw ork, add them to
/etc/rc.local
.Hope this helps. The
r-sig-debian
list is a good source of Ubuntu / Debian tips too.