exit shell background job got killed?

SIGHUP信号处理技巧
本文探讨了在使用带有huponexit选项的登录shell时如何处理SIGHUP信号的问题。文章提供了几种解决方法:关闭huponexit选项、使用nohup命令运行进程或将进程置于screen会话中。

Feature, of a sort. If you are using a login shell, and if the huponexit shell option is enabled, then all jobs are sent a SIGHUP signal when the shell is exited. cp does not handle this signal, so the kernel applies the signal's default action, which is to terminate the process.

You have a few options:

  • Turn off huponexitshopt -u huponexit. The process may still exit if it ever tries to write to the terminal after it's been closed, depending on how it handles write errors. (Note that twiddling this option only affects that one shell. You'll need to add it to a shell startup script — or find and delete the corresponding command that's turning the option on — to have it apply to all shells subsequently started.)
  • Use nohup to run the command instead. This wrapper ignores SIGHUP, letting the underlying command run unaffected. Standard output streams are redirected into a file.
  • Use screen. This starts a completely new terminal session. You can detach to this session and reattach to it at a later time, as you please.


在分布式任务调度系统中,终止正在运行的作业通常涉及调度中心对作业执行线程的中断操作。以常见的调度框架为例,调度中心通过共享变量与作业执行线程通信,并调用中断方法来尝试终止线程。具体实现中,调度中心会调用类似 `toStop` 的方法,将作业状态标记为“停止”,并通过设置共享变量(如 `toStop` 标志)通知线程主动退出。由于 `Thread.interrupt()` 仅能中断处于阻塞状态的线程(如 `wait`、`join`、`sleep` 状态),因此实际终止线程通常依赖于线程自身的逻辑判断和退出机制 [^3]。 当作业被终止后,其状态显示为 "killed",这通常意味着调度中心已经主动请求终止该作业,而作业执行器在接收到终止信号后未能正常完成退出流程。例如,在某些调度系统中,如果作业执行器未能在规定时间内响应中断请求,调度中心可能会强制终止该作业,从而将其状态标记为 "killed"。此外,如果作业执行过程中发生严重错误(如资源不足、任务超时等),调度中心也可能主动将其终止并标记为 "killed" [^1]。 在 Elastic-Job-Cloud 中,常驻作业在任务异常崩溃后,无论是否开启失效转移功能,调度器都会重新提交执行器进行调度。因此,若一个常驻作业被标记为 "killed",可能是由于调度器检测到作业异常后主动终止了该任务,并准备重新调度 。 ### 代码示例 以下是一个简单的作业终止逻辑示例: ```java public class JobThread extends Thread { private volatile boolean toStop = false; private String stopReason; public void toStop(String stopReason) { this.toStop = true; this.stopReason = stopReason; } @Override public void run() { while (!toStop) { // 执行作业逻辑 try { // 模拟作业执行 Thread.sleep(1000); } catch (InterruptedException e) { // 处理中断请求 break; } } // 退出线程 System.out.println("作业线程已终止,原因: " + stopReason); } } ``` 在上述代码中,作业线程通过检查 `toStop` 标志来决定是否继续执行。当调度中心调用 `toStop` 方法时,线程会在下一次检查标志时退出。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值