linux-autostart

 
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
如果窗口管理器中有文章,请参见其自动启动部分。

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)

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    
原文地址:https://www.cnblogs.com/sztom/p/13233803.html