1. Autostarting 2. 開機啟動 2.1 On bootup / shutdown (systemd) 2.2 On user login / logout (systemd/user) 2.3 On shell login / logout (/etc/profile) 2.4 On Xorg startup (/etc/xprofile) 2.5 On desktop environment startup (/etc/xdg/autostart; ~/.config/autostart) 2.6 On window manager startup (Ex: ~/.config/i3/config) 2.7 non-login shell (~/.bashrc) 3. 運行過程中自動運行 3.1 On device plug in / unplug (udev) 3.2 On time events 定時事件: (systemd/timers) 3.3 On filesystem events (incron)
1. Autostarting
https://wiki.archlinux.org/index.php/Autostarting
某種事件發生時自動啟動腳本或應用程序. 包括開機過程中自動啟動, 運行過程中自動啟動.
power on | 本機 | 啟動時的用戶界面 | 用戶操作 | 備注 | |
固件: POST自檢; 初始化硬件; |
BIOS | POST自檢過程, | (可選) 進入COMS設置; (可選) 選擇啟動媒介 |
Firmware 固件存儲在主板本身的閃存中 | |
主引導記錄 | MBR | 存儲在硬盤(光盤,U盤)上 | |||
引導加載程序 | GRUB | 引導界面 | (可選) 選擇啟動OS | 多系統引導 | |
內核加載,啟動 | Kernel | 非常多的快速加載的條目... 也有些系統配置為隱藏部分或全部... |
|||
早期用戶空間, | initramfs | initramfs也被稱為早期用戶空間 | |||
2.1 | 啟動腳本 初始化 | systemd | systemd | 系統總管 systemd. Service 守護進程 | |
getty | 如果系統上存在“ getty”,則可以啟動顯示管理器。 | ||||
Display manager | lightdm | 登陸窗口 | (必須) 輸入密碼 | 可以將顯示管理器配置為替換tty上的getty登錄提示。 | |
2.2 | Login | User | systemd/User | 驗證/etc/passwd; 設置環境變量... | |
etc/motd | 消息 | /etc/motd | 顯示/etc/motd的內容(當天的消息) | ||
2.3 | Login shell | bash | /etc/profile ~/.profile |
Ctl: /etc/profile; ~/.bash_profile\.bash_login\.profile GUI: 啟動用戶會話; /etc/profile, ~/.profile |
|
2.4 | xorg startup startX |
xorg | ~/.xinitrc ~/.xprofile |
xinit or wayland, 啟動WM. ~/.xprofile和/etc/xprofile在X用戶會話的開始-在啟動WM之前執行命令。 | |
2.5 | Desktop environment | xfce | Linux 桌面 | ~/.config/autostart /etc/xdg/autostart |
[Applications Menu > Settings > Settings Manager] [Session and Startup]option\ tab[Application Autostart] |
2.6 | Window manager | xfwm4 | if fluxbox: ~/.fluxbox/startup ...; if Openbox: ~/.config/openbox/autostart ...; if Awesome: ~/.config/awesome/rc.lua ...; if i3: ~/.config/i3/config … |
||
2.7 | non-login shell | fish | 桌面終端 | ~/.bashrc | 通常會運行配置文件,例如bashrc。 |
3.1 device plug 配置 udev 規則 開機時硬件加載, 運行過程中插入新設備 3.2 time events systemd/Timers 也可以使用傳統的 crom, at 3.3 fs events inotify, incron 各種文件事件
2. 開機啟動
2.1 On bootup / shutdown (systemd)
開機/關機: 使用系統服務 systemd
https://wiki.archlinux.org/index.php/Systemd
2.2 On user login / logout (systemd/user)
用戶登陸/注銷: Use systemd/User services.
https://wiki.archlinux.org/index.php/Systemd/User
2.3 On shell login / logout (/etc/profile)
在shell登錄/注銷 See Command-line shell#Configuration files.
https://wiki.archlinux.org/index.php/Command-line_shell#Configuration_files
要在控制台中或登錄時自動啟動程序,可以使用Shell啟動文件/目錄。閱讀您的Shell的文檔,或閱讀其ArchWiki文章,例如Bash#Configuration文件或Zsh#Startup / Shutdown文件。
https://wiki.archlinux.org/index.php/Bash#Configuration_files
https://en.wikipedia.org/wiki/Unix_shell#Configuration_files
- /etc/profile : 登錄后,所有兼容Bourne的shell都將獲取/etc/profile文件,而/etc/profile文件又將獲取/etc/profile.d/中任何可讀的* .sh文件:這些腳本不需要解釋器指令,也不需要可執行文件。它們用於設置環境和定義特定於應用程序的設置。
- ~/.bash_profile : Per-user, after /etc/profile. 如果此文件不存在,則按該順序檢查〜/.bash_login和〜/.profile。骨架文件/etc/skel/.bash_profile也來自〜/.bashrc。
- ~/.bash_logout : After exit of a login shell.
- /etc/bash.bashrc : Depends on the -DSYS_BASHRC="/etc/bash.bashrc" compilation flag. Sources /usr/share/bash-completion/bash_completion.
- ~/.bashrc : Per-user, after /etc/bash.bashrc.
2.4 On Xorg startup (/etc/xprofile)
在Xorg啟動時
xinitrc (如果要使用xinit手動啟動Xorg)。
https://wiki.archlinux.org/index.php/Xinit#xinitrc
https://wiki.archlinux.org/index.php/Xinit
https://wiki.archlinux.org/index.php/Xorg
手動啟動X
$ startx
$ startx /usr/bin/i3
登陸時自動啟動X
對於Bash, 將以下內容添加到 ~/.bash_profile 的底部。如果文件不存在,請從以下位置復制框架版本 /etc/skel/.bash_profile
if systemctl -q is-active graphical.target && [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then exec startx fi
在桌面環境/窗口管理器之間切換
如果您經常在不同的桌面環境或窗口管理器之間進行切換,則可以使用顯示管理器或進行擴展~/.xinitrc以方便切換。
以下示例顯示如何使用參數啟動特定的桌面環境或窗口管理器:
~/.xinitrc ... # Here Xfce is kept as default session=${1:-xfce} ca se $session in i3|i3wm ) exec i3;; kde ) exec startplasma-x11;; xfce|xfce4 ) exec startxfce4;; # No known session, try to run it as command * ) exec $1;; esac
傳遞參數會話:
$ xinit session
or
$ startx ~/.xinitrc session
>>> xprofile (如果使用顯示管理器)。
https://wiki.archlinux.org/index.php/Xprofile
xprofile文件~/.xprofile和/etc/xprofile允許您在X用戶會話的開始-在啟動窗口管理器之前執行命令。
xprofile文件的樣式類似於xinitrc。
xprofile文件由以下顯示管理器本地提供:
- GDM -/etc/gdm/Xsession
- LightDM -/etc/lightdm/Xsession
- LXDM -/etc/lxdm/Xsession
- SDDM -/usr/share/sddm/scripts/Xsession
https://wiki.archlinux.org/index.php/Display_manager
2.5 On desktop environment startup (/etc/xdg/autostart; ~/.config/autostart)
桌面環境啟動時: 大多數桌面環境都實現XDG Autostart。
https://wiki.archlinux.org/index.php/Desktop_environment
https://wiki.archlinux.org/index.php/XDG_Autostart
所述XDG自動啟動規范定義了一個方法自動啟動普通桌面條目上的桌面環境的啟動和可移動介質的安裝,通過將它們放置在特定#Directories。
按優先順序排列的自動啟動目錄為:
- 特定於用戶的:$XDG_CONFIG_HOME/autostart(~/.config/autostart默認情況下)
- 全系統:$XDG_CONFIG_DIRS/autostart(/etc/xdg/autostart默認情況下)
系統范圍的桌面條目可以由具有相同文件名的用戶特定條目覆蓋。
要禁用系統范圍的條目,請創建一個包含的覆蓋條目Hidden=true。
如果桌面環境中有文章,請參見其自動啟動部分。
Xfce#Autostart: https://wiki.archlinux.org/index.php/Xfce#Autostart
click the [Applications Menu > Settings > Settings Manager]
and then choose the [Session and Startup] option
and click the tab [Application Autostart].
Autostart applications are stored as [name.desktop] in ~/.config/autostart/.
或者,將希望運行的命令(包括設置環境變量)添加到xinitrc(或使用顯示管理器時的xprofile)。
Tip: Sometimes it might be useful to delay the startup of an application.
Note that specifying under [Application > Autostart] a command such as (sleep 3 && command) does not work;
a workaround is to use the syntax (sh -c "sleep 3 && command")
GNOME#Autostart: https://wiki.archlinux.org/index.php/GNOME#Autostart
KDE#Autostart: https://wiki.archlinux.org/index.php/KDE#Autostart
LXDE#Autostart: https://wiki.archlinux.org/index.php/LXDE#Autostart
LXQt#Autostart: https://wiki.archlinux.org/index.php/LXQt#Autostart
2.6 On window manager startup (Ex: ~/.config/i3/config)
許多窗口管理器實現XDG Autostart。
https://wiki.archlinux.org/index.php/Window_manager
如果窗口管理器中有文章,請參見其自動啟動部分。
- Fluxbox#Autostart: https://wiki.archlinux.org/index.php/Fluxbox#Autostart
- ~/.fluxbox/startup文件是用於自動啟動應用程序以及啟動Fluxbox本身的腳本。#符號表示注釋。 確保在啟動Fluxbox本身之前調用啟動應用程序的所有行。 否則,直到Fluxbox本身終止后,這些行才會到達。
- Openbox#Autostart: https://wiki.archlinux.org/index.php/Openbox#Autostart
- sources /etc/xdg/openbox/environment
- sources ~/.config/openbox/environment
- runs /etc/xdg/openbox/autostart
- runs ~/.config/openbox/autostart
- Awesome#Autostart: https://wiki.archlinux.org/index.php/Awesome#Autostart
- 要實現XDG自動啟動規范,請在~/.config/awesome/rc.lua中添加以下行
- i3#Autostart: https://wiki.archlinux.org/index.php/I3#Autostart
- 在~/.config/i3/config文件中添加exec命令。例如: exec terminator
2.7 non-login shell (~/.bashrc)
桌面終端中使用命令操作, 打開終端窗口時通常會運行配置文件,例如bashrc。
3. 運行過程中自動運行
3.1 On device plug in / unplug (udev)
開機運行時加載硬件, 運行過程中插入或拔出設備,
https://wiki.archlinux.org/index.php/Udev
3.2 On time events 定時事件: (systemd/timers)
- Periodically at certain times, dates or intervals: 定期在某些時間,日期或間隔:
- systemd/Timers https://wiki.archlinux.org/index.php/Systemd/Timers
- Cron https://wiki.archlinux.org/index.php/Cron
- Once at a date and time: 在日期和時間一次:
- systemd/Timers
- at
3.3 On filesystem events (incron)
文件系統事件, 使用inotify事件觀察器: https://en.wikipedia.org/wiki/inotify
Inotify(inode通知)是一個Linux內核子系統,用於擴展文件系統以注意到文件系統的更改,並將這些更改報告給應用程序。它替代了具有類似目標的早期設施dnotify。Inotify可用於自動更新目錄視圖,重新加載配置文件,日志更改,備份,同步和上載。Inotifywait和inotifywatch命令允許從命令行使用inotify子系統。一種主要用途是在桌面搜索實用程序(例如Beagle)中,該實用程序的功能允許重新索引已更改的文件,而無需每隔幾分鍾掃描文件系統是否有更改,這將非常低效。
https://developer.ibm.com/tutorials/l-inotify/
您可以將inotify用於許多目的。這里有一些可能性:
性能監視: 您可能需要確定應用程序打開頻率最高的文件。如果發現反復打開和關閉一個小文件,則可以考慮使用內存版本或更改應用程序,以便以其他方式共享數據。
元信息: 您可能希望記錄有關文件的其他信息,例如原始創建時間或上次修改文件的用戶的ID。
安全: 性出於安全原因,您可能希望監視對特定文件或目錄的所有訪問。
https://developer.ibm.com/tutorials/l-ubuntu-inotify/
安裝inotify-tools套件
inotifywait只是阻塞等待inotify事件。您可以監視任何文件和目錄集,也可以監視整個目錄樹(目錄,其子目錄,子目錄等)。inotifywait在shell腳本中使用。
inotifywatch 收集有關受監視文件系統的統計信息,包括每個inotify事件發生了多少次。
因為Linux中的所有內容都是文件,所以無疑您會發現 inotify watches 的無數用途。
So, the real question is, “Who watches the watches?”
inotify-tools, see inotifywait(1) inotify-tools是一個C庫和一個Linux命令行程序集,提供了一個簡單的inotify接口。
incron https://wiki.archlinux.org/index.php/Incron
incron是一個守護程序,它監視文件系統事件並執行在系統表和用戶表中定義的命令。
安裝后,默認情況下將不啟用該守護程序。您可以啟用 incrond.service。
usr/bin/incrond, incrontab
usr/lib/systemd/system/incrond.service
usr/lib/sysusers.d/incrond.conf
usr/lib/tmpfiles.d/incrond.conf
配置: Incrontab絕對不能直接編輯;相反,用戶應使用incrontab程序來處理其incrontab。
$ incrontab --help
$ incrontab --help | inotify cron table manipulator | |
usage | incrontab [<options>] <operation> | |
incrontab [<options>] <FILE-TO-IMPORT> | ||
<operation> | may be one of the following: | 可能的操作是以下之一: |
-?, --about | gives short information about program | 提供有關程序的簡短信息 |
-h, --help | prints this help text | 打印此幫助文本 |
-l, --list | lists user table | 列出用戶表 |
-r, --remove | removes user table | 刪除用戶表 |
-e, --edit | provides editing user table | 提供編輯用戶表 |
-t, --types | list supported event types | 列出支持的事件類型 Mask type |
-d, --reload | request incrond to reload user table | 請求被入侵者重新加載用戶表 |
-V, --version | prints program version | 打印程序版本 |
These options may be used: | 可以使用以下選項: | |
-u <USER>, --user=<USER> | overrides current user (requires root privileges) | 覆蓋當前用戶(需要root特權) |
-f <FILE>, --config=<FILE> | overrides default configuration file (requires root privileges) | 覆蓋默認配置文件(需要root特權) |
incrontab文件中的每一行都是一個表,當某個目錄或文件發生事件時,dameon將運行該表。
incrontab的基本格式為: path mask command
- path是被入侵者監視更改的目錄或文件。
- mask是incrond將監視的文件系統事件的類型。該參數可以用逗號分隔。
- command是在指定的文件系統事件發生后要運行的命令。
Mask types
$ incrontab -t
Mask type | $ incrontab -t | ||
訪問 | IN_ACCESS | To trigger an command if a file is accessed or read: | 要在訪問或讀取文件時觸發命令: |
修改 | IN_MODIFY | To trigger a command if a file was modified: | 要在文件被修改時觸發命令: |
屬性 | IN_ATTRIB | To trigger an command if metadata of a file change (e.g. timestamps, permissons): | 如果文件的元數據發生更改(例如時間戳,許可),則要觸發命令: |
關閉 寫 | IN_CLOSE_WRITE | To trigger a command if a file opened for writing is closed: | 如果要打開的文件已關閉,則要觸發命令: |
IN_CLOSE_NOWRITE | To trigger a command if a file or directory not opened for writing is closed: | 如果未打開要寫入的文件或目錄已關閉,要觸發命令: | |
打開 | IN_OPEN | To trigger a command if a watched file or directory is opened: | 如果打開了監視的文件或目錄,則要觸發命令: |
移出 | IN_MOVED_FROM | To trigger a command if a file or directory is moved out of the watched directory: | 如果文件或目錄從監視目錄中移出,則要觸發命令: |
移入 | IN_MOVED_TO | To trigger a command if a file or directory is moved to the watched directory: | 要在文件或目錄移動到監視目錄時觸發命令: |
創建 | IN_CREATE | To trigger a command if a file or directory is created in a watched directory: | 如果在監視目錄中創建了文件或目錄,則要觸發命令: |
刪除 | IN_DELETE | To trigger a command if a file or directory is deleted from a watched directory: | 要從監視的目錄中刪除文件或目錄時觸發命令: |
刪除監視 | IN_DELETE_SELF | To trigger a command if a watched file or directory is deleted (or moved to a different filesystem): | 要在刪除監視文件或目錄(或將其移至其他文件系統)時觸發命令: |
移動監視 | IN_MOVE_SELF | To trigger a command if a watched file or directory is moved within the filesystem: | 如果監視的文件或目錄在文件系統中移動,要觸發命令: |
關閉 | IN_CLOSE | ||
移動 | IN_MOVE | ||
IN_ONESHOT | |||
所有事件 | IN_ALL_EVENTS | ||
不要跟隨 | IN_DONT_FOLLOW | ||
僅目錄 | IN_ONLYDIR |