简体   繁体   English

如何在Amazon EC2 Linux实例上远程终止进程

[英]How to remotely kill a process on amazon EC2 linux instance

I use Amazon EC2 instances to perform some complex computation by using the AWS Java SDK, these computations might take so long sometimes. 我使用Amazon EC2实例通过使用AWS Java SDK执行一些复杂的计算,这些计算有时可能需要很长时间。 Hence, I need to kill the process running on the EC2 instance remotely from my Java code so that I can reuse the same instance for another task without needing to stop and start the instance. 因此,我需要从Java代码远程终止在EC2实例上运行的进程,以便可以将同一实例重用于另一任务,而无需停止和启动实例。

The problem with stop and start, is that Amazon treat partial hours as complete hours, so my aim is to make my code more cost effective. 停止和启动的问题是,Amazon将部分小时数视为完整小时数,因此我的目标是使代码更具成本效益。

I use SSH to connect with my EC2 instances and that is how I pass commands to be executed there. 我使用SSH连接我的EC2实例,这就是我传递要在此处执行的命令的方式。 I doubt that if I disconnect the SSH connection and connect to it again, it would kill whatever process was running there. 我怀疑如果断开SSH连接并再次连接它,它将杀死那里正在运行的任何进程。

In short, what I need is away of doing Ctrl+C but from within my Java code (without user intervention). 简而言之,我需要做的是Ctrl + C,而无需执行Java代码(无需用户干预)。 Is that possible? 那可能吗?

EDIT: To clarify, the computation is executed by a separate tool installed on the Linux EC2 instance. 编辑:澄清一下,计算是由安装在Linux EC2实例上的单独工具执行的。 So I just pass the command to start this tool, then the command line waits until its finished and shows the results. 因此,我只是传递命令以启动此工具,然后命令行等待其完成并显示结果。 On manual usage scenario, I can click Ctrl+C on linux command line and will get control of the command line back. 在手动使用情况下,我可以在linux命令行上单击Ctrl + C,它将重新获得对命令行的控制。 But in my case, I want to do similar thing from java code if possible. 但就我而言,如果可能的话,我想通过Java代码执行类似的操作。

Use the SIGAR library to scan the process list on a machine and kill your process. 使用SIGAR库扫描计算机上的进程列表并杀死您的进程。

Use getProcList() to get all process IDs, getProcArgs() to get the full command line of each process, and kill() to kill the relevant process or processes. 使用getProcList()来获取所有进程ID, getProcArgs()来获得每个进程的完整的命令行, 杀()杀死相关进程或进程。

So if a task is running and you want to kill it, SSH again into the machine and run your SIGAR based process killer. 因此,如果某个任务正在运行并且您想杀死它,请再次SSH进入计算机并运行基于SIGAR的进程杀手。

One dirty/functional way would be to kill your process via SSH using the java Runtime to execute it. 一种肮脏/有效的方法是使用Java Runtime通过SSH杀死您的进程。

Something like 就像是

Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec("ssh user@host command");

So in your case, if you know the program's PID (1234 for example): 因此,在您的情况下,如果您知道程序的PID(例如1234):

Process p = runtime.exec("ssh user@host kill 1234");

Or if you know the program's name: 或者,如果您知道程序的名称:

Process p = runtime.exec("ssh user@host pkill my_program_name_x64");

Note that you usually have to give absolute paths to the executables invoked via runtime. 注意,通常必须为通过运行时调用的可执行文件提供绝对路径。

So you'll have to replace ssh by something like /bin/ssh or /usr/bin/ssh as well as for kill and pkill or killall . 因此,您必须用/bin/ssh/usr/bin/ssh类的东西替换ssh以及killpkillkillall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM