<?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>unix &#8211; redpanthers.co</title>
	<atom:link href="/tag/unix/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Tue, 22 Nov 2016 16:39:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>Different ways to run Shell Commands in Ruby</title>
		<link>/different-ways-to-run-shell-commands-in-ruby/</link>
				<comments>/different-ways-to-run-shell-commands-in-ruby/#comments</comments>
				<pubDate>Tue, 22 Nov 2016 16:39:41 +0000</pubDate>
		<dc:creator><![CDATA[anjana]]></dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cmd app]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=651</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[During development of an application, there will come cases when we need to access system command from our program itself. In some cases we just need to know if the status was complete, in some cases, we also need to know the output the application returns.But before we start, let's find out what Shell Commands are. Simply put, the shell is a program that takes your commands from the keyboard and gives them to the operating system to perform. There are several additional shell programs available on a typical Linux system. Described below are 6 different way to execute a shell script from ruby and their properties.


<ol>
 	

<li>Exec</li>


 	

<li>System</li>


 	

<li>Backticks ()</li>


 	

<li>IO#popen</li>


 	

<li>open3#popen3</li>


 	

<li>open4#popen4</li>


</ol>


<span style="font-size: large;"><b>Exec </b></span>
Kernel#exec (or simply exec) replaces the current process by running the given command, which can take one of the following forms:
<code>exec(commandline)</code>
<em> command line string which is passed to the standard shell</em>
<code>exec(cmdname, arg1, ...)</code>
<em> command name and one or more arguments (no shell)</em>
<code>exec([cmdname, argv0], arg1, ...)</code>
<em> command name, argv and zero or more arguments (no shell)</em>
In the first form, the string is taken as a command line that is subject to shell expansion before being executed.
In the second form (exec(&#8220;command1&#8221;, &#8220;arg1&#8221;, &#8230;)), the first is taken as a command name and the rest are passed as parameters to command with no shell expansion.
In the third form (exec([&#8220;command&#8221;, &#8220;argv0&#8221;], &#8220;arg1&#8221;, &#8230;)), starting a two-element array at the beginning of the command, the first element is the command to be executed, and the second argument is used as the argv[0] value, which may show up in process listings.
The major drawback is that you have no knowledge of the success or failure of the command from your Ruby script.
Eg:


<pre class="lang:ruby decode:true ">exec "echo", "*" # echoes an asterisk
</pre>


&nbsp;
<strong style="font-size: large;">System</strong>
The system command operates similarly but the system command runs in a subshell instead of replacing the current process. The system gives us a little more information than exec in that it returns true if the command ran successfully and false otherwise.
The system returns true if the command gives zero exit status, false for non-zero exit status. Returns nil if command execution fails. An error status is available in $?. System eats up all the exceptions. So the main operation never needs to worry about capturing an exception raised from the child process.
Eg:


<pre class="lang:ruby decode:true ">system("ls") #=&gt; true
puts $? #=&gt; pid 14822 exit 0</pre>


&nbsp;
<span style="font-size: large;"><strong>Backticks ()</strong></span>
Backticks (also called “backquotes”) runs the command in a subshell and returns the standard output from that command. Backtick operation forks the master process and the operation is executed in a new process. Backtick is a blocking operation. The main application waits until the result of backtick operation completes. Backtick runs the command via shell. So shell features like string interpolation and a wild card can be used.
Eg:


<pre class="lang:ruby decode:true ">date  #=&gt; "Mon Nov 14 13:32:54 IST 2016\n"
</pre>


&nbsp;
<strong style="font-size: large;">IO#popen</strong>
IO#popen is another way to run a command in a subprocess. popen gives you a bit more control where the subprocess standard input and standard output are both connected to the IO object.It returns an object of &#8216;IO&#8217; class. You can loop through the output or use map to access individual elements.
Eg:


<pre class="lang:ruby decode:true ">IO.popen("date") { |f| f.gets() } #=&gt; "Mon Nov 14 13:36:13 IST 2016\n"</pre>


&nbsp;
<strong style="font-size: large;">Open3#popen3</strong>
The Ruby standard library includes the class Open3. It’s easy to use and returns stdin, stdout and stderr.
Eg:


<pre class="lang:ruby decode:true ">require 'open3'
cmd = 'git push heroku master'
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
  puts "stdout is:" + stdout.read
  puts "stderr is:" + stderr.read
end</pre>


Output:
stdout is:
stderr is:
&#8212;&#8211;&gt; Heroku receiving push
&#8212;&#8211;&gt; Ruby/Rails app detected
&#8212;&#8211;&gt; Installing dependencies using Bundler version 1.2.1
<span style="font-size: large;"><strong>Open4#popen4</strong></span>
Open4#popen4 is a Ruby Gem put together by Ara Howard. It operates similarly to open3 except that we can get the exit status from the program. popen4 returns a process id for the subshell and we can get the exit status from that waiting on that process.
Eg:


<pre class="lang:ruby decode:true">$ irb
&gt;&gt; require "open4"
=&gt; true
&gt;&gt; pid, stdin, stdout, stderr = Open4::popen4 "false"
=&gt; [26327, #, #, #]
&gt;&gt; $?
=&gt; nil
&gt;&gt; pid
=&gt; 26327
&gt;&gt; ignored, status = Process::waitpid2 pid
=&gt; [26327, #]
&gt;&gt; status.to_i
=&gt; 256</pre>


<span style="font-size: large;"><strong>References</strong></span>
<a href="http://ruby-doc.org/core/Kernel.html#method-i-exec">http://ruby-doc.org/core/Kernel.html#method-i-exec</a>
<a href="http://ruby-doc.org/core/Kernel.html#method-i-system">http://ruby-doc.org/core/Kernel.html#method-i-system</a>
Hope this article comes to your use.]]&gt;		</p>
]]></content:encoded>
							<wfw:commentRss>/different-ways-to-run-shell-commands-in-ruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
							</item>
		<item>
		<title>Enable log rotation within the rails application</title>
		<link>/enable-log-rotation-within-rails-application/</link>
				<pubDate>Wed, 27 Jul 2016 05:00:19 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[devops]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Logs]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[Production]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rotate]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[space]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=359</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Any person who has worked with a rails application for a good amount of time would have faced the issue of log files growing and consuming the entire space in the hard disk. Although this sounds funny, it does happen if you do not place a log management for your rails app (in production). You can manage your logs using log rotation. Do note that log rotation is not required if you host your app in <a href="http://heroku.com/">heroku</a>, as heroku won&#8217;t let you store files for more than 15 minutes in their server. You also do not need it if you are using some third party services like <a href="https://www.loggly.com/docs/rails-logs/">loggly</a> or <a href="https://papertrailapp.com/">papertrail</a> to manage your logs .
In the unix world, you can use log rotate service, which would be installed by default in all linux servers. But we at Red Panthers feel that everything that touches or involves our rails application should be placed along with our rails application as much as possible. If you feel the same way, it can be achieved by placing the code below in the production.rb file.


<pre class="toolbar:2 show-plain:3 lang:ruby decode:true ">config.logger = Logger.new( Rails.root.join("log", Rails.env + ".log" ), 5 , 100 * 1024 * 1024 )</pre>


The Logger.new accepts three parameters:


<ol>
 	

<li>The file name, which would be <em>production.log</em></li>


 	

<li>The number of files you want to keep</li>


 	

<li>The size of each file in bytes. (100 * 1024*1024 means 100 MB)</li>


</ol>


So, when the time comes to add the 6th 100 MB log file, it will delete the oldest.
<strong>Unix Way</strong>
For those who still want to use the unix log rotate, they should create a new config file at <code>/etc/logrotate.d/myrailsapp-config</code> as shown below


<pre class="toolbar:2 show-plain:3 lang:sh decode:true">$ cat /etc/logrotate.d/myrailsapp-config
/var/www/myrailsapp/current/log/*.log {
  compress
  copytruncate
  daily
  dateext
  delaycompress
  missingok
  rotate 90
}</pre>


In case you were wondering, here are what these options mean:


<ul>
 	

<li><b>compress</b> – Compress logs using gzip</li>


 	

<li><b>copytruncate</b> – Copy the log out of the way, and then truncate the existing logs</li>


 	

<li><b>daily</b> – Rotate daily</li>


 	

<li><b>dateext</b> – Add the date the logs were rotated as the extension instead of a number</li>


 	

<li><b>delaycompress</b> – Skip compressing the log until the following day</li>


 	

<li><b>missingok</b> – Do not raise an error when there is a missing log file</li>


 	

<li><b>rotate 90</b> – Keep up to 90 days worth of logs</li>


</ul>

]]&gt;		</p>
]]></content:encoded>
										</item>
	</channel>
</rss>
