linux 脚本 休眠,如何唤醒睡眠中的bash脚本?

问题描述

是否可以唤醒使用sleep命令暂停的进程?

例如,假设您具有以下脚本:

#!/bin/bash

echo "I am tired"

sleep 8h

echo "I am fresh :)"

30分钟后,您发现需要停止脚本,也就是说,希望您编写了sleep 30m。

您不希望致电kill PID或按Ctrl + C,因为那样一来,最后的命令将不会执行,您将很累。

有没有办法从sleep唤醒该过程,或者使用其他支持唤醒的命令?欢迎为后台和前台流程提供解决方案。

最佳解决思路

当Bash脚本运行sleep时,pstree可能如下所示:

bash(10102)───sleep(8506)

两者都具有进程ID(PID),即使作为脚本运行也是如此。如果我们想中断睡眠,我们将发送kill 8506,然后Bash会话将恢复。问题是在脚本环境中,我们不知道sleep命令的PID,也没有人看进程树。

我们可以通过$$魔术变量获取Bash会话的PID。如果可以将其存储在某个位置,则可以定位在该PID下运行的sleep实例。这是我在脚本中输入的内容:

# write the current session's PID to file

echo $$ >> myscript.pid

# go to sleep for a long time

sleep 1000

然后我们可以告诉pkill核对在该PID下运行的sleep实例:

pkill -P $(

同样,这将自身限制为仅在该Bash会话下直接运行的sleep进程。只要正确记录了PID,这将使其比killall sleep或pkill sleep安全得多,后者可能会破坏系统上的任何sleep进程(允许权限)。

我们可以通过以下示例证明这一理论,其中有三个单独的bash会话,其中两个运行sleep。仅由于我们指定了top-left bash会话的PID,所以仅杀死了其sleep。

be89c19f3de2f5ddafb759ba14b48f08.png

另一种方法是将sleep推入后台,存储其PID,然后将其返回到前台。在脚本中:

sleep 1000 &

echo $! > myscript.sleep.pid

fg

并杀死它:

kill $(

次佳解决思路

您可以编写脚本来处理(“trap”)来自kill等的其他信号。因此,可以根据需要修改脚本行为。见男人bash:

SIGNALS

When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not

kill an interactive shell), and SIGINT is caught and handled (so that the wait builtin is interrupt-

ible). In all cases, bash ignores SIGQUIT. If job control is in effect, bash ignores SIGTTIN, SIGT-

TOU, and SIGTSTP.

Non-builtin commands run by bash have signal handlers set to the values inherited by the shell from

its parent. When job control is not in effect, asynchronous commands ignore SIGINT and SIGQUIT in

addition to these inherited handlers. Commands run as a result of command substitution ignore the

keyboard-generated job control signals SIGTTIN, SIGTTOU, and SIGTSTP.

The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the

SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the

SIGHUP. To prevent the shell from sending the signal to a particular job, it should be removed from

the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to not receive

SIGHUP using disown -h.

If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an inter-

active login shell exits.

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. When bash is waiting for an asynchronous com-

mand via the wait builtin, the reception of a signal for which a trap has been set will cause the wait

builtin to return immediately with an exit status greater than 128, immediately after which the trap

is executed.

第三种解决思路

您可以杀死睡眠,这将继续执行脚本的下一行:

pkill sleep

请注意,这将杀死系统中运行的所有睡眠进程,而不仅仅是在脚本中。

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值