具有R停止EC2机它的运行它的

2023-09-11 08:44:00 作者:孤王°

我有几个工作流程,我想为r的剧本完成后暂停在Linux机器它的运行上。我能想到的两个类似的方法可以做到这一点:

I have a few work flows where I would like R to halt the Linux machine it's running on after completion of a script. I can think of two similar ways to do this:

运行为r根,然后调用系统(暂停) 从一个root shell脚本运行R(可以运行第r脚本的任何用户),然后让shell脚本执行停止 R位完成后。 run R as root and then call system("halt") run R from a root shell script (could run the R script as any user) then have the shell script run halt after the R bit completes.

是否有这样做的其他简便的方法?

Are there other easy ways of doing this?

该用例这里是在AWS上运行脚本,我想实例停止脚本完成后,这样我就不会被收取机时交的作业运行。我的情况下我使用的数据分析是一个EBS支持的实例,所以我不希望终止它,只是暂停。从实例中发出停止命令是相同的效果停止/从AWS控制台暂停。

The use case here is for scripts running on AWS where I would like the instance to stop after script completion so that I don't get charged for machine time post job run. My instance I use for data analysis is an EBS backed instance so I don't want to terminate it, simply suspend. Issuing a halt command from inside the instance is the same effect as a stop/suspend from AWS console.

推荐答案

我的IM pressed的作品。 (对于任何人惊讶的是,一个实例可以自行停止,见注1和; 2)

I'm impressed that works. (For anyone else surprised that an instance can stop itself, see notes 1 & 2.)

您也可以尝试命令叫停,因为你不会需要以root用户运行,只要运行R中的用户帐户可以运行sudo的的。这是pretty的共同上EC2大量的AMI。

You can also try "sudo halt", as you wouldn't need to run as a root user, as long as the user account running R is capable of running sudo. This is pretty common on a lot of AMIs on EC2.

注意什么构成的 - [R戒烟的假设 - 不管你信不信,人能崩溃R.这可能是更好的有手表第r PID和,一旦PID不再有效单独的脚本,终止实例。这样做的R里面这个命令意味着,当R崩溃,它永远不会到达呼叫停止。如果你从另一个脚本中调用它,这可能是危险的,太。如果你知道Linux的好,你要找的是什么PID启动R,它可以通到另一个脚本,检查 PS ,说的每1秒,然后一旦终止的PID不再运行的实例。

Be careful about what constitutes an assumption of R quitting - believe it or not, one can crash R. It may be better to have a separate script that watches the R pid and, once that PID is no longer active, terminates the instance. Doing this command inside of R means that if R crashes, it never reaches the call to halt. If you call it from within another script, that can be dangerous, too. If you know Linux well, what you're looking for is the PID from starting R, which you can pass to another script that checks ps, say every 1 second, and then terminates the instance once the PID is no longer running.

我觉得一个更好的解决方案是使用EC2 API工具(参阅:的http:// docs.amazonwebservices.com/AWSEC2/latest/APIReference/ 的文档)终止的或的停止实例。有两个的这些之间的差,并且如果你的实例的EBS备份或S3支持它很重要。你有私钥和证书表明亚马逊,你是老板,上面的海polloi的只是谁对您的实例的根接入方式的事实 - 你不必为根,以终止该实例中运行。

I think a better solution is to use the EC2 API tools (see: http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ for documentation) to terminate OR stop instances. There's a difference between the two of these, and it matters if your instance is EBS backed or S3 backed. You needn't run as root in order to terminate the instance - the fact that you have the private key and certificate shows Amazon that you're the BOSS, way above the hoi polloi who merely have root access on your instance.

由于这些凭据可用于恶作剧,小心从给定的服务器上运行的API工具,你需要你的证书,并在服务器上的私有密钥。这是在你有一个安全问题的事件是一个坏主意。这将是更好的消息到主服务器并将其关闭实例。如果您有信息设置中的实例之间的任何方式,这可以为你做的所有工作。

Because these credentials can be used for mischief, be careful about running API tools from a given server, you'll need your certificate and private key on the server. That's a bad idea in the event that you have a security problem. It would be better to message to a master server and have it shut down the instance. If you have messaging set up in any way between instances, this can do all the work for you.

注1:埃里克·哈蒙德的报告的停止只会挂起EBS实例,所以你还是有保管费。如果你碰巧启动了很多这样的情况下,这可能会搞乱的东西了。你原来的问题,似乎都不清楚自己是否意味着终止或停止实例。他此页面上有其他好的建议

Note 1: Eric Hammond reports that the halt will only suspend an EBS instance, so you still have storage fees. If you happen to start a lot of such instances, this can clutter things up. Your original question seems unclear about whether you mean to terminate or stop an instance. He has other good advice on this page

注2:短螺纹在EC2开发人员论坛给建议对于Linux和放大器; Windows用户。

Note 2: A short thread on the EC2 developers forum gives advice for Linux & Windows users.

注3:重新启动时EBS实例照单支付部分小时,甚至。 (见从开发者论坛这个线程)具有自动暂停接近小时标记可能是有用的,假设R过程不工作,万一有可能重新任务实例(即节省而不是重新启动)。其他有用的工具来考虑: setTimeLimit setSessionTimeLimit ,以及各种检查点工具(我有问答的mentions一对夫妇)。如果一个人有潜在的顽劣code使用自动杀是非常有用的。

Note 3: EBS instances are billed for partial hours, even when restarted. (See this thread from the developer forum.) Having an auto-suspend close to the hour mark can be useful, assuming the R process isn't working, in case one might re-task that instance (i.e. to save on not restarting). Other useful tools to consider: setTimeLimit and setSessionTimeLimit, and various checkpointing tools (I have a Q that mentions a couple). Using an auto-kill is useful if one has potentially badly behaved code.

注4:我最近学会了关机命令包乐趣。这是多平台。请参见这个博客帖子的评论,和code是的此处。危险的东西,如果你想以适应Windows,但它可能是有益的。我还没有尝试过,但。

Note 4: I recently learned of the shutdown command in package fun. This is multi-platform. See this blog post for commentary, and code is here. Dangerous stuff, but it could be useful if you want to adapt to Windows. I haven't tried it, though.

更新1.三种更多的想法:

Update 1. Three more ideas:

您可以使用。去年() runLast = TRUE Q() 退出(),这可能关闭实例。 如果使用利特勒或调用通过RSCRIPT剧本脚本,同样的命令行功能都可以使用。 在我最喜欢的包的今天, tcltk2 有一个整洁的定时器机制,名为 tclTask​​Schedule(),可用于安排的当然pression执行。然后,您可以就在一个小时的时间间隔已经过去疯狂地使用的东西执行。 You could use .Last() and runLast = TRUE for q() and quit(), which could shut down the instance. If using littler or a script that invokes the script via Rscript, the same command line functions could be used. My favorite package of today, tcltk2 has a neat timer mechanism, called tclTaskSchedule() that can be used to schedule the execution of an expression. You could then go crazy with the execution of stuff just before a hourly interval has elapsed.