python - 创建一个新进程并在 bash/python 中检查父进程中的变量

标签 python bash fork

这是我想用伪代码执行的操作:

    create process 
    {
        while not (answer == yes in parent process)
        {
            mplayer alert.mp3
        }
    }

    show dialog "Time is up! Press 'yes' to stop the alarm."
    answer = get userinput
<小时/>

这是我最终使用的代码:

    #!/bin/bash

    sleep $1
    lockfile=$(mktemp)
    {
        while [[ -f "$lockfile" ]]
        do
            kdialog --passivepopup 'Time is up!' 1
            sleep 1
        done
    }&

    kdialog --msgbox 'Time is up! Leave this dialog to stop notifications.'
    rm $lockfile

感谢@abeaumet

最佳答案

由于您要共享的变量似乎是 bool 值,因此您可以根据临时文件是否存在来判断。

具体示例如下代码:

#!/bin/sh

# Create a lock file to permit communication (act as your bool variable)
LOCKFILE=`mktemp /tmp/scriptXXXX`

# Fork a subprocess in background
{
  # While the lockfile exists, wait
  while [ -f "$LOCKFILE" ] ; do sleep 1 ; done

  # When the file no longer exists, this is the signal from the main process
  echo 'File removed! Play music!'
} &

# Do some long stuff in main process...
sleep 5

# Delete the lockfile (child wait for it)
rm -f "$LOCKFILE" &>/dev/null

exit 0

关于python - 创建一个新进程并在 bash/python 中检查父进程中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16713540/

相关文章:

Python正则表达式提取 token

python - 在 python 中就地转换树

linux - fork 和僵尸进程

c - 这个程序创建了多少进程?

python - 在线程中打印不适用于python 3

bash - 将单个文件脚本分解为具有适当目录布局的项目

linux - !# :3? 是什么意思

bash - Unix - 按文件名过滤查找结果

c++ - 将以 execv 启动的进程重定向到/dev/null

python - Python 2.7(enum34)中的枚举类型检查?