AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1237805
Accepted
Gregory Opera
Gregory Opera
Asked: 2020-05-11 03:41:18 +0800 CST2020-05-11 03:41:18 +0800 CST 2020-05-11 03:41:18 +0800 CST

帮助创建 Systemd 计时器以自动更改窗口主题

  • 772

我正在尝试让 Ubuntu 20.04 LTS(“Focal Fossa”)在每天早上 0600(早上 6 点)自动切换到“浅色”窗口主题,并在每晚 1800(下午 6 点)自动切换到“深色”窗口主题。

以下终端命令可用于更改为“轻”窗口主题:

gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

以下终端命令可用于更改为“深色”窗口主题:

gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark

但如上所述,我想自动化这个过程。

向我提出的第一个建议是通过 cron 作业,但是这屡次被证明是不成功的,所以另一个用户通过 Systemd “计时器”提出了一种更“现代”的方法......不幸的是,我不熟悉 Systemd 和创建过程计时器,所以我一直在学习,但迄今为止没有成功。

此时,我的“home”文件夹中有六个文件:

  • 黑暗服务
  • 暗计时器
  • 黑暗的.sh
  • 轻服务
  • 光定时器
  • 光.sh

“dark.service”的内容是:

[Unit]
Description=Automatically change the "Window Theme" to "dark" in the evening.

[Service]
ExecStart=/home/gregory/dark.sh

[Install]
WantedBy=dark.sh

dark.timer 的内容是:

[Unit]
Description=Automatically change the "Window Theme" to "dark" in the evening.

[Timer]
OnCalendar=*-*-* 18:00:00
Persistent=true

[Install]
WantedBy=dark.service

“dark.sh”的内容是:

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark

“light.service”的内容是:

[Unit]
Description=Automatically change the "Window Theme" to "light" in the morning.

[Service]
ExecStart=/home/gregory/light.sh

[Install]
WantedBy=light.sh

“light.timer”的内容是:

[Unit]
Description=Automatically change the "Window Theme" to "light" in the morning.

[Timer]
OnCalendar=*-*-* 06:00:00
Persistent=true

[Install]
WantedBy=light.service

“light.sh”的内容是:

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

我使用“启动应用程序首选项”(gnome-session-properties)在登录时运行“light.timer”和“dark.timer”。

根据我在其他地方得到的建议以及我在网上阅读的内容,我认为通过 Systemd 创建“计时器”可能是实现我想要的正确方法(在“浅色”和“深色”窗口主题之间自动切换,基于一天中的时间)...我只需要一点帮助才能让事情正常运行,因为 Systemd、计时器和脚本对我来说是一个全新的世界。

gnome themes systemd gnome-shell
  • 1 1 个回答
  • 589 Views

1 个回答

  • Voted
  1. Best Answer
    meuh
    2020-05-12T04:39:45+08:002020-05-12T04:39:45+08:00

    (后期编辑WantedBy=default.service至WantedBy=default.target)

    这可能是一个漫长的旅程,但这里有一些建议。所有的WantedBy条目都是错误的。它们不能依赖于 shell 脚本。一个计时器单元x.timer总是与一个服务单元相关联x.service。如果您start使用计时器,他们将在给定时间开始服务。但是您需要在每次登录时执行此操作。因此,您需要启用计时器,并让 systemd 自动启动它们。为此,您需要让计时器WantedBy=成为在用户登录时实现的目标。通常,这是default.target. 尽管有很多系统目标,但用户目标却很少。

    由于您的脚本非常短,您可能希望将它们包含在服务单元中,以使内容更小。服务和计时器文件必须在 ~/config/systemd/user/ 中。如果此目录不存在,您需要创建它。例如:dark.service:

    [Unit]
    Description=Automatically change the "Window Theme" to "dark" in the evening.
    [Service]
    Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
    ExecStart=/usr/bin/gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark
    

    黑暗计时器:

    [Unit]
    Description=Automatically change the "Window Theme" to "dark" in the evening.
    [Timer]
    OnCalendar=*-*-* 18:00:00
    Persistent=true
    [Install]
    WantedBy=default.target
    

    灯光服务:

    [Unit]
    Description=Automatically change the "Window Theme" to "light" in the morning.
    [Service]
    Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
    ExecStart=/usr/bin/gsettings set org.gnome.desktop.interface gtk-theme Yaru-light
    

    光定时器:

    [Unit]
    Description=Automatically change the "Window Theme" to "light" in the morning.
    [Timer]
    OnCalendar=*-*-* 06:00:00
    Persistent=true
    [Install]
    WantedBy=default.target
    

    每当您更改这些文件的内容,或从该目录添加或删除文件时,您需要通过给出以下命令通知 systemd:

    systemctl --user daemon-reload
    

    我不确定 gnome“启动应用程序首选项”的作用。开始使用这些单元的 systemd 方法是只做一次,而不是作为 root:

    systemctl --user enable dark.timer light.timer
    

    现在,每当您登录时,这些单元将启动,并在您注销时停止。


    在开发单元时,您可能希望立即测试它们,而无需登录/注销。您可以使用以下命令显式启动它们:

    systemctl --user start dark.timer light.timer
    

    你可以用

    systemctl --user stop dark.timer light.timer
    

    您需要先停止它们,然后才能再次启动它们。您可以结合停止和开始

    systemctl --user restart dark.timer light.timer
    

    你可以检查他们的状态

    systemctl --user list-timers
    

    这应该说明他们下一次触发的时间,以及他们将运行什么服务。

    有一个关于 systemd用户单元的教程会很好,但我还没有找到任何合适的东西。他们主要谈论系统单位。您可以从这个大的redhat文档中收集一些基础知识,但它是针对管理员的,所以有点过于详细,但有助于理解其中一些对于用户单元是通用的。

    • 2

相关问题

  • 命令列出启动时启动的服务?

  • 如何更改 GDM 中的登录屏幕主题?

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 如何获取和安装更多主题、图标和指针?

  • 如果顶部面板中缺少会话小程序,如何注销?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve