<?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>Subjectively &#187; bash shell script pid</title>
	<atom:link href="http://kirk.webfinish.com/tag/bash-shell-script-pid/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirk.webfinish.com</link>
	<description>dd if=/dev/random &#124; kirk &#62; blog</description>
	<lastBuildDate>Thu, 09 Sep 2010 23:31:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>bash shell script to manage pid file creation/deletion</title>
		<link>http://kirk.webfinish.com/2009/10/bash-shell-script-to-manage-pid-file-creationdeletion/</link>
		<comments>http://kirk.webfinish.com/2009/10/bash-shell-script-to-manage-pid-file-creationdeletion/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 20:52:42 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[bash shell script pid]]></category>

		<guid isPermaLink="false">http://kirk.webfinish.com/?p=52</guid>
		<description><![CDATA[ You want your script to execute without overlapping another execution.]]></description>
			<content:encoded><![CDATA[<p>Problem: You want your script to execute without overlapping another execution.<br />
Solution: Make a flag file so you know that your script is already executing.<br />
Extended problem: What if your script dies and leaves the flag file behind?<br />
Extended solution: Check the process list to see if the old instance is still alive.<br />
Ext. Ext. Problem: What if somebody kills my script?<br />
Ext. Ext. Solution: Trap the abnormal exit and clean up properly.<br />
Ext. Ext. Ext. Problem:  What if someone else is invoking a script with exactly the same name as mine?<br />
Ext. Ext. Ext. Solution: You&#8217;re screwed.  Give your script a really long descriptive name so this won&#8217;t happen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#===  FUNCTION  ================================================================</span>
<span style="color: #666666; font-style: italic;">#          NAME:  pidfilename</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION:  create a predictable pid file name, put it in the right inode</span>
<span style="color: #666666; font-style: italic;">#    PARAMETERS:  none</span>
<span style="color: #666666; font-style: italic;">#       RETURNS:  path and filename</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #000000; font-weight: bold;">function</span> pidfilename<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">myfile</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;$0&quot;</span> .sh<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #007800;">whoiam</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">whoami</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #007800;">mypidfile</span>=<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$myfile</span>.pid
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$whoiam</span>&quot;</span> == <span style="color: #ff0000;">'root'</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">mypidfile</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$myfile</span>.pid
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$mypidfile</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#===  FUNCTION  ================================================================</span>
<span style="color: #666666; font-style: italic;">#          NAME:  cleanup</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION:  post service processing (clean temp space,pid files)</span>
<span style="color: #666666; font-style: italic;">#    PARAMETERS:  none</span>
<span style="color: #666666; font-style: italic;">#       RETURNS:  none</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #000000; font-weight: bold;">function</span> cleanup <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;">#Don't recurse in the exit trap</span>
  <span style="color: #7a0874; font-weight: bold;">trap</span> - INT TERM EXIT
  <span style="color: #666666; font-style: italic;">#remove the pid file cleanly on exit</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mypidfile</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mypidfile</span>&quot;</span>
  <span style="color: #666666; font-style: italic;">#add other post processing cleanup here</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#===  FUNCTION  ================================================================</span>
<span style="color: #666666; font-style: italic;">#          NAME:  isrunning</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION:  is any previous instance of this script already running</span>
<span style="color: #666666; font-style: italic;">#    PARAMETERS:  pidfile location</span>
<span style="color: #666666; font-style: italic;">#       RETURNS:  boolean 0|1</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #000000; font-weight: bold;">function</span> isrunning<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">pidfile</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>  <span style="color: #666666; font-style: italic;">#pid file is nonexistent</span>
  <span style="color: #007800;">procpid</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$procpid</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>  <span style="color: #666666; font-style: italic;">#pid file contains no pid</span>
  <span style="color: #666666; font-style: italic;"># check process list for pid existence and is an instance of this script</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$procpid</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> $<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">value</span>=<span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">value</span>=<span style="color: #000000;">1</span>
  <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #007800;">$value</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#===  FUNCTION  ================================================================</span>
<span style="color: #666666; font-style: italic;">#          NAME:  createpidfile</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION:  atomic creation of pid file with no race condition</span>
<span style="color: #666666; font-style: italic;">#    PARAMETERS:  the pid to put in the file, the filename to use as a lock</span>
<span style="color: #666666; font-style: italic;">#       RETURNS:  none</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #000000; font-weight: bold;">function</span> createpidfile<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #007800;">mypid</span>=$<span style="color: #000000;">1</span>
  <span style="color: #007800;">pidfile</span>=$<span style="color: #000000;">2</span>
  <span style="color: #666666; font-style: italic;">#Close stderr, don't overwrite existing file, shove my pid in the lock file.</span>
  $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>-; <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> noclobber; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mypid</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #666666; font-style: italic;">#Lock file creation failed</span>
  <span style="color: #007800;">procpid</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$mypid</span> <span style="color: #660033;">-ne</span> <span style="color: #007800;">$procpid</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #666666; font-style: italic;">#I'm not the pid in the lock file</span>
    <span style="color: #666666; font-style: italic;"># Is the process pid in the lockfile still running?</span>
    isrunning <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
      <span style="color: #666666; font-style: italic;"># No.  Kill the pidfile and relaunch ourselves properly.</span>
      <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pidfile</span>&quot;</span>
      $<span style="color: #000000;">0</span> $<span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span>
  <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">mypidfile</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>pidfilename<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
createpidfile <span style="color: #007800;">$$</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mypidfile</span>&quot;</span>
<span style="color: #666666; font-style: italic;">#  I win!  set a trap for the lockfile on exit</span>
<span style="color: #7a0874; font-weight: bold;">trap</span> <span style="color: #ff0000;">'cleanup'</span> INT TERM EXIT
<span style="color: #666666; font-style: italic;"># Go ahead and do some processing </span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">10</span></pre></td></tr></table></div>

<p>I have been informed that there are a few corner cases that this script does not cover.  For one, I may have write access to the pidfile, but not delete access.  This would cause the second instance of the script to start a fork bomb.  I can&#8217;t imagine a case where this would actually be true, so I&#8217;m not bothering to fix it.  Second, this script forks itself in the background if the pid file is determined to be abandoned (i.e. power shut off).  This might cause some calling script to return and proceed when the child is not finished.  I&#8217;m not sure how to fix that without making the code extremely ugly, but when I think of something, I&#8217;ll stick it in here.</p>
]]></content:encoded>
			<wfw:commentRss>http://kirk.webfinish.com/2009/10/bash-shell-script-to-manage-pid-file-creationdeletion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
