gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:22 25 4
gpt4 key购买 nike

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

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com