How shell scripts avoid repeated execution _linux shell

Source: Internet
Author: User

Many of the shell scripts executed with Cron may take a long time to execute for a variety of reasons, and it is necessary to check whether they are still running while running. This article provides a Linux shell script to check with the command sh ... To execute the shell script. To make a unique check on something else, you can modify the source code a little bit.

Copy Code code as follows:

# Check whether the shell script executed through the SH command is still in execution and avoid repeated execution.
# put this code on the head of the program that needs to be guaranteed to be unique.
# Note that if you put this script directly into Cron, you have to grep-v "-C sh" to eliminate the cron-generated process!
Checkitem= "$"
Let proccnt= ' ps-a--format= '%p%p%c%x%a '--width 2048-w--sort pid|grep "$checkitem" |grep-v grep|grep-v "-C sh" |grep -V "$$" | Grep-c Sh|awk ' {printf ('%d ', '} '} '
If [${proccnt}-GT 0]; Then
The echo "$ script is already running [procs=${proccnt}] and this execution is canceled automatically."
Exit 1;
Fi

Related Article

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.