search
HomeSystem TutorialLINUXDo you know some reasons why crontab scheduled tasks are not executed?

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

or

service crond start

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

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

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

/root/test.sh

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

参考这篇文章:

5变量问题

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

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

#!/bin/bash
echo 123 >> testFile

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

# sh test.sh

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

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

*/1 * * * * echo 123 >> /home/denglinjie/testFile

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

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

#!/bin/bash
echo 123 >> /data/denglinjie/testFile

之后就可以了。

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

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

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

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

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

import pymongo

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

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

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

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

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

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

由于我的这个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!

Statement
This article is reproduced at:ITcool. If there is any infringement, please contact admin@php.cn delete
crontab定时任务不执行的一些原因,你知道吗?crontab定时任务不执行的一些原因,你知道吗?Mar 09, 2024 am 09:49 AM

crontab定时任务不执行的一些缘由总结更新时间:2019年01月09日09:34:57作者:田野上的希望这篇文章主要给你们总结介绍了关于crontab定时任务不执行的一些缘由,对每种可能发生的诱因都给出了解决方式,对遇见这个问题的同事们具有一定的参考学习价值,须要的同学们下边随着小编来一起学习学习吧序言近来在工作中遇见了一些问题,crontab定时任务竟然不执行,后来我在网上找的时侯发觉网上主要说了这5个诱因:1crond服务未启动crontab不是Linux内核的功能,而是依赖一个cron

如何使用Systemd和Crontab在Linux系统中实现任务的并行执行如何使用Systemd和Crontab在Linux系统中实现任务的并行执行Sep 26, 2023 pm 06:37 PM

如何使用Systemd和Crontab在Linux系统中实现任务的并行执行在Linux系统中,任务的并行执行是提高系统效率和性能的重要手段之一。本文将介绍如何使用Systemd和Crontab两个工具,在Linux系统中实现任务的并行执行,并提供具体的代码示例。一、Systemd介绍Systemd是一个用于管理Linux系统启动流程和服务管理的工具。通过配置

linux注释crontab文件及crontab执行sh的坑怎么解决linux注释crontab文件及crontab执行sh的坑怎么解决May 15, 2023 pm 09:58 PM

linux注释crontab文件及crontab执行sh的坑原来,在Linux下写了很多crontab,来定时执行某些任务,现在有以下需求:需求:是要注释某些crontab任务,方法:只要在所要取消的crontab任务前,加'#'即可。e.g.54**sunecho"runat5after4everysunday"注释:#54**sunecho"runat5after4everysunday"就这么简单。遇坑1看下面例子最近

PHP中如何进行任务调度和定时任务?PHP中如何进行任务调度和定时任务?May 12, 2023 pm 06:51 PM

在Web开发中,很多网站和应用需要定期执行一些任务,比如清理垃圾数据、发送邮件等。为了自动化这些任务,开发人员需要实现任务调度和定时任务的功能。本文将介绍PHP中如何实现任务调度和定时任务,以及一些常用的第三方库和工具。一、任务调度任务调度是指按照规定的时间或事件来执行某些任务。在PHP中,实现任务调度可以使用cron定时器或类似的机制。通常情况下,任务调度

Spring Boot的任务调度和定时任务实现方法Spring Boot的任务调度和定时任务实现方法Jun 22, 2023 pm 11:58 PM

SpringBoot是一款非常流行的Java开发框架,不仅具有快速开发的优势,而且还内置了很多实用的功能,其中,任务调度和定时任务就是其常用的功能之一。本文将探讨SpringBoot的任务调度和定时任务实现方法。一、SpringBoot任务调度简介SpringBoot任务调度(TaskScheduling)是指在特定的时间点或某个条件下,执行一些特

linux crontab 错误日志怎么看linux crontab 错误日志怎么看Mar 07, 2023 am 09:29 AM

linux查看crontab错误日志的方法:1、查看文件目录“/var/log/cron”;2、通过“tail -f /var/log/cron”命令实现实时文件尾部查看;3、通过“vim /var/log/cron”命令实现通过高级文本查看器查看即可。

如何使用Systemd和Crontab在Linux系统中自动重启应用程序如何使用Systemd和Crontab在Linux系统中自动重启应用程序Sep 28, 2023 pm 03:35 PM

如何使用Systemd和Crontab在Linux系统中自动重启应用程序在Linux系统中,Systemd和Crontab是两个非常重要的工具。Systemd是一个系统和服务管理器,而Crontab则是一个用于在指定时间自动执行任务的工具。本文将以一个具体的例子,介绍如何使用Systemd和Crontab在Linux系统中自动重启应用程序。假设我们有一个No

如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级Sep 27, 2023 am 08:25 AM

如何利用Systemd和Crontab在Linux系统中设置定时任务的优先级,需要具体代码示例在Linux系统中,我们经常需要设置定时任务来执行一些重复性的操作,例如定时备份文件、定期清理日志等。然而,不同的任务可能具有不同的优先级,有些任务需要更高的优先级来确保其准时执行,而有些任务则可以稍后执行。本文将介绍如何利用Systemd和Crontab来设置定时

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft