Linux Shell Single Instance run __linux

Source: Internet
Author: User

#!/bin/sh
# **************************************************
# * * * Libraries
# **************************************************
Source/etc/profile;
Sleep 2
Date= ' date + '%f_%h:%m '

If [x$1!= "X"]
Then
Date=$1
Fi

Echo $DATE

Basedir= ' DirName $ '
CD $BASEDIR
#######################################################
tmpfile= $BASEDIR/isrunning.tmp

Echo $TMPFILE

# begin--Check if there are any other instances running to ensure that only one instance can be run at the same time

If [f $TMPFILE];then #判断临时文件是否存在www. linuxidc.com

echo "Other instance is running!" #存在表明有一个实例在运行
Exit 0 #退出本脚本的执行
Else
Touch $TMPFILE #监时文件若不存在, use your touch to create a new
chmod $TMPFILE #把临时文件属性改为只建立者可读写
Fi

#用 Trap command to set up a listener to the signal
#程序运行中当监听到信号 0,1,2,3,9,15 Deletes the temporary file and exits the script execution
#比如说, when the script ends itself, the user Ctrl + C drops, gets killed, and the terminal is closed
#系统关机或重启的情况下, you need to delete the temporary files, otherwise the script will not be able to execute
#在 Linux Shell can run trap-l check to see so the signal

Trap "Rm-f ${tmpfile}; Exit "0 1 2 3 9 15

# end--Check if there are any other instances running to ensure that only one instance can be run at the same time

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.