Linux prevents shell scripts from repeating execution

Source: Internet
Author: User

Today, when deployed in Jenkins, because developers frequently submitted code, resulting in Jenkins shell script in that frequent deployment, in order to prevent such problems, a special search to prevent the recurrence of code. Here's a look at the code:
#!/bin/bash

Lock_name= "/tmp/my.lock"
if (Set-o noclobber echo "$$" > "$LOCK _name") 2>/dev/null;
Then
Trap ' rm-f ' $LOCK _name '; Exit $? ' INT TERM EXIT


### Start normal process

### Normal Process End


### removing lock
Rm-f $LOCK _name
Trap-int TERM EXIT
Else
echo "Failed to acquire Lockfile: $LOCK _name."
echo "Held by $ (cat $LOCK _name)"
Exit 1
Fi

echo "Done."
This ensures that My.lock can only be created by one process. Much more reliable than the touch.
A trap captures a variety of signals and then processes it:
INT is used to handle CTRL + C canceling script execution.
TERM is used to handle kill-term PID cases.
EXIT not clear
In addition, it is not valid for kill-9.

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.