Home  >  Article  >  System Tutorial  >  What should I do if Linux scheduled tasks are not executed? This article teaches you how to troubleshoot

What should I do if Linux scheduled tasks are not executed? This article teaches you how to troubleshoot

王林
王林Original
2024-07-18 10:09:34693browse

Linux 定时任务不执行怎么办?一文教你排查方法

The Linux scheduled tasks implemented include: cron, anacron, at, etc., but what I come into contact with in my daily work is the cron service. cron is the service name, crond is the background process, and crontab is the customized scheduled task list. Scheduled tasks are often used in projects to implement individual functions. Recently, I encountered a situation where scheduled tasks were not executed. Because I didn’t know much about this before, it was time-consuming and laborious, and finally I found the problem. For this reason, I would like to share with you some troubleshooting techniques to avoid detours in the future.

1. Check whether the crond service is executed.

You can view the crond process pid through the command pgrepcrond or /sbin/servicecrondstatus, and you can determine whether the crond service is running.

2. Check whether the cron service is working:

linux定时任务不执行_定时执行任务代码_定时执行任务软件

Use the command sudotail–f/var/log/cron

As long as the command appears in this log, it means it has been executed. When I reached this step, I found that there was no execution record in the log, which meant that my scheduled task was not executed.

linux定时任务不执行_定时执行任务代码_定时执行任务软件

3. After this, we need to investigate why it was not executed. There are probably the following situations:

(1) Script: The probability of script problems causing crontab to not execute is very high and it is also the most common. There is no other way but to check the script. You can execute the script automatically to see if an error is reported. If an error is reported, detect the script;

linux定时任务不执行_定时执行任务软件_定时执行任务代码

The automatic execution is successful, check whether the environment variable causes the crontab execution failure. The execution environment of the task is the execution environment of the current user or the specified user linux scheduled tasks are not executed, so when codinglinux scheduled tasks are not executed, try to reference the file or call the command through the full path name, or replace the referenced file Or add the path of the command to the environment variable.

(2) Different system times may also cause scheduled tasks not to be executed. In this case, you need to check the system time

Date(View)

Date–smm/dd/yy (modify system time)

(3) The crontab daemon ran away, restart the process;

servicecrondrestart

(4) There is a problem with script encoding;

The script compiles Linux rar under window. After being transferred to Linux, it will report "锘?!/bin/bash". Use vi editor to create a new shell script, enter the content and save it.

(5) Check whether the script has executable permissions. You must ensure that the user executing the script has the permission to execute the modified file.

The above is the detailed content of What should I do if Linux scheduled tasks are not executed? This article teaches you how to troubleshoot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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