Shell Tips--use "$$" and "$" to prevent the same script from starting multiple times

Source: Internet
Author: User

When you write a shell script that starts with the Crontab timer task, if the shell script execution time exceeds, Crontab pulls the script time interval, this time may appear the same script simultaneously initiates several times the phenomenon. This time may lead to duplication of data collection, calculation inaccuracies and so on, in such a clear first and sequence requirements, there will be problems.

Some means must be taken to prevent this phenomenon. It is possible to use "$$" and "$" to achieve this requirement, only after the execution of the current script is completed, the second execution can be initiated. Okay, no more nonsense, just paste a demo, as long as a little change, you can apply to their own program inside. With the same flexibility, you can achieve the effect of the daemon.

$ $ Current Program name $n Current program parameters (starting at 1, space-delimited) $* all parameters of the current program $# the current program's parameters $$ the current program's PID (the current process ID number of the script run) $? Executes the return value of the previous instruction (displays the exit status of the last command. 0 means no error, any other value indicates an error)

First time execution

#sh ponlyone.sh &[1] 30538# pid=30538  pname=ponlyone.sh expid=30452$$==30538$0==ponlyone.sh$?==0$#==1$1== 111$2==$\!==


Second execution

#sh ponlyone.sh &[2] 30570# pid=30570  pname=ponlyone.sh expid=30538the process ponlyone.sh is already exists! PID : 30538[2]+  done                    sh ponlyone.sh



#!/bin/shsource/etc/profilepid= ' echo $$ ' pname= ' echo $ ' mypidfile=~/pid.testecho-e ' \ n pid= $pid  pname= $pname \ n ' Function Dodo () {     echo "\$\$==" $$     echo "\$0=="     $ echo "\$?==" $?     echo "\$#==" $#     echo "\$1=="     echo "\$2==" $     echo "\$\!==" $!     Sleep 100}if test-f "$mypidfile" then     expid= ' cat $mypidfile '     echo ' expid= $expid "     pfalg= ' Ps-ef|grep" $ Expid "|grep" $pname "|wc-l"     If ["1" = "$pfalg"]     then          echoes "the process $pname is already exists! PID: $expid "     else          echo $pid > $mypidfile          Dodo 111              fielse     echo $pid > $mypidfile     Dodo     fi



Copyright NOTICE: Disclaimer: This document can be reproduced, must be signed by the original author. Inaction qq:490073687

Shell Tips--use "$$" and "$" to prevent the same script from starting multiple times

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.