<?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>Linux Explore &#187; shutdown process</title>
	<atom:link href="https://blog.linuxexplore.com/tag/shutdown-process/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.linuxexplore.com</link>
	<description>Exploring Linux</description>
	<lastBuildDate>Mon, 07 Apr 2014 00:30:50 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Use of subsystem lock files in init script</title>
		<link>https://blog.linuxexplore.com/2014/03/19/use-of-subsystem-lock-files-in-init-script/</link>
		<comments>https://blog.linuxexplore.com/2014/03/19/use-of-subsystem-lock-files-in-init-script/#comments</comments>
		<pubDate>Wed, 19 Mar 2014 17:51:43 +0000</pubDate>
		<dc:creator><![CDATA[linuxexplore]]></dc:creator>
				<category><![CDATA[Linux Explore Tips & Tricks]]></category>
		<category><![CDATA[/var/lock/subsys]]></category>
		<category><![CDATA[init script]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[shutdown process]]></category>
		<category><![CDATA[startup process]]></category>
		<category><![CDATA[subsys]]></category>
		<category><![CDATA[subsystem]]></category>

		<guid isPermaLink="false">http://linuxexplore.com/?p=843</guid>
		<description><![CDATA[In /var/lock/subsys/ directory, there are various files like network, iptables, ip6tables, sshd, rsyslog etc. all these files are the lock files created by their init scripts. Most of these files may be empty. These files play the important role in Linux startup and shutdown process. When a service is started through an init script, a&#8230;]]></description>
				<content:encoded><![CDATA[<p>In <strong>/var/lock/subsys/</strong> directory, there are various files like network, iptables, ip6tables, sshd, rsyslog etc. all these files are the <strong>lock files </strong>created by their <strong>init scripts</strong>. Most of these files may be empty.</p>
<p><a href="http://linuxexplore.files.wordpress.com/2014/03/screenshot-subsys.png"><img class="aligncenter size-large wp-image-845" alt="subsys" src="http://linuxexplore.files.wordpress.com/2014/03/screenshot-subsys.png?w=700" width="700" height="294" /></a>These files play the important role in <strong>Linux startup and shutdown process</strong>. When a <strong>service</strong> is started through an init script, a file is touched in the <strong>/var/lock/subsys/</strong> directory with the same name as the init script. This lock file helps in various manners like:</p>
<ul>
<li>This file represents that the service should be running or <strong>subsystem locked</strong>.</li>
<li>It helps to avoid another instance of a service, if it accidentally started again.</li>
<li>It is mandatory to create, if service need to be stopped in shutdown.</li>
</ul>
<p>When the service is stopped, this file is removed.</p>
<p>However only lock file is not enough to know that the service is running or not. Status of a running service can be captured by using <strong><em>status</em> function</strong> defined in <strong>/etc/rc.d/init.d/functions</strong>. Following command in an init script show the status of the service (if status case created in init script):</p>
<pre><strong>service &lt;servicename&gt; status</strong></pre>
<p>It <strong>checks both PID and the lock file</strong> of the service. If PID is not found but the lock file exists, you will get following message:</p>
<pre><strong> dead but subsys locked</strong></pre>
<p>It is not always mandatory to create lock file, the services can be started and stopped without it. But it can create problem during shutdown and <strong>RUNLEVEL switch</strong>. So follow the recommended steps (recommended by me) to write a <strong>good init script </strong>(also drafting a tutorial for writing init script):</p>
<ul>
<li>In start section of init script <strong>touch the lock file</strong> after starting the service. You can use touch at the end of start case.</li>
</ul>
<pre style="padding-left:60px;"><strong>touch /var/lock/subsys/</strong></pre>
<ul>
<li>Don’t forget to check the lock file before starting the service in start case to <strong>avoid the multiple session of same service</strong>.</li>
</ul>
<pre style="padding-left:60px;"><strong>if [ ! -f /var/lock/subsys/ ]; then
     start # start service here
fi</strong></pre>
<ul>
<li>Don’t forget to <strong>remove the lock in stop case</strong>, it can also be added at the end of stop case.</li>
</ul>
<pre style="padding-left:60px;"><strong>rm -f /var/lock/subsys/<strong>&lt;servicename&gt;</strong></strong></pre>
<p>In case system goes power down, lock files remain exist in the system due to non-execution of stop case. It does not tend it to failure as we added a check of existence of lock file in start case. , But the PID of those services will not found that means service is <strong>staled</strong>. During startup process, init checks if PID of a service exist or not. It realize a <strong>stale lock file</strong>, and clean itself up, which allow the init script to start the service successfully.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.linuxexplore.com/2014/03/19/use-of-subsystem-lock-files-in-init-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
