Linux crond Reason Analysis __linux

Source: Internet
Author: User
Tags close close flock

In order to regularly monitor the Linux system CPU, memory, load usage, wrote the Linux shell script, when it is worth it, send the mail notification regularly.

However, let Crond to periodically execute the script to send mail notification, encountered a problem, after adding the execution script in Crontab-e, the discovery script did not execute.

However, it is normal to manually execute the Shell script command (./mimvp-email.sh), because the manual execution of the script can obtain the Linux environment variable by default, but you cannot get the environment variable by the timed task crontab.

Analysis of the reasons, Crond do not carry out the main reasons for the following:

1, Crond service did not start

Ps-ef | Grep-v grep | grep crond//View Crond service is running
Service Crond start//Shutdown services
Service Crond stop//off services
Service Crond restart//Restart services
Service Crond Reload//Reload Configuration

2, the user does not execute Crond permission

The Vim/etc/cron.deny file is used to control which users cannot perform the functions of the Crond service.

You can delete yourself from the file, or contact root

3. Crontab does not provide environment variables for the executed user

Workaround: Add the following line to the script:

. /etc/profile
. ~/.bash_profile

4, not using absolute path

The absolute path here includes two aspects of the path in the script and the path in the Crond command, for example:

*/10 * * * * sh/root/script/mysql_files_monitor.sh &

5, if none of the above solve the problem, you can look for the problem:

1 go to the mail to see, in the process of the user should receive mail, such as received such a hint:

Vim/var/spool/mail/root

You have mail in/var/spool/mail/root

Go and see what's in there, Crond.

The file is too large to open, you can intercept the last 1000 lines to view

Tail-n 1000/var/spool/mail/root > Aaa.txt && vim aaa.txt

2 in the script to add output to debug

You can add a crontab in the script

echo $PATH >/tmp/test.log

echo $PATH for script execution under contrast and terminal

6, Crond process too much, all kill restart Crond service

#!/bin/bash
For I in $ (ps-elf | grep-v grep | grep crond | awk-f "" ' {print $} '); Todo
Kill-9 $i
Done

Use root to perform a reboot after the problem is resolved:

Service Crond Restart

7, Crond prevent the script cycle did not perform repeated execution

Personal experience: Flock-xn my.lock cmd
My.lock is a file, can be any file, you can create a new empty file
When Flock gets the lock, it executes the following CMD

Test Process:

$1:flock-xn My.lock Sleep 20
$2:FLOCK-XN My.lock ls

2 LS will succeed only if 1 is returned.

If a script runs for 30 minutes, you can set the script interval to at least one hour in crontab to avoid conflict. Worse, it is possible that the script did not complete during the execution cycle, and then the second script starts running again. How to make sure that only one script instance is running. A good way to do this is to use LOCKF (FreeBSD 8.1 under Lockf,centos 5.5) to detect whether a file lock can be obtained before the script is executed to prevent the script from running a conflict.

LOCKF parameters are as follows

-K: Wait for the file lock to get.

-s:silent, does not emit any information, even if cannot get the file lock.

-T seconds: Set timeout time is seconds seconds, if more than time, then automatically give up.

The following crontab scheduled tasks need to obtain temporary files Create.lock file locks, crontab the contents of the scheduled tasks are as follows:

1 */10 * * * * * (lockf-s-t 0/tmp/create.lock/usr/bin/python/home/project/cron/create_tab.py >>/home/project/lo Gs/create.log 2>&1)

If the first instance does not run out within 10 minutes, the 2nd instance does not run. I used to solve this problem with a shell script, such as using a while...do loop and then doing it in the background. But it was later found that its practical flock or Lockf method was simpler.

attached is the use of flock under Linux:

Flock (Util-linux 2.13-pre7)
Usage:flock [-sxun][-w #] Fd#
Flock [-sxon][-w #] file [-c] command ...
-S--shared get a shared lock
#共享锁, during the time that a shared lock was set on an FD directed to a file without releasing the lock, another process attempted to set a request for an exclusive lock on the FD directed to this file, and the other process attempted to set a request for a shared lock on the FD directed to this file to succeed
-X--exclusive get a exclusive lock
#独占或排他锁, during the time that an exclusive lock was set on an FD directed to a file without releasing the lock, other processes failed to attempt to set a shared or exclusive lock on the FD directed to this file. This parameter is set by default as long as the-s parameter is not set
-U--unlock Remove a lock
#手动解锁, the general situation does not have to, when the FD shutdown, the system will automatically unlock, this parameter used in the script command part of the need for asynchronous execution, part of the situation can be synchronously executed
-N--nonblock Fail rather than wait
#为非阻塞模式, when attempting to set the lock failed, use non-blocking mode to return directly to 1,
-W--timeout wait for a limited amount of time
#设置阻塞超时, when the number of seconds exceeds the set, it jumps out of the block and returns 1.
-o--close close file descriptor before running command
-C--command Run a single command string through the shell executes the following comand
-H--help Display this text
-V--version Display version
For an example, execute the following script:

Executes a script every 23:30, but you must obtain an exclusive file lock before execution, otherwise you cannot execute the command

1 * * * flock-xn/tmp/test.lock-c '/usr/local/php test.php '

8. and && differences

";" and "&&" are different.

";" : Regardless of the outcome of cmd1 execution, perform CMD2

"&&": only cmd1 execution of the returned results is successful before the CMD2

Cmd1 && cmd2; Cmd3

-CMD1 is executed, if it succeeds, then execute cmd2. And then CMD3 (regardless of cmd2 success or not)

-CMD1 is executed, if it fails, then CMD3 (Cmd2 won ' t be executed)

9. If you encounter a shell syntax error

Syntax Error: "(" unexpected  

Workaround:

Need to specify SHELL interpreter command: Shell=/bin/bash (see above crontab Edit sample Shell=/bin/bash)

Or see also: Linux-bash Syntax Error

If you encounter a path error

In/var/spool/crontab/yanggang, the following command was added, prompting/var/spool/mail/yanggang in the log file that the xxx.sh path could not be found

* * * * */home/barry/top800/top10/top10_fruits/top10_all.sh

Or

* * * * * bash/home/barry/top800/top10/top10_fruits/top10_all.sh

This is because you use absolute paths to execute script top10_all.sh in crontab, so other scripts referenced in script top10_all.sh also need to use an absolute path to be found and executed by crontab.

So how do you avoid the absolute path, recommend the following format:

* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_all.sh ( recommended in this way )

Go to the directory first, then execute the script; otherwise, the other scripts in the execution script need to be added to the absolute path

Reference recommendation:

CentOS 7.2 crontab Scheduled Task

Linux timed Run command script--crontab

CentOS crontab Scheduled Tasks do not perform a resolution

WordPress timed Task (wp-cron.php) caused the host CPU exceeded the solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.