Why does Linux crond not run? How can this problem be solved ?, Linuxcrond
In order to regularly monitor the CPU, memory, and load usage of the Linux system, write a Linux Shell script and send an email notification periodically when it reaches a certain value.
However, when we asked crond to periodically execute the script to send mail notifications, we encountered a problem. After adding the execution script to crontab-e, we found that the script was not executed.
However, by manually executing the Shell script command (. /mimvp-email.sh) is normal, because the manual execution of the script can get the Linux environment variables by default, but through Crontab for scheduled tasks, you can not get the environment variables.
The reason is analyzed. The main reasons for crond not to be executed are as follows:
1. The crond service is not started.
Ps-ef | grep-v grep | grep crond // check whether the crond service is running
Service crond start // close the service
Service crond stop // close the service
Service crond restart // restart the service
Service crond reload // reload the configuration
2. You are not authorized to execute crond.
The vim/etc/cron. deny file is used to control which users cannot execute the crond service.
You can delete yourself from the file or contact the root user.
3. crontab does not provide the environment variables of the executed user
Solution: add the following line to the script:
./Etc/profile
.~ /. Bash_profile
4. No absolute path is used.
The absolute path here includes the path in the script and the path in the crond command, for example:
*/10 *** sh/root/script/mysql_files_monitor.sh &
5. If the problem is not solved above, you can look for the problem again:
1) Check the email. In this process, the user should receive the email, for example, a prompt like this:
Vim/var/spool/mail/root
You have mail in/var/spool/mail/root
Check out the crond content.
The file is too large to open. You can capture the last 1000 lines to view the file.
Tail-n 1000/var/spool/mail/root> aaa.txt & vim aaa.txt
2) Add output to the script for debugging.
You can add one in the crontab script.
Echo $ PATH>/tmp/test. log
Compare with echo $ PATH for script execution on the terminal
6. Too many crond processes, killing and restarting the crond Service
#! /Bin/bash
For I in $ (ps-elf | grep-v grep | grep crond | awk-F "" '{print $4}'); do
Kill-9 $ I
Done
Run the root command to restart and solve the problem:
Service crond restart
7. crond prevents repeated execution during the script cycle
My personal experience: flock-xn my. lock cmd
My. lock is a file. It can be any file, and you can create an empty file.
When the flock gets the lock, it will execute the cmd
Test process:
$1: flock-xn my. lock sleep 20
$2: flock-xn my. lock ls
The ls of 2 is successful only when 1 is returned.
If a script is running for 30 minutes, you can set the script interval to at least one hour in Crontab to avoid conflicts. The worst case is that the script is not completed in the execution cycle, and the second script starts to run again. How can we ensure that only one script instance is running? A good method is to use lockf (lockf in FreeBSD 8.1 and flock in CentOS 5.5) to check whether a file lock can be obtained before the script is executed to prevent script running conflicts.
The lockf parameters are as follows:
-K: Always waiting to get the file lock.
-S: silent. No information is sent, even if no file lock is obtained.
-T seconds: Set the timeout time to seconds. If the time is exceeded, the system automatically gives up.
Before executing the following crontab scheduled task, you need to obtain the temporary file create. lock file lock. The crontab scheduled task content is as follows:
1 */10 * (lockf-s-t 0/tmp/create. lock/usr/bin/python/home/project/cron/create_tab.py>/home/project/logs/create. log 2> & 1)
If the first instance is not completed within 10 minutes, the first instance will not run. I used to solve this problem through Shell scripts, such as using the while... do loop and then executing it in the background. However, it was found that the use of the flock or lockf method is simpler.
The usage of flock in linux is attached:
Flock (util-linux 2.13-pre7)
Usage: flock [-sxun] [-w #] fd #
Flock [-sxon] [-w #] file [-c] command...
-S -- shared Get a shared lock
# Shared lock: during the time when a shared lock is set for the FD of a file but no lock is released, other processes fail to set an exclusive lock for the FD of the file, other processes attempt to set a shared lock on the FD of this file.
-X -- exclusive Get an exclusive lock
# Exclusive or exclusive lock, within the time when the exclusive lock is set for the FD of a file but the lock is not released, other processes attempt to set a shared or exclusive lock on the FD of this file. If the-s parameter is not set, this parameter is set by default.
-U -- unlock Remove a lock
# Manual unlocking. Generally, this parameter is not required. When FD is disabled, the system automatically unlocks it. this parameter is used when some script commands need to be executed asynchronously and some can be executed synchronously.
-N -- nonblock Fail rather than wait
# Non-blocking mode. If the attempt to set the lock fails, the system uses the non-blocking mode and returns 1 directly,
-W -- timeout Wait for a limited amount of time
# Set the blocking timeout. When the set number of seconds is exceeded, the blocking occurs and 1 is returned.
-O -- close Close file descriptor before running command
-C -- command Run a single command string through the shell after executing the comand
-H -- help Display this text
-V -- version Display version
For example, execute the following script:
Execute a script at every day, but the exclusive file lock must be obtained before execution; otherwise, the command cannot be executed.
130 23 *** flock-xn/tmp/test. lock-c '/usr/local/php test. php'
8.; and & difference
";" Is different from "&".
";": Cmd2 is executed regardless of the result of cmd1 execution.
"&": Cmd2 is executed only when the result returned by cmd1 is successful.
Cmd1 & cmd2; cmd3
-Cmd1 is executed, if it succeeds, then execute00002. and then cmd3 (regardless of cmd2 success or not)
-Cmd1 is executed, if it fails, then cmd3 (cmd2 won't be executed)
9. If a shell syntax error occurs
Syntax error: "(" unexpected
Solution:
You need to specify the shell interpreter command: SHELL =/bin/bash (see the preceding crontab editing example SHELL =/bin/bash)
For more information, see LINUX-BASH Syntax Error.
If a path error occurs
In/var/spool/crontab/yanggang, add the following command. In The log file/var/spool/mail/yanggang, the system prompts that the xxx. sh path cannot be found.
30 */home/barry/top800/top10/top10_fruits/top10_all.sh
Or
30 * bash/home/barry/top800/top10/top10_fruits/top10_all.sh
This is because you use the absolute path in crontab to execute the script top10_all.sh. Therefore, other scripts referenced in the script top10_all.sh also need to use the absolute path to be found and executed by crontab.
We recommend that you use the following format to avoid absolute paths:
30 * cd/home/barry/top800/top10/top10_fruits/&./top10_all.sh (this method is recommended)
First enter the Directory and then execute the script. Otherwise, the absolute path must be added to other scripts in the script.
Reference recommendations:
On CentOS 7.2, crontab schedules tasks.
Linux scheduled Run Command Script-crontab
Solution for non-execution of CentOS crontab scheduled tasks
WordPress scheduled task (wp-cron.php) causes host CPU exceeding Solution