<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>脚本 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E8%84%9A%E6%9C%AC/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:41:19 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]后台运行的shell脚本对文件锁的影响</title>
		<link>https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/</link>
					<comments>https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 24 Jan 2011 08:53:26 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[flock]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[后台]]></category>
		<category><![CDATA[影响]]></category>
		<category><![CDATA[文件锁]]></category>
		<category><![CDATA[脚本]]></category>
		<category><![CDATA[运行]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=1101</guid>

					<description><![CDATA[<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设shell脚本A中的一段代码设置有文件锁，在此文件锁的后面（外部）以后台运行的方式调用了脚本B（此脚本中也有文件锁，但是与脚本A的锁文件不是同一个文件），并且脚本B执行时间相当之长，那么，在后台运行的脚本B在结束之后，是否会导致脚本A无法启动第二个进程呢？</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">下面就来做一个简单的试验，以确定上面的问题。</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本A（<span style="color:#00f;">parent.sh</span>）内容如下：</span></span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&#34;/root/&#34;
LOCKFILE_1=$SHELL_DIR&#34;.lock1&#34;
{
    echo &#34;Go into lock1.&#34;
    if ! flock -n 4; then
        echo &#34;*** Shell is running, quit.&#34;
        exit 1
    fi
} 4&#60;&#62;$LOCKFILE_1

$SHELL_DIR&#34;child.sh&#34; &#38;      # run shell B on background 
</pre>
<p>
<span id="more-1101"></span></p>
<p>
	<span style="font-size:14px;"><strong style="font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B</span>（<span style="color: rgb(0, 0, 255); ">child.sh</span></span>&#8230; <a href="https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">假设shell脚本A中的一段代码设置有文件锁，在此文件锁的后面（外部）以后台运行的方式调用了脚本B（此脚本中也有文件锁，但是与脚本A的锁文件不是同一个文件），并且脚本B执行时间相当之长，那么，在后台运行的脚本B在结束之后，是否会导致脚本A无法启动第二个进程呢？</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">下面就来做一个简单的试验，以确定上面的问题。</span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本A（<span style="color:#00f;">parent.sh</span>）内容如下：</span></span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&quot;/root/&quot;
LOCKFILE_1=$SHELL_DIR&quot;.lock1&quot;
{
    echo &quot;Go into lock1.&quot;
    if ! flock -n 4; then
        echo &quot;*** Shell is running, quit.&quot;
        exit 1
    fi
} 4&lt;&gt;$LOCKFILE_1

$SHELL_DIR&quot;child.sh&quot; &amp;      # run shell B on background 
</pre>
<p>
<span id="more-1101"></span></p>
<p>
	<span style="font-size:14px;"><strong style="font-family: arial, helvetica, sans-serif; font-size: 14px; line-height: 20px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B</span>（<span style="color: rgb(0, 0, 255); ">child.sh</span>）内容如下：</span></p>
<p></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/sh
SHELL_DIR=&quot;/root/&quot;
LOCKFILE_2=$SHELL_DIR&quot;.lock2&quot;
{
    echo &quot;Go into lock2.&quot;
    sleep 1000
    echo &quot;Sleep done.&quot;
} 5&lt;&gt;$LOCKFILE_2
</pre>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><font class="Apple-style-span">然后运行脚本A：</font></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="color:#00f;"><span style="font-size:14px;">./parent.sh</span></span></span></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size:14px;">脚本B运行中，会暂停（sleep）1000秒，因此它会卡在那里，但是它是以后台方式运行的，并不会影响脚本A的终止。就算脚本B没有结束，你也可以再启动任意多个脚本A，并且你不会看到&ldquo;Shell is running, quit&rdquo;的提示，此外，你还可以查看到进程中有N个child.sh在运行（<span style="color:#00f;">ps -ef | grep child.sh</span>）。这说明在脚本B使用另一个文件锁、并且以后台方式被脚本A调用的情况下，不会对脚本A的文件锁造成影响。</span><br />
	</span></p>
<p>
	<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;"><span style="color:#f00;">但是</span>，如果你把脚本A中的&ldquo;$SHELL_DIR&quot;child.sh&quot; &amp;&rdquo;这句调用脚本B的语句放到锁的内部（即两个大括号中间）呢？答案是与上面<span style="color:#f00;">相反</span>的，即：脚本B没有结束之前，脚本A会被锁住，你将会看到打印出&ldquo;Shell is running, quit&rdquo;的提示。</span></span></p>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
<p>
	<span style="font-family:arial,helvetica,sans-serif;"><strong style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(46, 46, 46); border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(0, 0, 255); "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; "><span style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 13px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; outline-width: 0px; outline-style: initial; outline-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; border-style: initial; border-color: initial; color: rgb(255, 255, 255); font-weight: normal; line-height: 20px; ">文章来源：http://www.codelast.com/</span></span></span></strong></span></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c%e7%9a%84shell%e8%84%9a%e6%9c%ac%e5%af%b9%e6%96%87%e4%bb%b6%e9%94%81%e7%9a%84%e5%bd%b1%e5%93%8d/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
