【问题标题】:How do I end spawned shell script in?如何结束生成的 shell 脚本?
【发布时间】:2021-10-29 13:04:44
【问题描述】:

我正在编写我的第一个 gnome shell 扩展程序,通过制作和运行 shell 脚本将桌面背景更新为 Microsoft Daily Wallpaper 这是 shell 脚本,它每 15 分钟循环一次

while :
do
result=$(curl -s -X GET --header "Accept: */*" "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
regex="th.+\.jpg"
if [[ $result =~ $regex ]]
then
    gsettings set org.gnome.desktop.background picture-uri "http://bing.com/${BASH_REMATCH[0]}"
fi
sleep 900
done

这里是extension.js文件,我做了上面的shell脚本,然后运行它

const Util = imports.misc.util;

const loc = "~/.local/share/gnome-shell/extensions/bingwall@anb1142.io/bing_wallpaper.sh";
function init() {
    const command = `printf 'while :\ndo\nresult=$(curl -s -X GET --header "Accept: */*" "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")\nregex="th.+\.jpg"\nif [[ $result =~ $regex ]]\nthen\n    gsettings set org.gnome.desktop.background picture-uri "http://bing.com/\${BASH_REMATCH[0]}"\nfi\nsleep 900\ndone\n' > ${loc}`;
    Util.spawn(["/bin/bash", "-c", command]);
    Util.spawn(["/bin/bash", "-c", `chmod +rwx ${loc}`]);
}
function enable() {
    Util.spawn(["/bin/bash", "-c", loc]);
}

function disable() {
    // stop that script
}

这里有问题。我知道如何启动它,但我不知道如何打破该循环或终止脚本。我希望在禁用时发生这种情况。我怎么做 ?提前致谢

【问题讨论】:

    标签: gnome-shell gnome-shell-extensions


    【解决方案1】:

    首先,值得指出的是,您不应该在 shell 脚本中执行任何这些操作。 libsoupGSettings 的组合将允许您在不阻塞 GNOME Shell 的主线程或生成 shell 脚本的情况下做您想做的事情。

    gjs.guide 上有一个教程,描述了如何生成和控制Subprocesses in GJS。基本上,可以使用Gio.Subprocess.new() 生成进程,并通过在返回的对象上调用force_exit() 来停止:

    const {Gio} = import.gi;
    
    try {
        // If no error is thrown, the process started successfully and
        // is now running in the background
        let proc = Gio.Subprocess.new(['ls'], Gio.SubprocessFlags.NONE);
    
        // At any time call force_exit() to stop the process
        proc.force_exit();
    } catch (e) {
        logError(e, 'Error executing process');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多