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

时间:2013-05-23 12:08:11

标签: 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

1 个答案:

答案 0 :(得分:1)

由于您要共享的变量似乎是一个布尔值,您可以根据临时文件的存在与否来确定。

具有以下代码的具体示例:

#!/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