Summary of some reasons why crontab scheduled tasks are not executed

Summary of some reasons why crontab scheduled tasks are not executed

Preface

I recently encountered some problems at work. The crontab scheduled tasks did not execute. Later, when I searched online, I found that the main reasons were:

1 The crond service is not started

Crontab is not a function of the Linux kernel, but relies on a crond service, which can be started and stopped. If it is stopped, no scheduled tasks can be executed. The solution is to turn it on:

crond

or

service crond start

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

yum -y install crontabs

2. Permission issues

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

Add execution permissions, or use bash abc.sh to execute

It is also possible that the user to whom the crontab task belongs does not have write permission to a certain directory, which will also cause it to fail.

3 Path Problem

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

./test.sh

That's it. But in crontab, you won't find this script, for example, write the complete:

/root/test.sh

4. Time difference problem

Because of the time difference between the server and the client, the crontab time is based on the server time.

The time difference is really annoying. I have experienced it myself. The phenomenon is as follows:

(1) I set up a scheduled script and used the date command to observe the server time. When the script was executed, it was not executed.

(2) But I set the script to execute once every minute, and it works OK

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

But it is obvious that the non-execution is caused by time inconsistency.

Finally, the problem was solved with the following two lines:

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

Refer to this article: https://www.jb51.net/article/154296.htm

5. Variable Problems

Sometimes the command contains variables, but crontab does not have them when it is executed, which can also cause execution failure.

After verification, my scheduled script test.sh is not executed for any of the above reasons. In fact, my script is just one sentence:

#!/bin/bash
echo 123 >> testFile

I hope to test the timing script I set up in this way. So I set the script to execute once a minute, but I can't find the file in the directory where the script is located. I manually execute

# sh test.sh

But you can see this file in the directory where the script is located

I suspected that crontab was not executed at all, so I added it directly in crontab

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

The testFile file is generated, indicating that crontab is executed, so it seems that there is a problem with my script itself.

Finally, I found that the full path must be written here for testFile. I naively thought that testFile would be generated in the directory where the script is located, so I changed it to the following form

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

And then it works.

In fact, the path is a very easy place to go wrong. Suppose there is a script file test1.sh in the /home/denglinjie directory, and then there is a script file test2.sh in the same directory.

test2.sh is executed in test1.sh, and a relative path is used, that is, the path relative to test1.sh.

If you edit it in crontab -e, the execution method is

sh /home/denglinjie/test1.sh. When calling sh test2.sh, the system will think that it is looking for test2.sh in the directory where the crontab file is located, but it cannot find it, causing the execution to fail.

At first, I thought that I would put the script files I wrote to be executed and other called scripts and crontab files in one place, so that I could pull them, but it failed, probably because of permission issues, I couldn't enter the /var/spool/cron directory.

So another solution is to enter the directory where the script is located through the cd /home/denglinjie command before executing the script

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

Recently, a new reason for crontab not to execute was discovered

What I want to execute here is the python script. The directory of my python script is:

/data/denglinjie/work/UpdateModuleSwitch

At the beginning, my scheduled task was written like this:

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

It was found that it was not executed at the time point. Part of the content of update_switch.py ​​is as follows:

import pymongo

That is, I introduced the pymongo I installed in my script. Note that this pymongo is installed on the specified python version.

Reason for non-execution: When the crontab scheduled task is executed, the python used is not my python, and the python used does not have pymongo installed, resulting in import failure

The solution is to change it to the following form:

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

Specify the Python program to run. This Python program has pymongo installed and bound to it, or use the following format:

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

Because my python is installed in my own user directory, the system cannot find this python, so I just need to add my python to the system PATH environment variable.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to use Linux Crontab to execute PHP scripts regularly
  • Linux uses crontab to implement PHP execution plan timing tasks
  • Detailed explanation of using crontab to execute tasks regularly under CentOS 7
  • How to use crontab to execute a scheduled task once a second in Linux
  • How to execute tasks regularly under Linux and instructions on how to use crontab (collected and sorted)
  • Reasons why crontab scheduled tasks are not executed in Linux
  • Detailed explanation of using Crontab command to execute PHP files regularly in Ubuntu system
  • Detailed explanation of at and crontab commands for scheduled execution of tasks in Linux
  • Detailed explanation of crontab scheduled execution command under Linux
  • Solution to Linux crontab timing execution of Shell scripts when specific commands need to be executed

<<:  MySql index detailed introduction and correct use method

>>:  Detailed explanation of Mybatis special character processing

Recommend

SQL ROW_NUMBER() and OVER() method case study

Syntax format: row_number() over(partition by gro...

Detailed configuration of wireless network card under Ubuntu Server

1. Insert the wireless network card and use the c...

Detailed explanation of the basic commands of Docker run process and image

Table of contents 1. Run workflow 2. Basic comman...

Example code for implementing the "plus sign" effect with CSS

To achieve the plus sign effect shown below: To a...

How to solve the element movement caused by hover-generated border

Preface Sometimes when hover pseudo-class adds a ...

Detailed explanation of the implementation of MySQL auto-increment primary key

Table of contents 1. Where is the self-incremente...

CSS scroll-snap scroll event stop and element position detection implementation

1. Scroll Snap is a must-have skill for front-end...

Build a WebRTC video chat in 5 minutes

In the previous article, I introduced the detaile...

Vue large screen data display example

In order to efficiently meet requirements and avo...

Docker builds python Flask+ nginx+uwsgi container

Install Nginx First pull the centos image docker ...

Docker configuration Alibaba Cloud Container Service operation

Configuring Alibaba Cloud Docker Container Servic...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

How to write DROP TABLE in different databases

How to write DROP TABLE in different databases 1....

JavaScript to implement a simple shopping form

This article shares the specific code of JavaScri...

Sharing experience on MySQL slave maintenance

Preface: MySQL master-slave architecture should b...