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

2024-10-01 17:29:42 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我想在伪代码中执行的操作:

    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


Tags: to代码answertimeiscreatesleepprocess
1条回答
网友
1楼 · 发布于 2024-10-01 17:29:42

由于要共享的变量似乎是布尔值,因此可以根据是否存在临时文件来确定。你知道吗

以下代码的具体示例:

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

相关问题 更多 >

    热门问题