linux配置定时任务的用户环境变量

在Linux系统中,执行定时任务时,默认加载的是用户环境变量 ~/.bashrc ,不会加载系统变量,而要使用系统变量,可以用如下方式:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

*/1  *  *  *  * root /root/xx.sh                           # 使用用户环境变量
*/1  *  *  *  * root source ~/.bashrc &&  /root/xx.sh      # 使用用户环境变量

*/1  *  *  *  * root source /etc/profile &&  /root/xx.sh   # 使用系统环境变量

  在以上的配置中,定时任务只是默认加载当前用户的环境变量,即 所有的定时任务,默认使用 source ~/.bashrc 来加载执行后续命令的。

  如果要加载 系统环境变量,则需指定加载系统变量文件,即 source /etc/profile 这段命令。 && 符号意思就是执行多个命令,这里是先加载系统环境变量,然后再执行 /root/xx.sh的命令。

原文地址:https://www.cnblogs.com/xzlive/p/13409787.html