Share a shell script that prevents the script from starting repeatedly

Source: Internet
Author: User

One of the requirements of the project: to prevent the script from repeating the schedule, causing the simultaneous runtime to conflict with each other, you need to create a file before the script runs and delete it at the end.

When the script starts, it determines whether the file exists and exits if it exists.

At first this did not find the problem, but after running for some time, The problem occurs if the process exits without deleting the file.

And sometimes you want to wait for a while, regardless of whether you have started a script with the same name, go down.

Based on the above considerations, the logic to prevent recurrence is recently extracted as a stand-alone script, and some control logic is added, so that a similar function is required to invoke the script directly.

Flow chart:

Code:

#!/bin/sh#singleton.sh Process Name ($) Process ID ($$) Working directory sleep time attempts#0 attempts to represent an infinite number of times#The purpose of this script is to prevent the process from starting repeatedly (like a singleton) when the script starts and the working directory generates a process information file (Pidfile) that acts as a unique lock#if the process inside the pidfile does not exist, the script ends or waits until the number of attempts is overrun.if[ $#! = 5]Then Echo"usage:singleton.sh Process Name (\$0) process ID (\$\$) Working directory sleep Time attempts (0 for unlimited times)"Exit1Fiv_proc_name=${1##*/}V_pid= $V_work_dir= $V_sleep_seconds=$4V_retry_times= $#PID file path simulation file lock withv_pid_file=${v_work_dir}/Singleton_run_pidecho"cmd = $ $*"#determine if the PID file exists#If there are any processes in the file and the process name is the same#if the above conditions are met, try again until the maximum number of attempts is metif[- F ${v_pid_file}]then Echo"File[${v_pid_file}] exists!"V_times=0V_max_retry_times=${v_retry_times}     while[${v_times} -lt ${v_max_retry_times}-O${v_retry_times} -eq0 ]     DoV_pid_old=""; v_proc_name_old=""; Read V_pid_old V_proc_name_old <${v_pid_file}                if["${v_pid_old}"=""-O"${v_proc_name_old}"=""] then echo"error Pidfile [$ (Cat ${v_pid_file})]"             Break; Fi#Considering that the script is sometimes forced to kill will cause the PID file residue is not cleaned up, here plus the PID file content judgment        if[${v_proc_name_old}!=${v_proc_name}] then echo"Proc_name not euqal; [${v_proc_name_old}]! = [${v_proc_name}]! do next;"             Break; Fiif[${v_pid}=${v_pid_old}] then echo"PID Euqal; [${v_pid}] = [${v_pid_old}]! do next;"             Break; Fi#grep parameter--Indicates that the parameter result followed by a keyword or file name this is to avoid the problem that the process name is-bashv_pid_inf=$ (Ps-ef|awk-v v_pid_old=${v_pid_old}‘{if( $= = v_pid_old) print $; } ' |grep-w--${v_proc_name})        if["${v_pid_inf}"=""] then echo"cannot find the pid[ps-ef|awk-v V_pid_old=${v_pid_old} ' {if (\$2 = = v_pid_old) print \$0;} ' | Grep-w--${v_proc_name}]! do next;"             Break; Fi v_times=$ (expr${v_times}+ 1) echo"try Times ${v_times}; Sleep ${v_sleep_seconds}"Sleep${v_sleep_seconds}Done Echo"had tried ${v_times} times! do next;"Fiecho"Create file[${v_pid_file}]!"Echo${v_pid} ${v_proc_name}>${v_pid_file}

Share a shell script that prevents the script from starting repeatedly

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.