Linux 定時任務不生效的問題


 

Linux 中定時任務不生效的問題屢見不鮮, 本質原因是: 登錄式 shell & 非登錄式 shell.

 

登錄式 shell & 非登錄式 shell

登錄式 shell 有:

su - <username>

Login: username / password

 

非登錄式 shell

su <username>

圖形見面

腳本執行

 

Bash 配置文件

全局配置文件

/etc/profile 

/etc/profile.d/*.sh

/etc/bashrc

 

用戶配置文件

~/.bash_profile

~/.bashrc

 

profile 類文件主要用途

設定環境變量
運行命令和腳本 

 

bashrc 類文件主要用途
 
設定本地變量
定義命令別名 

 

shell 讀取配置文件順序

登錄式 shell

/etc/profile --> /etc/profile.d --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc

 

非登錄式 shell

~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh

 

 

總結

由以上信息可知, 因為定時任務是 [ 非登錄式 shell ], 其讀取的配置文件為: ~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh , 故而很多環境變量不會被加載, 其中包括 PATH.

 

我自己寫定時任務腳本的時候, 會在腳本的開頭寫:

#!/bin/sh

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/zookeeper/bin:/root/bin

 

這是一個很不錯的方法.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM