我希望我可以通过我试图整合的脚本获得一些社区帮助。我仍然在这里学习,所以我很容易犯愚蠢的错误。
当我挂载 USB 磁盘时,我正在尝试实现一些自动化。本质上,我希望复制一些基本文件夹,并在几个文件夹上放置一个 inotify 手表,这些文件夹将移动文件并在移动时删除空目录。
一切正常 - 除了当我尝试在所有这些中运行会自动打开终端以便我可以输入密码的命令时,终端不会使用 systemd 打开。
除了随机环境变量和脚本上的更改之外,似乎没有很多关于此的在线信息,我认为我已经尝试过足够的时间来寻求帮助。
详细信息和错误如下:
使用了几个脚本:
Systemd 在系统上安装 USB 驱动器时触发:
元素-usb.service
[Unit]
Description=elements-usb-trigger
Requires=home-usb\x2dstorage.mount
After=home-usb\x2dstorage.mount
[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"
ExecStart=/home/monster/.storage-scripts/elements-run-on-mount-SHELL
ExecStop=systemctl stop elements-usb.service
[Install]
WantedBy=graphical.target
WantedBy=home-usb\x2dstorage.mount
Execstart 指向这段代码,也就是应该启动终端的地方:
/home/monster/.storage-scripts/elements-run-on-mount-SHELL
#!/bin/bash
gnome-terminal -e "/bin/bash -c '/home/monster/.storage-scripts/elements-run-on-mount;exec $SHELL'"
这将执行下面的脚本...
#!/bin/bash
###--Move existing files to USB--###
#tv folder mover initial sync
rsync -avhP --remove-source-files /home/monster/Videos/htpc-server-tv/ /home/usb-storage/Video/tv
notify-send "R-Sync says" "Moving tv shows from tv shows"
find /home/monster/Videos/htpc-server-tv/ -type f -empty -exec rm {} \;
notify-send "TV folder says" "Cleaned :)"
#movies folder mover initial sync
rsync -avhP --remove-source-files /home/monster/Videos/htpc-server-movies/ /home/usb-storage/Video/movies
notify-send "R-Sync says" "Moving movies to movies folder"
find /home/monster/Videos/htpc-server-movies/ -type f -empty -exec rm {} \;
notify-send "Movie folder says" "Me too :)"
#R-Sync timeshift to usb copy
sudo rsync -avhP /home/timeshift/ /home/usb-storage/Backup/monster-laptop/timeshift --info=stats2&
notify-send "R-Sync says" "Timeshift backup of backup taking off"
#virtual server sync - htpc server to usb copy
sudo rsync -avhP /home/virtualbox/htpc-server/ /home/usb-storage/Backup/virtual servers/htpc-server --info=stats2&
notify-send 'R-Sync says' 'htpc-server backup of backup underway :)'
#virtual server sync - crypto server to usb copy
sudo rsync -avhP /home/virtualbox/crypto-server/ /home/usb-storage/Backup/virtual servers/crypto-server --info=stats2&
notify-send 'R-Sync says' 'crypto-server backup of backup underway :)'
####--WATCH FOLDERS--####
#NOTE: Requires inotify tools "sudo apt-get install inotify-tools"
#tv - move new videos to USB drive videos folder
sh /home/monster/.storage-scripts/inotify-script-watchenmove-videos-tv&
notify-send "R-Sync says" "Boom! tv"
#movie - move new videos to USB drive videos folder
sh /home/monster/.storage-scripts/inotify-script-watchenmove-videos-movies&
notify-send "R-Sync says" "Bam! - movie"
#Bind USB drive folders to home/videos/.usb-Videos to present to virtual machine
#sudo mount --bind '/home/usb-storage/Video' '/home/monster/Videos/.usb-Videos'&
当作为脚本运行时,这一切似乎运行良好,但当由 systemd 启动时,它似乎失败得很严重。
systemctl status elements-usb.service 的结果
● elements-usb.service - elements-usb-trigger
Loaded: loaded (/etc/systemd/system/elements-usb.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2019-08-24 11:20:51 BST; 31s ago
Main PID: 13044 (code=exited, status=1/FAILURE)
Aug 24 11:20:51 monster-laptop systemd[1]: Started elements-usb-trigger.
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Option “-e” is deprecated and might be removed in a later version
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Use “-- ” to terminate the options and put the command line to ex
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: No protocol specified
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: Unable to init server: Could not connect: Connection refused
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Failed to parse arguments: Cannot open display:
Aug 24 11:20:51 monster-laptop systemd[1]: elements-usb.service: Main process exited, code=exited, status=1/FAILURE
Aug 24 11:20:51 monster-laptop systemd[1]: elements-usb.service: Failed with result 'exit-code'.
**似乎由于启动终端的一些图形/GUI问题而失败。
如果 systemd 进程单独启动脚本,即 /home/monster/.storage-scripts/elements-run-on-mount
而不是:
gnome-terminal -e "/bin/bash -c '/home/monster/.storage-scripts/elements-run-on-mount;exec $SHELL'"
它在安装 USB 驱动器时运行良好,但没有关于它的视觉提示,并且某些 R-Sync 功能可能需要一些时间。
关于在脚本中使用 sudo 可能还有一个问题,因为 systemd“我认为”无论如何都会以 root 身份运行脚本,并且可能还需要进行一些调整。
journalctl -xe 的输出
Aug 24 11:27:50 monster-laptop gjs[5062]: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
Aug 24 11:27:50 monster-laptop gjs[5062]: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
Aug 24 11:28:48 monster-laptop gnome-shell[4130]: [night-light-slider] Setting night light schedule from 5 to 17
Aug 24 11:30:01 monster-laptop CRON[17646]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 24 11:30:01 monster-laptop CRON[17647]: (root) CMD ([ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/
Aug 24 11:30:01 monster-laptop CRON[17646]: pam_unix(cron:session): session closed for user root
Aug 24 11:31:15 monster-laptop org.gnome.Shell.desktop[4130]: [9934:9934:0824/113115.982679:ERROR:buffer_manager.cc(488)] [.DisplayCom
Aug 24 11:32:04 monster-laptop systemd[1]: Started Run anacron jobs.
-- Subject: A start job for unit anacron.service has finished successfully
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- A start job for unit anacron.service has finished successfully.
--
-- The job identifier is 6098.
Aug 24 11:32:04 monster-laptop anacron[18538]: Anacron 2.3 started on 2019-08-24
Aug 24 11:32:04 monster-laptop anacron[18538]: Normal exit (0 jobs run)
Aug 24 11:32:04 monster-laptop systemd[1]: anacron.service: Succeeded.
-- Subject: Unit succeeded
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The unit anacron.service has successfully entered the 'dead' state.
lines 1747-1769/1769 (END)
所以,请各位大侠帮忙——你能先帮我修一下我的剧本吗?
此外,虽然我在终端启动方面寻求直接帮助 - 如果您发现有改进,我很乐意听取您的建议。
谢谢
● elements-usb.service - elements-usb-trigger
Loaded: loaded (/etc/systemd/user/elements-usb.service; static; vendor preset: enabled)
Active: inactive (dead)
Aug 26 08:58:58 monster-laptop systemd[5099]: Starting elements-usb-trigger...
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # Option “-e” is deprecated and might be removed in a later versio
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # Use “-- ” to terminate the options and put the command line to e
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # _g_io_module_get_default: Found default implementation gvfs (GDa
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # _g_io_module_get_default: Found default implementation dconf (DC
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # watch_fast: "/org/gnome/terminal/legacy/" (establishing: 0, acti
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # unwatch_fast: "/org/gnome/terminal/legacy/" (active: 0, establis
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # watch_established: "/org/gnome/terminal/legacy/" (establishing:
Aug 26 08:58:59 monster-laptop systemd[5099]: elements-usb.service: Succeeded.
Aug 26 08:58:59 monster-laptop systemd[5099]: Started elements-usb-trigger.
TBH 不知道从哪里开始。有任何想法吗?
systemctl
下面概述的服务单元文件适用于我的 Ubuntu 18.04 桌面(systemd 237),即当我使用命令手动启动它时,它会打开一个终端窗口。当然,要根据您的需要调整它(usb-mount 触发器,调用脚本,..),您需要在各个地方使用您已经拥有的服务文件中的点点滴滴对其进行修改。正如您已经怀疑的那样,原始服务单元文件不起作用,因为导入环境变量和尝试打开新的 GUI 会话时出现问题,即新的 Gnome 终端窗口。
使其工作的最重要的变化:
您需要将服务单元作为 systemd用户服务运行或调用。
这是因为使用 systemd系统服务启动终端会话被认为是不安全的,在某些发行版中甚至被默认阻止。systemd 区分用户和系统单元文件的方式是按文件位置。例如,通过将服务单元文件(如下所示)放置在
/etc/systemd/user
将被识别systemd
为用户服务文件的目录中(有关可以使用哪些目录的更多信息,请参见systemd 手册页)。出于测试目的,您可以手动启动服务添加
ExecStartPre
指令以启用环境变量的导入。这只是一种方法。还有其他选项可能更适合您的情况(请参阅此处)。(可选)将服务运行为
Type=oneshot
. 这样它只会在触发时运行一次,并且您不需要ExecStop
指令。同样,这只是一种方法。请注意,似乎有一些正在筹备中的作品可能会在不久的(?)将来改变/简化事情。