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 / 问题

问题[inotify](ubuntu)

Martin Hope
C. Cristi
Asked: 2020-10-31 11:14:39 +0800 CST

将脚本 bash 变量添加到 awk 和 inotify

  • 1

我想创建一个脚本,该脚本将记录一天中对该目录的每次访问或该目录中的任何文件,为此,我使用 inotifywait,但即使我格式化了输出,我也不喜欢它,我需要用户也访问/修改了文件。我想以表格格式打印它。像这样的东西:

TIME               USER     FILE            EVENT
%mm:%HH PM/am      root     /home/root/x    Accesed(or anything the inotifywait gives)

我尝试了这样的事情:

#!/bin/sh

watchedDir=$1
logFileName="$(date +'%d.%m.%Y').log"

iwait() {
    inotifywait -r -m --timefmt "%Y/%m/%d %H:%M:%S" --format "%T;%w%f;%e" $watchedDir >> "$PWD/.$logFileName.tmp"
}

write_to_file() {
    while true; do
    last_entry=$(tail -n 1 "$PWD/$logFileName.tmp")
    time=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f1 -d';')
    user=$(stat $last_entry --format="%U")
    file=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f2 -d';')
    event=$(tail -n 1 "$PWD/$logFileName.tmp" | cut -f3 -d';')

    awk -v time="$time" -v user="$user" -v file="$file" -v event="$event" 'BEGIN {printf("%s %8s %8s %8s \n" ,"Time", "User", "File", "Event")}
    {printf("%s %s %s %s\n", time, user, file, event)}' >> "$PWD/.$logFileName.tmp"
    done
}

if [ "$(realpath $watchedDir)" != "$PWD" ]
then
    iwait &
    write_to_file &
    wait
fi

我还发现,如果我尝试查看当前目录并将文件重定向到当前目录,它将淹没输出......所以我尝试使用它来克服它if。

我怎么能做这样的事情?

scripts command-line bash inotify
  • 1 个回答
  • 326 Views
Martin Hope
DK Bose
Asked: 2019-09-01 05:50:00 +0800 CST

为什么 inotifywait 创建名称中带有“swp”或 goutputstream 的文件?

  • 2

我正在尝试了解inotifywait。我安装了inotify-tools:

Start-Date: 2019-08-31  18:11:48
Commandline: apt install inotify-tools
Requested-By: dkb (1000)
Install: inotify-tools:amd64 (3.14-7), libinotifytools0:amd64 (3.14-7, automatic)
End-Date: 2019-08-31  18:11:58

我制作了这个脚本jl.sh ,将~/Desktop/vvtemp中的任何修改文件复制到~/Public并为复制的文件添加时间戳:

#!/bin/bash
dir_to_watch="/home/dkb/Desktop/vvtemp/"

inotifywait -m -e modify --format '%w%f' "${dir_to_watch}" | while read line
do
s=$line
     newname=/home/dkb/Public/$(date +%Y%m%d%H%M%S)"-${s##*/}"
     cp $line $newname
done

 

  • 我创建了一个名为test.txt的文件并在一个终端中启动了脚本。没有错误或警告。

  • 在另一个终端中,我用nano打开test.txt,对其进行编辑,保存并关闭它一次。

  • 然后我使用gedit和geany编辑了同一个文件

我查看了目标文件夹~/Public中的文件列表:

dkb@Udin:~/Public$ ll
total 36
drwxr-xr-x  2 dkb dkb 4096 Aug 31 18:27 ./
drwxr-xr-x 17 dkb dkb 4096 Aug 31 18:08 ../
-rw-r--r--  1 dkb dkb 1024 Aug 31 18:24 20190831182416-.test.txt.swp
-rw-r--r--  1 dkb dkb 1024 Aug 31 18:24 20190831182419-.test.txt.swp
-rw-r--r--  1 dkb dkb  159 Aug 31 18:24 20190831182441-test.txt
-rw-r--r--  1 dkb dkb  179 Aug 31 18:26 20190831182619-.goutputstream-QN1A7Z
-rw-r--r--  1 dkb dkb  198 Aug 31 18:26 20190831182638-.goutputstream-UDEB7Z
-rw-r--r--  1 dkb dkb  210 Aug 31 18:27 20190831182722-.goutputstream-0X2K7Z
-rw-r--r--  1 dkb dkb  221 Aug 31 18:27 20190831182733-.goutputstream-01D86Z
dkb@Udin:~/Public$ 
  • 前三个文件与使用nano编辑和保存test.txt有关。前两个文件有一个.swp后缀。第三个文件是具有预期时间戳文件名的预期文件。

  • 然后我尝试使用gedit或geany编辑同一个文件。这些编辑器创建了备份文件,每次我修改和保存文件时都会创建一个,但生成的备份虽然具有预期的内容,但文件名类似于20190831182733-.goutputstream-01D86Z。因此,似乎时间戳得到了尊重,但“基本名称”却没有。

 

我的问题是:

  • 为什么我使用nano时inotifywait创建了两个.swp文件?

  • 为什么我使用gedit或geany时没有生成预期的名称?

scripts command-line bash inotify
  • 1 个回答
  • 603 Views
Martin Hope
Fredfree
Asked: 2019-08-25 02:38:39 +0800 CST

Systemd / Ubuntu 19.04 / 似乎无法获得 systemd 脚本来启动终端

  • 0

我希望我可以通过我试图整合的脚本获得一些社区帮助。我仍然在这里学习,所以我很容易犯愚蠢的错误。

当我挂载 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 不知道从哪里开始。有任何想法吗?

gnome-terminal usb-drive systemd rsync inotify
  • 1 个回答
  • 894 Views
Martin Hope
current_user
Asked: 2018-08-24 03:35:50 +0800 CST

sysctl:密钥“fs.inotify.max_user_watches”的权限被拒绝

  • 2

我有一个 Ubuntu 16.04 LTS (GNU/Linux 2.6.32-042stab127.2 x86_64) VPS 在运行我的 rails 应用程序时出现以下错误

致命:监听错误:无法监视目录的更改。访问 https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers 了解如何解决这个问题。

我正在尝试通过将监视限制增加

  $ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
  $ sudo sysctl -p

它抛出另一个错误

sysctl:密钥“fs.inotify.max_user_watches”的权限被拒绝

我对服务器具有 root 访问权限。

任何增加限制的帮助都非常感谢。提前致谢!

server 16.04 inotify
  • 2 个回答
  • 5084 Views
Martin Hope
fixit7
Asked: 2018-01-11 10:33:15 +0800 CST

停止 inotifywait 实例

  • 5

我制作了一个脚本来调用inotifywait. 它工作正常,但我有时想停止它。

如何停止最后一个inotifywait实例?

我无法理解如何使用inotify_rm_watch我理解的用于关闭它。

7341 ?        S      0:00 inotifywait -m /home/andy/Downloads/ --format %w
inotify
  • 4 个回答
  • 5515 Views

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