在~/目錄下修改.bash_profile檔案,添加./a.sh,運行source .bash_profile可以自動執行我自己寫的a.sh腳本。
但是在注銷登錄后并不能自動執行,這是為什么呢?要怎么修改才能登錄后就自動執行我自己寫的腳本呢?
對了,我使用的系統是ubuntu14.04
uj5u.com熱心網友回復:
有的是運行.bash_profile,如RH系列,有的是.xprofile, 有的是.profile。debian系用的可能不是.bash_profile。你可以到/etc/skel目錄下看下,這里會存放當前Linux發型版使用的默認配置,如Redhat系列的在此目錄下就會有.bash_profileuj5u.com熱心網友回復:
添加執行權限,使用全路徑uj5u.com熱心網友回復:
chmod u+x a.shuj5u.com熱心網友回復:
我已經添加過權限了,并且我在終端里面執行source .bash_profile可以執行我自己寫的腳本,但是在開機登錄后自己就不會運行,.bash_profile里面的東西不是在登錄后都會默認自動執行的嗎?
uj5u.com熱心網友回復:
我已經添加過了
uj5u.com熱心網友回復:
# ~/.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,也是不行
uj5u.com熱心網友回復:
不知道理解的是否正確,注銷/登陸是指X界面的操作么?這個當然不會觸發bash的profile/rc,打開終端才會起新shell吧。
uj5u.com熱心網友回復:
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.
uj5u.com熱心網友回復:
source表示在當前bash環境下執行命令
建議把腳本添加至/etc/rc.local
uj5u.com熱心網友回復:
在~/目錄下修改.bash_profile檔案,添加./a.sh,運行source .bash_profile可以自動執行我自己寫的a.sh腳本。
但是在注銷登錄后并不能自動執行,這是為什么呢?要怎么修改才能登錄后就自動執行我自己寫的腳本呢?
對了,我使用的系統是ubuntu14.04
你使用的是虛擬機嗎?如果是的話,你重啟之后要是從桌面的虛擬終端進入的話,.bash_profile并不會生效,因為現在是非登錄shell,要想使.bash_profile生效只能以登錄shell進行操作才可以,而登錄shell必須執行su - 用戶名 命令才可以,即使這樣,當你關閉登錄shell時,下次重啟機器還是會遇到同樣的問題,所以我建議你把./a.sh 配置到 .bashrc里面,這個組態檔不管是否登錄式shell都會生效,每次重啟機器時就不用重復操作了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/127245.html
標籤:系統維護與使用區
