我尝试在Linux中使用kill命令来终止一个进程。(不使用 -9 作为参数)
我需要确保该进程确实被终止。据我所知,kill 命令异步运行,可能需要一些时间才能完成。
我需要确保在使用 bash 运行 Kill 后我的进程已终止
你能帮忙吗?
谢谢!!!
我只想杀死一个进程,仅当该进程存在时,我想这样做,因为我有一个工具,仅在没有错误的情况下运行,并且我做了一些测试,当我尝试杀死一个不存在的进程时,确实存在错误不存在。这是我用来终止进程的行:
taskkill /f /IM notepad.exe
Run Code Online (Sandbox Code Playgroud)
但我想验证它仅在进程存在时执行。
谢谢你!!!
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_t test_thread;
void *thread_test_run (void *v)
{
int i=1;
while(1)
{
printf("into thread %d\r\n",i);
i++;
sleep(1);
}
return NULL
}
int main()
{
pthread_create(&test_thread, NULL, &thread_test_run, NULL);
sleep (20);
pthread_cancel(test_thread);
sleep(100);
// In this period (before the finish of myprogram),
// I execute killall to kill myprogram
// I want to add a signal handle function to
// execute pthread_exit() before the program quit
}
Run Code Online (Sandbox Code Playgroud)
我想通过添加一个信号处理函数来在程序退出之前执行 pthread_exit() 来完成我的代码。
怎么做 ?
我知道SIGSEGV当内核使用它报告内存访问冲突时不能忽略这一点。但是,如果我安装一个SIGSEGV不执行任何操作的信号处理程序,然后另一个进程使用kill该信号向我发送该信号,那么其行为是否与我使用“正常”信号(如 )相同SIGUSR1?
我是新手。我的程序在生命周期内使用 system call 创建了一些子进程fork()。我需要为父进程处理中断信号,并在它的处理程序中杀死在程序运行时创建的所有子进程。
我试过以下代码...
setpgrp(0,0);
$SIG{INT} = \&kill_all;
sub kill_all() {
print "Going to kill child processes \n";
kill -9, getpgrp();
}
....
fork()..
....
fork()...
....
fork()
.....
Run Code Online (Sandbox Code Playgroud)
但似乎并非所有进程都被杀死,请提出正确的方法来做到这一点。
假设我在子进程和父进程之间创建了一个管道并且子进程正常结束,子进程的管道会自动关闭吗?
另外,如果子进程也有子进程并且子进程以分段错误结束,它也会杀死我的孙子进程吗?我的意思是从进程表中删除它(我不需要等待它)。
编辑:例如,对于以下代码,我在子进程中生成分段错误并尝试在父进程中等待它。运行程序后,waitpid返回-1,但是当我检查WIFEXITED(status)时,子进程程序似乎正常退出。我得到了一个
杀死子进程失败:没有这样的进程
错误尝试杀死我的孙子进程。我想知道这是不是因为分段错误会自动关闭子进程和孙进程?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
int main( void ) {
pid_t childpid;
int ends[ 2 ];
pipe( ends );
if ( ( childpid = fork() ) == -1 ) {
perror( "fork failed" );
exit( 0 );
}
if( childpid == 0 ) {
pid_t cpid;
if ( ( cpid = fork() ) == -1 ) {
perror( "fork failed" );
exit( …Run Code Online (Sandbox Code Playgroud) 所以我正在处理一些后台任务,最终不得不生成一个子进程(使用另一个团队提供的二进制文件)。在某些时候,如果超时,我想停止这样的过程。
看起来够直观。
def run!(command, timeout)
Timeout.timeout(timeout) do
stdin, stdout, wait_thr = Open3.popen2e(command)
@pid = wait_thr.pid
# ... boring and irrelevant...
end
rescue Timeout::Error
Process.kill 'TERM', @pid
Process.wait pid
raise
end
Run Code Online (Sandbox Code Playgroud)
现在我也很喜欢在我的命令之前调用time. 漂亮的日志和所有。这使命令类似于(MacOS)
gtime -f 'Time spent %E memory used %M' some/binary --with parameters"
Run Code Online (Sandbox Code Playgroud)
所以我的流程树变成了这样
ruby (my background job)
\__ gtime
\__ some/binary
Run Code Online (Sandbox Code Playgroud)
当然,现在当我杀死子进程时,只会gtime被杀死,二进制文件会继续存在。
TERM进程的可执行文件,我可能会处理并杀死它的直接子进程。但它是time/gtime所以我显然没有。但也许有一些神秘的参数?我也可以解析ps输出,构建一个进程树并遍历它一个一个地杀死进程,但这似乎有点矫枉过正(抱歉)。我在这里缺少一些非常基本的东西吗?
我正在尝试编写一个shell脚本,它会杀死所有正在运行的匹配特定模式的进程,然后重新启动它们.我可以用以下方式显示进程:
ps -ef|grep ws_sched_600.sh|grep -v grep|sort -k 10
Run Code Online (Sandbox Code Playgroud)
其中列出了相关流程:
user 2220258 1 0 16:53:12 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW02_env
user 5562418 1 0 16:54:55 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW03_env
user 2916598 1 0 16:55:00 - 0:01 /bin/ksh /../../../../../ws_sched_600.sh EDW04_env
Run Code Online (Sandbox Code Playgroud)
但我不太确定如何通过进程ID来杀死?
我有这个任务,我需要从我的应用程序中杀死另一个我的应用程序,问题是另一个应用程序有一个“终止确认”对话框(没有要保存的关键数据,只有用户退出意愿的确认)。
在10.6+上,您将使用:
bool TerminatedAtLeastOne = false;
// For OS X >= 10.6 NSWorkspace has the nifty runningApplications-method.
if ([NSRunningApplication respondsToSelector:@selector(runningApplicationsWithBundleIdentifier:)]) {
for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.company.applicationName"]) {
[app forceTerminate];
TerminatedAtLeastOne = true;
}
return TerminatedAtLeastOne;
}
Run Code Online (Sandbox Code Playgroud)但在<10.6上,此常用的Apple Event:
// If that didn‘t work either... then try using the apple event method, also works for OS X < 10.6.
AppleEvent event = {typeNull, nil};
const char *bundleIDString = "com.company.applicationName";
OSStatus result = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeApplicationBundleID, …Run Code Online (Sandbox Code Playgroud)我正在寻找一种方法来使用Windows上的C#按名称杀死进程
.NET Compact Framework没有Process.GetProcessByName()方法,它只有一个.GetProcessById()方法.
但我不知道如何弄清楚我正在运行的进程的进程ID.
我想我可以循环所有进程ID,但这很可怕,因为我不知道进程ID的最大数量.谁知道更好的方法?
for (int i = 1; i < 40000; i++)
{
Process prs = Process.GetProcessById(i);
if (prs.StartInfo.FileName == "MyExe.exe")
{
prs.Kill();
}
prs.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
编辑:我找到了解决问题的方法.代码项目链接. http://www.codeproject.com/Articles/36841/Compact-Framework-Process-class-that-supports-full