Home > System Tutorial > LINUX > body text

Do you know some reasons why crontab scheduled tasks are not executed?

WBOY
Release: 2024-03-09 09:49:15
forward
1221 people have browsed it

Summary of some reasons why crontab scheduled tasks are not executed

Update time: January 9, 2019 09:34:57 Author: Hope on the Field

This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. It also provides solutions to every possible inducement. It has certain reference and learning value for colleagues who encounter this problem. Students who need it, please follow the editor to learn together

Preface

Recently, I encountered some problems at work. The crontab scheduled tasks were not executed. Later, when I searched online, I found that the Internet mainly mentioned these 5 incentives:

1crond service is not started

crontab is not a function of the Linux kernel, but relies on a crond service. This service can be started or stopped. If it stops, it will be difficult to execute any scheduled tasks. The solution is to open it:

crond
Copy after login

or

service crond start
Copy after login

If it prompts that the crond command does not exist, it may have been deleted. You can reinstall it under CentOS through this command:

yum -y install crontabs
Copy after login

2Permission issue

For example: the script does not have x execution permission, solution:

Reduce execution permissions, or use bashabc.sh to execute

It is also possible that the user to which the crontab task belongs does not have write permissions for a certain directory and it will fail.

3 Path problem

Some commands execute normally in the shell, but always fail when executed in crontab. It may be that the sh used by crontab does not recognize the path correctly. For example: after logging in to the shell as root and executing a /root/test.sh, just execute

./test.sh
Copy after login

That’s it. And this script will not be found in crontab, for example, write it completely:

/root/test.sh
Copy after login

4 Time difference issue

linux任务计划_linux 计划任务没执行_linux任务计划不生效

Due to the time difference between the server and the client, the crontab time is based on the server time.

The issue of jet lag is really annoying. I have experienced this myself. The phenomenon is as follows:

(1) I set up a scheduled script and used the date command to observe the server time when it reached the script execution time and found that it was not executed

(2)And I set the script to execute once every minute, which is OK

Damn it, is the server time correct? Do I need to add which time zones? So I tried reducing the script time by 10, 12, or 8 hours, but it didn't work.

然而很显著是时间不一致引起的不执行。

最后用如下两行解决了问题:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
service crond restart
Copy after login

参考这篇文章:

5变量问题

有时侯命令中富含变量,但crontab执行时却没有,也会导致执行失败。

验证后,我的定时脚本test.sh不执行不是上述任何一种缘由,虽然我的脚本就一句话:

#!/bin/bash
echo 123 >> testFile
Copy after login

我希望通过这些方法来测试我设置的定时脚本起作用了,于是我设置了该脚本每分钟执行一次,而且死活在脚本所在目录看不到这个文件linux 计划任务没执行,我自动执行

# sh test.sh
Copy after login

却能看见在脚本所在目录能看见这个文件

我怀疑是crontab根本没有执行,于是我在crontab中直接添加了

*/1 * * * * echo 123 >> /home/denglinjie/testFile
Copy after login

testFile文件生成了,说明crontab是执行了的,那看来是我脚本自身存在问题

最后发觉,原先是testFile这儿必须写完整的路径,我天真的以为testFile会生成在脚本所在的目录,所以改成了如下方式

#!/bin/bash
echo 123 >> /data/denglinjie/testFile
Copy after login

之后就可以了。

linux任务计划_linux 计划任务没执行_linux任务计划不生效

虽然路径是个十分容易出问题的地方,假定在/home/denglinjie目录下有一个脚本文件test1.sh,之后在该目录下还有一个脚本文件test2.sh

在test1.sh中执行了test2.sh,并且用的是相对路径,即相对test1.sh所在的路径。

若果在crontab-e中编辑的时侯,执行的方法是

sh/home/denglinjie/test1.sh,当执行到调用shtest2.sh的时侯,系统会觉得是从crontab文件所在的目录去找test2.sharm linux,而且显然是找不到的,导致执行失败

最开始我想的方式是,我要将我写的待执行的脚本文件以及被调用的其他的脚本和crontab文件放在一个地方,这样就可以拉,并且失败了,可能是由于权限问题,我进不去/var/spool/cron目录。

所以另外一个解决方式就是在执行脚本之前先通过cd/home/denglinjie命令步入到脚本所在目录

------------------------------------------------------------------

近来又发觉一种新的导致crontab不执行的诱因

这儿我要执行的是python脚本,我python脚本的目录为:

/data/denglinjie/work/UpdateModuleSwitch
Copy after login

一开始我的定时任务是这样写的:

linux任务计划_linux任务计划不生效_linux 计划任务没执行

0 * * * * cd /data/denglinjie/work/UpdateModuleSwitch;python update_switch.py
Copy after login

发觉到了时间点竟然没有执行,其中update_switch.py的部份内容如下:

import pymongo
Copy after login

就是我的脚本中引入了自己安装的pymongo,注意,这个pymongo是安装到了指定的python版本上的

不执行缘由:crontab定时任务执行的时侯,使用的python不是我的那种python,使用的这个python没有安装pymongo,致使import失败

解决办法,改成如下方式:

0 * * * * cd /data/denglinjie/work/UpdateModuleSwitch;/data/zhoumi/install_evn/bin/python update_switch.py
Copy after login

指定运行使用的python,这个python早已安装绑定了pymongo,或则用如下方式:

0 * * * * export PATH=/data/zhoumi/install_evn/bin/:$PATH;cd /data/denglinjie/work/UpdateModuleSwitch;python update_switch.py
Copy after login

由于我的这个python是安装在我自己的用户目录下linux 计划任务没执行,所以系统找不到这个python,所以只要将我的python也加入到系统PATH环境变量中就可以了

总结

以上就是这篇文章的全部内容了,希望本文的内容对你们的学习或则工作具有一定的参考学习价值,假如有疑惑你们可以留言交流,感谢你们对本站的支持。

The above is the detailed content of Do you know some reasons why crontab scheduled tasks are not executed?. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template