当前位置: 首页>>技术教程>>正文


如何唤醒睡眠中的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 $(<myscript.pid) sleep

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

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


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

sleep 1000 &
echo $! > myscript.sleep.pid
fg

并杀死它:

kill $(<myscript.sleep.pid)

次佳解决思路

您可以编写脚本来处理(“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

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

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/7578.html,未经允许,请勿转载。