登录Ubuntu时如何自动执行shell脚本

Mik*_*ave 15 gnome bash .bash-profile ubuntu-11.04

如何让脚本在我登录时自动执行?不是在机器启动时,也不是对所有用户,而是仅当我(或任何具有脚本的特定用户)通过 GNOME UI 登录时。

从其他地方阅读我认为它.bash_profile在我的主目录中,但对我来说它没有影响。当我通过键入在终端窗口中手动执行它时,~/.bash_profile它可以工作,但是当我登录时它不会自动运行。

我正在运行 Ubuntu 11.04。我的 .bash_profile 的文件权限是-rwx------. 在我今天创建它之前,我的主目录中不存在 .bash_profile 。

我似乎记得旧版本的 Linux.profile为每个用户都有一个文件,但这也不起作用。

它是如何完成的?我是否需要配置其他东西才能使 .bash_profile 工作?或者每个用户的登录脚本是否需要在其他文件中?

Jua*_*ero 12

您可以简单地将以下命令添加到系统 > 首选项 > 启动应用程序中:

bash /full/path/to/your/script.sh
Run Code Online (Sandbox Code Playgroud)

这应该够了吧 ;)

  • `/etc/xdg/autostart` 似乎是一个系统范围的文件,而不是特定于用户的。 (2认同)

小智 8

所以基本上,正如nodiscc建议的那样,创建一个桌面启动器:~/.config/autostart/script.desktop包含以下内容:

[Desktop Entry]
Type=Application
Name=Autostart Script
Exec=autostart
Icon=system-run
X-GNOME-Autostart-enabled=true
Run Code Online (Sandbox Code Playgroud)

然后使用您的 bash 内容创建自动启动脚本:~/bin/autostart

#!/bin/bash
# Execute bash script below
Run Code Online (Sandbox Code Playgroud)

确保~/bin/autostart是可执行的


小智 5

您可以在 crontab 中添加一行 -

crontab -e
Run Code Online (Sandbox Code Playgroud)

然后将此行添加到打开的文件中:

@reboot /path/to/your/cool/script
Run Code Online (Sandbox Code Playgroud)

这将在重新启动时运行脚本。有关更多详细信息,请参阅man crontab