Do you know some reasons why crontab scheduled tasks are not executed?
Mar 09, 2024 am 09:49 AMSummary 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.
然而很顯著是時間不一致引起的不執(zhí)行。
最后用如下兩行解決了問題:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime service crond restart
參考這篇文章:
5變量問題
有時侯命令中富含變量,但crontab執(zhí)行時卻沒有,也會導(dǎo)致執(zhí)行失敗。
驗證后,我的定時腳本test.sh不執(zhí)行不是上述任何一種緣由,雖然我的腳本就一句話:
#!/bin/bash echo 123 >> testFile
我希望通過這些方法來測試我設(shè)置的定時腳本起作用了,于是我設(shè)置了該腳本每分鐘執(zhí)行一次,而且死活在腳本所在目錄看不到這個文件linux 計劃任務(wù)沒執(zhí)行,我自動執(zhí)行
# sh test.sh
卻能看見在腳本所在目錄能看見這個文件
我懷疑是crontab根本沒有執(zhí)行,于是我在crontab中直接添加了
*/1 * * * * echo 123 >> /home/denglinjie/testFile
testFile文件生成了,說明crontab是執(zhí)行了的,那看來是我腳本自身存在問題
最后發(fā)覺,原先是testFile這兒必須寫完整的路徑,我天真的以為testFile會生成在腳本所在的目錄,所以改成了如下方式
#!/bin/bash echo 123 >> /data/denglinjie/testFile
之后就可以了。
雖然路徑是個十分容易出問題的地方,假定在/home/denglinjie目錄下有一個腳本文件test1.sh,之后在該目錄下還有一個腳本文件test2.sh
在test1.sh中執(zhí)行了test2.sh,并且用的是相對路徑,即相對test1.sh所在的路徑。
若果在crontab-e中編輯的時侯,執(zhí)行的方法是
sh/home/denglinjie/test1.sh,當(dāng)執(zhí)行到調(diào)用shtest2.sh的時侯,系統(tǒng)會覺得是從crontab文件所在的目錄去找test2.sharm linux,而且顯然是找不到的,導(dǎo)致執(zhí)行失敗
最開始我想的方式是,我要將我寫的待執(zhí)行的腳本文件以及被調(diào)用的其他的腳本和crontab文件放在一個地方,這樣就可以拉,并且失敗了,可能是由于權(quán)限問題,我進不去/var/spool/cron目錄。
所以另外一個解決方式就是在執(zhí)行腳本之前先通過cd/home/denglinjie命令步入到腳本所在目錄
------------------------------------------------------------------
近來又發(fā)覺一種新的導(dǎo)致crontab不執(zhí)行的誘因
這兒我要執(zhí)行的是python腳本,我python腳本的目錄為:
/data/denglinjie/work/UpdateModuleSwitch
一開始我的定時任務(wù)是這樣寫的:
0 * * * * cd /data/denglinjie/work/UpdateModuleSwitch;python update_switch.py
發(fā)覺到了時間點竟然沒有執(zhí)行,其中update_switch.py的部份內(nèi)容如下:
import pymongo
就是我的腳本中引入了自己安裝的pymongo,注意,這個pymongo是安裝到了指定的python版本上的
不執(zhí)行緣由:crontab定時任務(wù)執(zhí)行的時侯,使用的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 計劃任務(wù)沒執(zhí)行,所以系統(tǒng)找不到這個python,所以只要將我的python也加入到系統(tǒng)PATH環(huán)境變量中就可以了
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對你們的學(xué)習(xí)或則工作具有一定的參考學(xué)習(xí)價值,假如有疑惑你們可以留言交流,感謝你們對本站的支持。
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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

How to read Excel data using PyCharm? The steps are as follows: install the openpyxl library; import the openpyxl library; load the Excel workbook; access a specific worksheet; access cells in the worksheet; traverse rows and columns.

Orange3 is a powerful open source data visualization and machine learning tool. It has rich data processing, analysis and modeling functions, providing users with simple and fast data mining and machine learning solutions. This article will briefly introduce the basic functions and usage of Orange3, and combine it with actual application scenarios and Python code cases to help readers better master the usage skills of Orange3. The basic functions of Orange3 include data loading, data preprocessing, feature selection, model establishment and evaluation, etc. Users can use the intuitive interface to drag and drop components to easily build data processes. At the same time, more complex data processing and modeling tasks can also be completed through Python scripts. Below we will go through a practical

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

CoreFreq: Introduction to CPU frequency monitoring tool under Linux In Linux systems, monitoring and managing CPU frequency has always been a relatively important task. By monitoring the frequency of the CPU, we can understand the operating status of the CPU in time and adjust the frequency to improve performance or reduce power consumption. In Linux systems, there are many tools that can be used to monitor CPU frequency, one of the better tools is CoreFreq. This article will introduce the basic functions of the CoreFreq tool and how to

Website subdomain query tools include: 1. Whois Lookup: can query the registration information of a domain name, including subdomain names; 2. Sublist3r: can automatically scan the subdomain name of a domain name with the help of search engines and other tools; 3. DNSdumpster: can query Information such as the subdomain name, IP address and DNS record of the domain name; 4. Fierce: You can query the subdomain name information of the domain name through the DNS server: 5. Nmap; 6. Recon-ng; 7. Google Hacking.

Golang is an open source programming language developed by Google and is widely used in back-end service development, cloud computing, network programming and other fields. As a statically typed language, Golang has an efficient concurrency model and a powerful standard library, so it is favored by developers. However, in actual development, Golang developers usually need to combine other programming languages ??for project development to meet the needs of different scenarios. PythonPython is an object-oriented programming language that is concise, clear, and easy to learn.
