我希望社区能帮助我编写一个脚本。我还在学习,所以很容易犯愚蠢的错误。
我试图在安装 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.
老实说,不知道接下来该怎么做。有什么想法吗?
答案1
下面列出的服务单元文件适用于我的 Ubuntu 18.04 桌面(systemd 237),也就是说,当我使用命令手动启动它时,它会打开一个终端窗口systemctl
。当然,为了根据您的需要进行调整(usb-mount 触发器、调用脚本等),您需要使用您已有的服务文件中的零碎内容在各个地方对其进行修改。
正如您已经怀疑的那样,原始服务单元文件不起作用,因为导入环境变量和尝试打开新的 GUI 会话(即新的 Gnome 终端窗口)时出现问题。
使其发挥作用的最重要的变化是:
您需要以 systemd 形式运行或调用服务单元用户服务。
这是因为使用 systemd 启动终端会话系统服务被视为不安全,在某些发行版中甚至默认被阻止。systemd 区分用户和系统单元文件是通过文件位置来识别的。例如,通过将服务单元文件(如下所示)放置在/etc/systemd/user
将被识别systemd
为用户服务文件(有关可以使用哪些目录的更多信息,请参阅systemd 手册页)。为了测试目的,你可以手动启动服务systemctl --user start servicefilename.service
添加
ExecStartPre
指令以启用环境变量的导入。这只是一种方法。还有其他选项可能更适合您的情况(请参阅这里)。(可选)以 身份运行服务
Type=oneshot
。这样,它只会在触发时运行一次,并且您不需要指令ExecStop
。同样,这只是一种方法。[Unit] Description=elements-usb-trigger Requires=default.target After=default.target [Service] Type=oneshot ExecStartPre=/bin/systemctl --user import-environment ExecStart=/usr/bin/gnome-terminal -e "/bin/bash -c 'echo hello;exec $SHELL'" [Install] \# optional dependencies
请注意,似乎有一些作品在不久的将来可能会改变/简化事情。