简体   繁体   English

懒惰/优雅地停止当前运行的 bash 脚本

[英]stop currently running bash script lazily/gracefully

Say I have a bash script like this:假设我有一个这样的 bash 脚本:

#!/bin/bash
exec-program zero
exec-program one

the script issued a run command to exec-program with the arg "zero", right?脚本向 exec-program 发出了一个运行命令,参数为“零”,对吗? say, for instance, the first line is currently running.例如,第一行当前正在运行。 I know that Ctrl-C will halt the process and discontinue executing the remainder of the script.我知道 Ctrl-C 将停止进程并停止执行脚本的其余部分。

Instead, is there a keypress that will allow the current-line to finish executing and then discontinue the script execution (not execute "exec-program one") (without modifying the script directly)?相反,是否有一个按键可以让当前行完成执行然后停止脚本执行(不执行“exec-program one”)(不直接修改脚本)? In this example it would continue running "exec-program zero" but after would return to the shell rather than immediately halting "exec-program zero"在这个例子中,它会继续运行“exec-program zero”,但之后会返回到shell而不是立即停止“exec-program zero”

TL;DR Something runtime similar to "Ctrl-C" but more lazy/graceful ?? TL;DR 运行时类似于“Ctrl-C”但更懒惰/优雅??

In the man page, under SIGNALS section it reads:在手册页的 SIGNALS 部分下,它写道:

If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.如果 bash 正在等待命令完成并接收到设置了陷阱的信号,则在命令完成之前不会执行陷阱。

This is exactly what you're asking for.这正是你要问的。 You need to set an exit trap for SIGINT, then run exec-program in a subshell where SIGINT is ignored;您需要为 SIGINT 设置退出陷阱,然后在忽略 SIGINT 的子 shell 中运行exec-program so that it'll inherit the SIG_IGN handler and Ctrl+C won't kill it.这样它就会继承 SIG_IGN 处理程序并且Ctrl+C不会杀死它。 Below is an implementation of this concept.下面是这个概念的一个实现。

#!/bin/bash -
trap exit INT
foo() (
  trap '' INT
  exec "$@"
)

foo sleep 5
echo alive

If you hit Ctrl+C while sleep 5 is running, bash will wait for it to complete and then exit;如果在sleep 5运行时按 Ctrl+C ,bash 将等待它完成然后退出; you will not see alive on the terminal.你不会在终端上看到alive

exec is for avoiding another fork() btw. exec是为了避免另一个 fork() 顺便说一句。

暂无
暂无

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

相关问题 如何优雅地停止bash脚本中的无限循环? - How to stop infinite loop in bash script gracefully? 将尾巴输出到当前正在运行的bash脚本 - Pipe tail output to bash script currently running 一线确定当前正在运行的bash脚本的目录 - One-liner to determine the directory of the currently running bash script Solr在Docker中运行时优雅地停止 - Stop Solr gracefully when it running in docker 如何在bash脚本中优雅地编辑文件 - how to edit a file in bash script gracefully 对脚本当前所在目录以外的目录运行 bash 脚本时,二进制运算符预期错误 - Binary operator expected error when running a bash script against a directory other than the one the script is currently located in Bash脚本:目前,我在一个目录中应该有500个文件,如果缺少任何文件,如何停止bash脚本? - Bash Scripting: I currently am supposed to have 500 files inside a directory, how can I stop a bash script if any files are missing? 在Mac中,如何在脚本(sh / bash / applescript)中确定当前是否通过Apple Remote Desktop运行? - In Mac, how do I determine in a script (sh/bash/applescript) if currently running via Apple Remote Desktop? 在Mac Terminal中显示当前正在运行的进程时隐藏bash脚本的内容 - Hide bash script's content when displaying currently-running processes in Mac Terminal 确定当前运行的 shell 是 bash 还是 zsh - Definitively determine if currently running shell is bash or zsh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM