修改.bash_profile文件使登录后自动执行脚本

sensal 2015-11-28 01:48:25
在~/目录下修改.bash_profile文件,添加./a.sh,运行source .bash_profile可以自动执行我自己写的a.sh脚本。
但是在注销登录后并不能自动执行,这是为什么呢?要怎么修改才能登录后就自动执行我自己写的脚本呢?
对了,我使用的系统是ubuntu14.04
...全文
652 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_41698443 2019-05-22
  • 打赏
  • 举报
回复
引用 楼主 sensal 的回复:
在~/目录下修改.bash_profile文件,添加./a.sh,运行source .bash_profile可以自动执行我自己写的a.sh脚本。
但是在注销登录后并不能自动执行,这是为什么呢?要怎么修改才能登录后就自动执行我自己写的脚本呢?
对了,我使用的系统是ubuntu14.04


你使用的是虚拟机吗?如果是的话,你重启之后要是从桌面的虚拟终端进入的话,.bash_profile并不会生效,因为现在是非登录shell,要想使.bash_profile生效只能以登录shell进行操作才可以,而登录shell必须执行su - 用户名 命令才可以,即使这样,当你关闭登录shell时,下次重启机器还是会遇到同样的问题,所以我建议你把./a.sh 配置到 .bashrc里面,这个配置文件不管是否登录式shell都会生效,每次重启机器时就不用重复操作了!
nswcfd 2015-12-02
  • 打赏
  • 举报
回复
PS,关于.bash_profile和.bashrc,参考man bash的以下内容。 When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and exe- cutes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior. When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
nswcfd 2015-12-02
  • 打赏
  • 举报
回复
不知道理解的是否正确,注销/登陆是指X界面的操作么? 这个当然不会触发bash的profile/rc,打开终端才会起新shell吧。
皇御雷霆 2015-12-02
  • 打赏
  • 举报
回复
引用 4 楼 sensal 的回复:
[quote=引用 2 楼 fan63182830 的回复:] 添加执行权限,使用全路径
我已经添加过权限了,并且我在终端里面执行source .bash_profile可以执行我自己写的脚本,但是在开机登录后自己就不会运行,.bash_profile里面的东西不是在登录后都会默认自动执行的吗?[/quote] source表示在当前bash环境下执行命令 建议把脚本添加至/etc/rc.local
sensal 2015-12-02
  • 打赏
  • 举报
回复
引用 1 楼 fxbszj 的回复:
有的是运行.bash_profile,如RH系列,有的是.xprofile, 有的是.profile。debian系用的可能不是.bash_profile。你可以到/etc/skel目录下看下,这里会存放当前Linux发型版使用的默认配置,如Redhat系列的在此目录下就会有.bash_profile
# ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. 这是unbuntu /etc/skel的.profile里面的注释,是.bash_profile,我试了一下在~目录下面添加.bash_login,也是不行
sensal 2015-12-02
  • 打赏
  • 举报
回复
引用 3 楼 chjc289 的回复:
chmod u+x a.sh
我已经添加过了
sensal 2015-12-02
  • 打赏
  • 举报
回复
引用 2 楼 fan63182830 的回复:
添加执行权限,使用全路径
我已经添加过权限了,并且我在终端里面执行source .bash_profile可以执行我自己写的脚本,但是在开机登录后自己就不会运行,.bash_profile里面的东西不是在登录后都会默认自动执行的吗?
皇御雷霆 2015-11-29
  • 打赏
  • 举报
回复
添加执行权限,使用全路径
Happy_dog Jack 2015-11-29
  • 打赏
  • 举报
回复
chmod u+x a.sh
fxbszj 2015-11-28
  • 打赏
  • 举报
回复
有的是运行.bash_profile,如RH系列,有的是.xprofile, 有的是.profile。debian系用的可能不是.bash_profile。你可以到/etc/skel目录下看下,这里会存放当前Linux发型版使用的默认配置,如Redhat系列的在此目录下就会有.bash_profile

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧