在启动时启动应用程序

Har*_*ton 4 linux systemd xfce4 xfce4-terminal

任务:

\n\n

启动时启动 xfce4-clipman

\n\n

尝试过:

\n\n

我在正确的位置创建了一个具有正确权限的 sh 脚本:

\n\n
martin@martin:/etc/init.d$ ls -l start_clipman.sh \n-rwxrwxr-x 1 root root 26 \xd0\xbc\xd0\xb0\xd1\x80 12 09:05 start_clipman.sh\n
Run Code Online (Sandbox Code Playgroud)\n\n

sh文件内容:

\n\n
martin@martin:/etc/init.d$ cat start_clipman.sh \n#!/bin/bash\nxfce4-clipman\nmartin@martin:/etc/init.d$ \n
Run Code Online (Sandbox Code Playgroud)\n\n

我跑了

\n\n
martin@martin:/etc/init.d$ sudo update-rc.d start_clipman.sh \ndefaultsinsserv: warning: script 'K01mount_disk.sh' missing LSB tags and overrides\ninsserv: warning: script 'K01start_clipman.sh' missing LSB tags and overrides\ninsserv: warning: script 'start_clipman.sh' missing LSB tags and overrides\ninsserv: warning: script 'mount_disk.sh' missing LSB tags and overrides\n
Run Code Online (Sandbox Code Playgroud)\n\n

潜在问题:

\n\n

当我这样做时:

\n\n
martin@martin:~$ xfce4-clipman\n
Run Code Online (Sandbox Code Playgroud)\n\n

应用程序正在运行,但 xfce-clipman 正在占用终端,它正在执行,它正在工作,但如果我想使用终端输入其他内容,我必须取消它。

\n\n
martin@martin:~$ xfce4-clipman\n^C\nmartin@martin:~$ \n
Run Code Online (Sandbox Code Playgroud)\n\n

我该怎么办

\n

use*_*686 6

xfce4-clipman 需要访问您的 Xorg 显示(X11 图形系统)\xe2\x80\x93,不仅因为它确实是一个图形应用程序,还因为它是一个剪贴板管理器,X11 也涵盖了这一点。

\n\n

(主要)问题:您的 Xorg 显示在启动时实际上还不可用。它在系统启动完成很久之后在登录时启动。因此,不可能在“启动时”\xe2\x80\x93 启动 xfce4-clipman,您真正想要的是在登录时启动该应用程序。

\n\n

(Linux 是为了支持多个用户而构建的,他们可以随时登录\xc2\xa0in 和登录\xc2\xa0out \xe2\x80\x93 每个用户都会获得运行的 Xorg 的全新副本,并且登录屏幕本身会获得一个作为好吧。所以服务根本不能指望 Xorg 可用。 )

\n\n

登录时启动应用程序

\n\n

大多数桌面环境,包括 Xfce,都可以通过*.desktop文件启动“登录”任务(这确实是各种 Xfce 组件的启动方式)。

\n\n

他们的目的是/etc/xdg/autostart/提供全球服务和~/.config/autostart/个人服务。创建后,默认情况下它是“启用”的,但仍然可以通过 禁用xfce4-session-settings

\n\n

事实上,甚至 xfce4-clipman 也将自己的自动启动文件安装到 /etc/xdg/autostart \xe2\x80\x93 中,它的名称是xfce4-clipman-plugin-autostart.desktop. 只要您使用 Xfce4(由于OnlyShowIn=XFCE线路原因),它就应该自动工作。

\n\n

但是,如果您的系统缺少该文件,或者您想在非 Xfce 桌面环境中使用 xfce4-clipman,那么您可以创建一个新文件。自动启动.desktop文件通常如下所示:

\n\n
\n[桌面条目]\n类型=应用程序\n名称=剪贴板管理器\nExec=xfce4-clipman\n终端=false\n
\n\n

手动启动非 CLI 程序

\n\n

许多桌面环境都有一个“运行”对话框AltF2,它可以让您在不占用终端的情况下运行程序。

\n\n

终端执行相同操作的各种方法包括:

\n\n
    \n
  • (setsid xfce4-clipman 2>/dev/null &)
  • \n
  • (xfce4-clipman &)
  • \n
  • nohup xfce4-clipman &
  • \n
  • xfce4-clipman & disown
  • \n
  • 等等。
  • \n
\n\n

init.d 脚本的其他问题

\n\n

在系统服务合适的情况下,您应该记住 /etc/init.d 中的文件不仅仅是简单的脚本,它们也在关闭时运行,并且必须接受诸如“stop”或“restart”之类的子命令。当系统调用 时/etc/init.d/your_service stop,initscript 需要实际停止服务 \xe2\x80\x93,而不是再次启动它!

\n\n

您已使用标记了问题,那么为什么不为自己省去很多麻烦并编写一个本机 systemd*.service文件呢?虽然“正确的”init.d 脚本可以填满多个屏幕,但 systemd .services 通常不到十几行。

\n\n

更重要的是:有数十个服务在不同的时间点启动。在后期阶段,可以使用一些早期阶段没有的设施。(例如,网络。)

\n\n

如果您的初始化脚本没有明确说明其顺序要求(“必须在 Y 之前运行,但在 Z 之后运行”),操作系统将在不可预测的阶段运行它,与其他所有内容并行。如果你很幸运,它会在正确的时刻运行 \xe2\x80\x93 但大多数时候它会运行得太早,无法正常工作。

\n\n

Before=在本机 systemd 单元文件中,使用和参数指定顺序After=Requires=(使用或指定依赖项也是一个好主意Wants=。)同时,init.d 脚本(包含 SysVinit 和 systemd)使用标记为 的特殊注释块### BEGIN INIT INFO,并带有诸如Required-Start:和 之类的参数Should-Start:

\n