登录后运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39024657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run bash script after login
提问by Godzilla74
I have a bash script with a series of whiptail menus that allows a user to setup their new system, which is Ubuntu server, with no GUI, just CLI (it's going to be a Virtual Machine image).
我有一个带有一系列鞭尾菜单的 bash 脚本,允许用户设置他们的新系统,即 Ubuntu 服务器,没有 GUI,只有 CLI(它将是一个虚拟机映像)。
I'm already forcing a root login by editing /etc/default/grub
and /etc/init/tty1.conf
, so the user is dropped directly into the root command prompt. From there the user has to type in ./whiptail.sh
to start the script and get the whiptail prompts to further setup their host.
我已经通过编辑/etc/default/grub
和强制进行 root 登录/etc/init/tty1.conf
,因此用户被直接放入 root 命令提示符中。从那里,用户必须输入./whiptail.sh
以启动脚本并获得whiptail 提示以进一步设置其主机。
Now, I'd like for my script to be what appears up after the the login occurs instead of the user being dropped to the command prompt. How can I do this?
现在,我希望我的脚本成为登录后出现的内容,而不是将用户拖放到命令提示符下。我怎样才能做到这一点?
采纳答案by heemayl
All interactive sessions of bash
will read the initialization file ~/.bashrc
.
所有交互会话bash
都会读取初始化文件~/.bashrc
。
So you can just add the script at the end of the root
's .bashrc
i.e. /root/.bashrc
, assuming the script is executable:
所以,你可以只添加脚本在的结束root
的.bashrc
,即/root/.bashrc
,假设脚本是可执行的:
echo '/path/to/whiptail.sh' >>/root/.bashrc
Now the script will be always run when root
opens a new interactive shell. If you only want to run while login only, not all all interactive sessions you should rather use ~/.bash_profile
/~/.bash_login
/~/.profile
(the first one available following the order).
现在,当root
打开一个新的交互式 shell时,脚本将始终运行。如果你只是想运行,而只登录,不是所有的所有的互动环节,你应该宁愿使用~/.bash_profile
/ ~/.bash_login
/ ~/.profile
(第一个可用下列顺序)。