Do 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
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
之后就可以了。
虽然路径是个十分容易出问题的地方,假定在/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
一开始我的定时任务是这样写的:
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!

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools