当前位置:网站首页>crontab报错,但本地执行正常
crontab报错,但本地执行正常
2022-07-30 16:52:00 【欧晨eli】
在使用crontab时遇到的环境变量问题表现为: 在shell中能正常执行的脚本,但是通过crontab任务调度时就提示命令不存在:
产生该问题的本质原因是:crontab任务在执行时所能读取到环境变量与用户登录后所读取到到的环境变量是不同的。 用户登录shell后所能读取到的环境变量通常定义在如下几个地方:~/.bashrc,/etc/profile,而crontab定时任务所能读取到的环境变量是定义在 /etc/crontab中的。
如下是我的Linux主机的/etc/crontab内容:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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如上所示,crontab任务所能读取到的PATH变量在如下路径中:/sbin:/bin:/usr/sbin:/usr/bin 。 也就是说在crontab任务中运行的命令都需要在这些路劲下,否则就会提示路径找不到的报错信息。
解决办法如下:
1,将所有使用的非bash内置命令都改为绝对路径调用
# 例如:
# 原来的python3,改为/home/user/python3/python3
# python3 python.py
/home/user/python3/python3 python.py2,在脚本开头先加载环境变量
# 将需要用到的命令加到环境变量中
export PATH=$PATH:/usr/local/bin/python3
# 再执行命令
...
3,将/usr/local/bin/python3 添加到/etc/profile或者~/.bashrc中,然后在crontab任务的执行命令中动态加载环境变量
source /etc/profile
source /home/appadmin/.bash_profile边栏推荐
- 数组和指针(2)
- 云厂商做生态需要“真连接、真赋能”,用“技术+真金实银”发展伙伴
- [TypeScript] Introduction, Development Environment Construction, Basic Types
- lotus 爆块失败
- 新零售saas小程序如何探索数字化门店的破局之路?
- 每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
- Chapter 6: Decisive Autumn Moves
- Summary of String Copy, Concatenation, Comparison and Split Functions (1)
- 华为云数据治理生产线DataArts,让“数据‘慧’说话”
- Gorilla Mux 和 GORM 的使用方法
猜你喜欢

arcpy tutorial

LeetCode167: Sum of two numbers in sorted array

华为云数据治理生产线DataArts,让“数据‘慧’说话”

MySQL详细学习教程(建议收藏)

Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function

疫情之下的裁员浪潮,7点建议帮你斩获心仪offer

Public Key Retrieval is not allowed error solution

Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板

The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.

Summary of String Copy, Concatenation, Comparison and Split Functions (1)
随机推荐
代码越写越乱?那是因为你没用责任链
(1) Cloud computing technology learning - virtualized vSphere learning
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
lotus 爆块失败
Tensorflow中实现正则化
You are a first-class loser, you become a first-class winner
Chapter 5 Advanced SQL Processing
华为云数据治理生产线DataArts,让“数据‘慧’说话”
FP6606ACAW4 TQFN-20L (3mmx3mm) USB双端口充电控制器 百盛电子代理
@Bean注解详解
Oracle动态监听与静态监听详解
哎,这要人老命的缓存一致问题啊
报错500,“message“: “nested exception is org.apache.ibatis.binding.BindingException: 解决记录
全球架构师峰会
Is it reliable to work full-time in self-media?
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
The way of life, share with you!
阿里SIM-基于检索的用户行为兴趣CTR模型(Search-based user Interest Model(SIM))
Leetcode 119. Yang Hui's Triangle II
Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs