<?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>AskApache &#187; Search Results  &#187;  renice</title>
	<atom:link href="http://www.askapache.com/search/renice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.askapache.com</link>
	<description>Advanced Web Development</description>
	<lastBuildDate>Thu, 26 Apr 2012 11:29:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Optimizing Servers and Processes for Speed with ionice, nice, ulimit</title>
		<link>http://www.askapache.com/optimize/optimize-nice-ionice.html</link>
		<comments>http://www.askapache.com/optimize/optimize-nice-ionice.html#comments</comments>
		<pubDate>Sat, 10 Oct 2009 05:41:28 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=3167</guid>
		<description><![CDATA[<p><a href="http://www.askapache.com/linux/optimize-nice-ionice.html" class="IFL" id="id18"></a>To prepare for several upcoming articles on AskApache that are focused on optimizing Servers and Sites from a server admin level, here is an article to introduce the main tools that we will be using.  These tools are used to optimize CPU time for each process using <strong>nice</strong> and <strong>renice</strong>, and other tools like <strong>ionice</strong> are used to optimize the Disk IO, or Disk speed / Disk traffic for each process.  Then you can make sure your mysqld and httpd processes are always fast and prioritized.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/optimize/optimize-nice-ionice.html"></a><a href="http://www.askapache.com/optimize/optimize-nice-ionice.html"><cite>AskApache.com</cite></a></p><p>Ok, sup.  I really felt I had to get this out of the way, because I have a whole stack of drafts waiting to be published, but I realized that not many people will benefit from all the advanced optimizations and tricks I'm writing unless they get a basic understanding of some of the tools I'm using.  I decided to write a series of articles explaining how I optimize servers for speed because lately I've been getting a lot more people wanting to hire me to do that.  I take on projects when I can but there is clearly a need out here on the net for some self-help.   The momentum is swinging more and more towards VPS type of web hosting, and I would say that 99% of those customers are getting supremely ripped off, which goes against the foundation of the web.</p>
<p>Keep in mind that this blog and my research is only a hobby of mine, my job is primarily marketing and sales, so I'm not some licensed expert or anything, or even an unlicensed expert! haha.  But it does bother me that those who are tech-savvy enough to run web-hosting companies are happily ripping people off.  So this article details the main tools that are used to speed up and optimize your machine by delegating levels of priority to specific processes.  Future articles will use these tools alot, so this is meant as an intro.</p>




<p><a id="cpu-disk-io" name="cpu-disk-io"></a></p><h2>CPU and Disk I/O</h2>
<p>As most of you are aware, there are 2 variables that determine any computer or programs speed.  CPU and Disk I/O.  CPU determines how fast you can process data, crunch numbers, etc. while disk I/O determines how fast your disks can read and write data to the hard-drive.  Wouldn't it be great if you could easily configure your server to give your httpd, php, and other processes both greater CPU processing and disk IO than your non-important processes like backup scripts, ftp daemons, etc.?  We are talking about Linux in this article, so of course YES not only can you do that, you should!</p>
<p><a name="optimize-ram" id="optimize-ram"></a></p><h3>RAM</h3>
<p>RAM is like a hard-drive in that data is stored on it, and read/written to it.  The difference is that RAM is somewhere around 30x faster than disk I/O, but the cost of that incredible speed is that the data stored on it is only temporary in the sense that it won't be stored permanently, it is completely erased when your machine is rebooted.  RAM is also expensive, and there is a limit to how much a server or machine can have due to hardware limits.</p>
<p><a name="optimize-swap" id="optimize-swap"></a></p><h3>SWAP</h3>
<p>SWAP takes off when you run out of RAM but you still want certain data to be read/write quickly.  Basically when you start running out of RAM your machine starts supplementing RAM with SWAP storage.  SWAP is usually a partition on a second hard-drive disk.  There is an upper limit on how much I/O can occur on a disk at one time, and the more I/O takes place, the slower all I/O becomes, so SWAP works well on a separate hard-drive as it will have much faster I/O.  On Windows they opted to copy the SWAP mechanism but instead use a file named pagefile.sys, and that is just one reason people in the know do not care for Windows.</p>
<p><a name="optimize-cpu" id="optimize-cpu"></a></p><h3>CPU</h3>
<p>So lets do this, think of your CPU (your processor) as having an amount of 100% processing available when not being used, 0% when its maxed out.  CPU's handle multiple processing tasks simultaneously, so what we will discuss in this article is how to specify HOW MUCH of that processing amount each of your programs (heretofore "processes") are able to use.  Yes, very very cool.</p>
<p>That is correct, you can easily configure your server to provide more of the available processing time to certain programs over others, like you can configure apache and php to utilize 50% of your CPU processing time by themselves, so that all other processes (proftpd, sshd, rsync, etc.) combined can only utilize 50%.  The terminology is we can give certain specific processes (like php.cgi, httpd, fast-cgi.cgi) a specific <strong>priority</strong>, where -19 is the most priority, and +19 is the least amount of priority, or CPU processing time.  I know it seems backwards.. </p>


<p><a id="tools" name="tools"></a></p><h2>The Tools</h2>
<p>If you run Windows, you are in the right place... because the following advice will save your life:  GET LINUX! Ok, now that that is out of the way, the following are the tools dicussed on this page.  All of them are free, open-source, and wonderful.  The basic idea of these tools is to control how much CPU is devoted to each process, and also how much Disk IO/Disk traffic is given to each process.</p>
<dl>
<dt><a href="#nice-tool">nice</a></dt><dd>run a program with modified scheduling priority</dd>
<dt><a href="#renice-tool">renice</a></dt><dd>alter priority of running processes</dd>
<dt><a href="#ionice-tool">ionice</a></dt><dd>set or retrieve the I/O priority for a given pid or execute a new task with a given I/O priority.</dd>
<dt><a href="#iostat-tool">iostat</a></dt><dd>Report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions.</dd>
<dt><a href="#ulimit-tool">ulimit</a></dt><dd>Ulimit provides control over the resources available to processes started by the shell, on systems that allow such control.</dd>
<dt><a href="#chrt-tool">chrt</a></dt><dd>set or retrieve real-time scheduling parameters for a given pid or execute a new task under given scheduling parameters.</dd>
<dt><a href="#taskset-tool">taskset</a></dt><dd>set or retrieve task CPU affinity for a given pid or execute a new task under a given affinity mask.</dd>
<dt></dt><dd></dd>
</dl>




<p><a id="part1-processes" name="part1-processes"></a></p><h2>Part 1: Process Processes Faster</h2>
<p>Ok so lets tackle figuring out how to give your response-intensive processes (like apache, php, ruby, perl, java) meaning a request to your server/machine requires a <em>response</em>.  For instance, when you requested this page that you are reading at this very second, several things on my server had to happen for you to be able to read this.</p>
<p>First your computer sends out a request to see what server the www.askapache.com domain name is.  DNS servers respond with my server IP, so for servers dedicated as nameservers, optimizing the DNS processes like bind would speed that up.  Now that your computer knows how to reach my server it sends an HTTP GET request for this url.  This request is received by the httpd process that is apache, and apache determines this url should be handled by my custom compiled php5.3.0 binary, because this page is WordPress generated.  So the php binary loads up the WordPress /index.php file, which chain-loads several other php files, including <code>wp-config.php</code> containing my MySql database settings.  Now php connects to my MySql Server to fetch this articles content, comments, title, tags, etc. and then generates the HTML and hands that back to Apache.</p>
<p>Finally, Apache generates a HTTP RESPONSE and sends the RESPONSE and CONTENT back to your Browser, which then in turn renders the page for your eyes with the necessary javascript, images, css, and other files included in the HTML response.</p>

<h3>Too much Processing</h3>
<p>Now you see why I've opted to write my own caching plugin that takes the php and mysql processes OUT of that equation.  Both the php binary and the mysql instance consume CPU processing, and disk IO, to load all their library files, make various network requests and sockets, check permissions, and on and on.  And that's completely ok, the thing is, unless you configure these processes (Apache, PHP, MySQL) they will use the same amount of CPU processing that other processes use, other processes that have very little to do with you reading this sentence.  Processes to run my mail server, my FTP server, my SSH server, my cronjobs, cleanup scripts, atd daemon, etc.. and they will get the same amount of CPU!</p>
<p>Another even simpler example is what got me to look into this myself.  I wrote a shell script that created hourly, daily, weekly, and monthly backups for all of my websites and sql databases, and set it up to run by cronjob at those set intervals.  Eventually I noticed my sites were slower, my php even slower, and sometimes I even saw 503 errors that my host throws up when my server is overloaded.  The research that I pursued to prevent that from happening has been hugely eye-opening.  What does a backup script do?  Mine just created tar archives of all the files in my web root, then gzipped the tar archive saving to a backup server using scp (a file transfer using ssh).  This resulted in the following huge problems that seem to have nothing to do with a faster server and speedier website, but they have everything to with it.</p>
<ol>
<li><strong>CPU Bottleneck #1</strong> - tar and gzip use compression algorithms at a low level to create a compressed version, and all that compressing uses a whole lot of crunching - CPU processing</li>
<li><strong>DISK IO Bottleneck</strong> - Tarring the whole web root directory was creating a ton of disk io, and remember the more disk io that is going on, the less is available for everything else.</li>
<li><strong>CPU Bottleneck #2</strong> - Using scp to send my backups was security-smart, but these huge archive files had to be encrypted and sent over the net.</li>
</ol>






<p><a id="breaking-bottlenecks" name="breaking-bottlenecks"></a></p><h2>Breaking Bottles</h2>
<p>I apologize for being a little long-winded there, but I think it's important to make sure everyone understands those basic concepts, which are foreign to most people.  Once you understand what is causing the bottlenecks, then you can understand the solutions, which actually are incredibly simple and even a novice linux user can easily do.  Besides, the net gets a little bit faster every time someone implements this.</p>

<p><a id="nice-tool" name="nice-tool"></a></p><h3>nice</h3>
<p><img src="http://uploads.askapache.com/2009/10/nice-chart.png" alt="NICE Levels Chart" title="NICE Levels Chart" width="351" height="225" class="IFL" />Nice allows you to run a program with modified scheduling priority which specifies how much CPU is devoted to a particular process.  Run COMMAND with an adjusted niceness, which affects process scheduling.  With no COMMAND, print the current niceness.  <br /><br />Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).   <code>-n, --adjustment=N</code> -  add integer N to the niceness (default 10).   <code>nice +19</code> tasks get a HZ-independent 1.5%.  Running a <code>nice +10</code> and a <code>nice +11</code> task means the first will get 55% of the CPU, the other 45%.<br class="C" /></p>

<p><a id="nice-usage" name="nice-usage"></a></p><h4>nice usage</h4>
<pre>nice [OPTION] [COMMAND [ARG]...]
&nbsp;
-n, --adjustment=ADJUST   increment priority by ADJUST first</pre>

<p><a id="nice-examples" name="nice-examples"></a></p><h4>Examples of nice</h4>
<p>Using nice to download a file</p>
<pre>nice -n 17 curl -q -v -A &#039;Mozilla/5.0&#039; -L -O http://wordpress.org/latest.zip</pre>
<p>Unzipping a file with nice</p>
<pre>nice -n 17 unzip latest.zip</pre>
<p>Nice way to build from source</p>
<pre>nice -n 2 ./configure
nice -n 2 make
nice -n 2 make install</pre>
<p>It is sometimes useful to run non-interactive programs with reduced priority.</p>
<pre>$ nice factor `echo &#039;2^9 - 1&#039;|bc`
511: 7 73</pre>
<p>Since nice prints the current priority, we can invoke it through itself to demonstrate how it works: The default behavior is to reduce priority by 10.</p>
<pre> $ nice nice
10
$ nice -n 10 nice
10</pre>
<p> The ADJUSTMENT is relative to the current priority.  The first <code>nice</code> invocation runs the second one at priority 10, and it in turn runs the final one at a priority lowered by 3 more.</p>
<pre>$ nice nice -n 3 nice
13</pre>
<p>Specifying a priority larger than 19 is the same as specifying 19.</p>
<pre>$ nice -n 30 nice
19</pre>
<p>Only a privileged user may run a process with higher priority.</p>
<pre>$ nice -n -1 nice
nice: cannot set priority: Permission denied
$ sudo nice -n -1 nice
-1</pre>

<blockquote cite="http://uploads.askapache.com/2009/08/sched-nice-design.txt">
<p>The new scheduler in v2.6.23 addresses all three types of complaints:</p>
<p>To address the first complaint (of nice levels being not "punchy" enough), the scheduler was decoupled from 'time slice' and HZ concepts (and granularity was made a separate concept from nice levels) and thus it was possible to implement better and more consistent nice +19 support: with the new scheduler nice +19 tasks get a HZ-independent 1.5%, instead of the variable 3%-5%-9% range they got in the old scheduler.</p>
<p>To address the second complaint (of nice levels not being consistent), the new scheduler makes nice(1) have the same CPU utilization effect on tasks, regardless of their absolute nice levels. So on the new scheduler, running a nice +10 and a nice 11 task has the same CPU utilization "split" between them as running a nice -5 and a nice -4 task. (one will get 55% of the CPU, the other 45%.) That is why nice levels were changed to be "multiplicative" (or exponential) - that way it does not matter which nice level you start out from, the 'relative result' will always be the same.</p>
<p>The third complaint (of negative nice levels not being "punchy" enough and forcing audio apps to run under the more dangerous SCHED_FIFO scheduling policy) is addressed by the new scheduler almost automatically: stronger negative nice levels are an automatic side-effect of the recalibrated dynamic range of nice levels.</p>
</blockquote>







<p><a id="renice-tool" name="renice-tool"></a></p><h3>renice</h3>
<p>Renice is similar to the nice command, but it lets you modify the nice of a currently running process.  This is nice for shell scripts where you can add this to the top of the script to nicify the whole script to 19.</p>

<p><a id="renice-usage" name="renice-usage"></a></p><h4>renice usage</h4>
<pre>renice priority [ [ -p ] pids ] [ [ -g ] pgrps ] [ [ -u ] users ]
&nbsp;
-g      Force who parameters to be interpreted as process group ID&#039;s.
-u      Force the who parameters to be interpreted as user names.
-p      Resets the who interpretation to be (the default) process ID&#039;s.</pre>

<p><a id="renice-examples" name="renice-examples"></a></p><h4>Examples of renice</h4>
<p>From the shell, changes the priority of the shell and all children to 19.  From a shell script, does the same but only for the script and its children.</p>
<pre>renice 19 -p $$</pre>
<p>This runs renice without any output</p>
<pre>renice 19 -p $$ &amp;&gt;/dev/null</pre>
<p>10 gets more CPU than 19</p>
<pre>renice 10 -p $$</pre>
<p>change the priority of process ID's 987 and 32, and all processes owned by users daemon and root.</p>
<pre>renice +1 987 -u daemon root -p 32</pre>









<p><a id="part2-disk-io" name="part2-disk-io"></a></p><h2>Part 2: Optimizing Disk I/O</h2>
<p><a id="scheduling-policies" name="scheduling-policies"></a></p><h3>Linux Scheduling Policies</h3>
<p>The scheduler is the kernel component that decides which runnable process will be executed by the CPU next.  Each process has an associated scheduling policy and a static scheduling priority, sched_priority</p>
<p>Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the <strong>range 1 (low) to 99 (high)</strong>.  (As the numbers imply, real-time processes always have higher priority than normal processes.)   The following "real-time" policies are also supported, for special time-critical applications that need precise control over the way in which runnable processes are selected for execution:</p>
<p>Currently, Linux supports the following "normal" (i.e., non-real-time) scheduling policies:</p>
<dl>
<dt><strong>SCHED_OTHER</strong>: Default Linux time-sharing scheduling</dt><dd>The standard round-robin time-sharing policy</dd><dt><strong>SCHED_BATCH</strong>: Scheduling batch processes</dt><dd>This policy is useful for workloads that are non-interactive, but do not want to lower their nice value, and for workloads that want a deterministic scheduling policy without interactivity causing extra preemptions (between the workload's tasks).</dd>
<dt><strong>SCHED_IDLE</strong>: Scheduling very low priority jobs</dt>
<dd>This policy is intended for running jobs at extremely low priority (lower even than a +19 nice value with the SCHED_OTHER or SCHED_BATCH policies)</dd>
<dt><strong>SCHED_FIFO</strong>: First In-First Out scheduling</dt><dd>A first-in, first-out policy</dd>
<dt><strong>SCHED_RR</strong>: Round Robin scheduling</dt><dd>A round-robin policy.</dd>
</dl>

<p><a id="scheduling-classes" name="scheduling-classes"></a></p><h3>Scheduling Classes</h3>
<dl>
<dt><code>IOPRIO_CLASS_RT</code></dt>
<dd>This is the realtime io class. The RT scheduling class is given first access to the disk, regardless of what else is going on in the system. Thus the RT class needs to be used with some care, as it can starve other processes. As with the best effort class, 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window.  This scheduling class is given higher priority than any other in the system, processes from this class are given first access to the disk every time. Thus it needs to be used with some care, one io RT process can starve the entire system. Within the RT class, there are 8 levels of class data that determine exactly how much time this process needs the disk for on each service. In the future this might change to be more directly mappable to performance, by passing in a wanted data rate instead.</dd>
<dt><code>IOPRIO_CLASS_BE</code></dt>
<dd>This is the best-effort scheduling class, which is the default for any process that hasn't set a specific io priority. This is the default scheduling class for any process that hasn't asked for a specific io priority. Programs inherit the CPU nice setting for io priorities. This class takes a priority argument from 0-7, with lower number being higher priority. Programs running at the same best effort priority are served in a round-robin fashion.  The class data determines how much io bandwidth the process will get, it's directly mappable to the cpu nice levels just more coarsely implemented. 0 is the highest BE prio level, 7 is the lowest. The mapping between cpu nice level and io nice level is determined as: io_nice = (cpu_nice + 20) / 5.</dd>
<dt><code>IOPRIO_CLASS_IDLE</code></dt>
<dd>This is the idle scheduling class, processes running at this level only get io time when no one else needs the disk. A program running with idle io priority will only get disk time when no other program has asked for disk io for a defined grace period. The impact of idle io processes on normal system activity should be zero. This scheduling class does not take a priority argument.    The idle class has no class data, since it doesn't really apply here.</dd>
</dl>








<p><a id="ionice-tool" name="ionice-tool"></a></p><h3>ionice</h3>
<p>ionice - get/set program io scheduling class and priority.  This program sets the io scheduling class and priority for a program.  Since v3 (aka CFQ Time Sliced) CFQ implements I/O nice levels similar to those of CPU scheduling. These nice levels are grouped in three scheduling classes each one containing one or more priority levels:</p>

<p><a id="ionice-usage" name="ionice-usage"></a></p><h4>ionice usage</h4>
<p>If no arguments or just -p is given, ionice will query the current io scheduling class and priority for that process.</p>
<pre>ionice [-c] [-n] [-p] [COMMAND [ARG...]]</pre>
<ul>
<li><strong>-c</strong> - The scheduling class. 1 for real time, 2 for best-effort, 3 for idle.</li>
<li><strong>-n</strong> - The scheduling class data. This defines the class data, if the class accepts an argument. For real time and best-effort, 0-7 is valid data.</li>
<li><strong>-p</strong> - Pass in a process pid to change an already running process. If this argument is not given, ionice will run the listed program with the given parameters.</li>
</ul>

<p><a id="ionice-examples" name="ionice-examples"></a></p><h4>ionice Examples</h4>
<p>Sets process with PID 89 as an idle io process.</p>
<pre>ionice -c3 -p89</pre>
<p>Runs 'bash' as a best-effort program with highest priority.</p>
<pre>ionice -c2 -n0 bash</pre>
<p>Returns the class and priority of the process with PID 89</p>
<pre>ionice -p89</pre>

<blockquote cite="http://gaarai.com/2009/03/06/multitasking-from-the-linux-command-line-plus-process-prioritization/">
<p><p>With the ionice command, you can set the IO priority for a process to one of three classes: Idle (3), Best Effort (2), and Real Time (1). The Idle class means that the process will only be able to read and write to the disk when all other processes are not using the disk. The Best Effort class is the default and has eight different priority levels from 0 (top priority) to 7 (lowest priority). The Real Time class results in the process having first access to the disk irregardless of other process and should never be used unless you know what you are doing.</p>
<p>If we wish to run the updatedb process in the background with an Idle IO class priority, we can run the following:</p>
<pre>$ sudo date
$ sudo updatedb &amp;
[1] 16324
$ sudo ionice -c3 -p16324</pre>
<p>If we’d rather just lower the Best Effort class priority (defaults to 4) for the command so the process isn’t limited to idle IO periods, we can run the following:</p>
<pre>$ sudo date
$ sudo updatedb &amp;
[1] 16324
$ sudo ionice -c2 -n7 -p16324</pre>
<p>Again, the Real Time class should not be used as it can prevent you from being able to interact with your system.</p>
<p>You may wonder where you can get the process ID if you don’t know it, can’t remember it, or didn’t start the process (an automatted script may have launched it). You can find process IDs with the ps command.</p>
<p>For example, if I had an updatedb program running in the background, and I wanted to find its process ID, I can run the following:</p>
<pre>$ ps -C updatedb
PID TTY TIME CMD
4234 ? 00:00:42 updatedb</pre>
<p>This tells me that the process’ process ID (PID) is 4234.</p></p>
</blockquote>





<p><a id="iostat-tool" name="iostat-tool"></a></p><h3>iostat</h3>
<p><a id="iostat-usage" name="iostat-usage"></a></p><h4>iostat Usage</h4>
<pre>iostat [ -c ] [ -d ] [ -N ] [ -n ] [ -h ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ -z ] [ &lt;device&gt; [...] | ALL ] [ -p [ &lt;device&gt; [,...] | ALL ] ] [ &lt;interval&gt; [ &lt;count&gt; ] ]
&nbsp;
-c     The -c option is exclusive of the -d option and displays only the CPU usage report.
-d     The -d option is exclusive of the -c option and displays only the device utilization report.
-k     Display statistics in kilobytes per second instead of blocks per second.  Data displayed are valid only with kernels 2.4 and newer.
-m     Display statistics in megabytes per second instead of blocks or kilobytes per second.  Data displayed are valid only with kernels 2.4 and newer.
-n     Displays the NFS-directory statistic.  Data displayed are valid only with kernels 2.6.17 and newer.  This option is exclusive ot the -x option.
-h     Display the NFS report more human readable.
-p [ { device | ALL } ]   The  -p  option  is  exclusive  of  the -x option and displays statistics for block devices and all their partitions that are used by the system.
-t     Print the time for each report displayed.
-x     Display extended statistics.</pre>

<p><a id="iostat-examples" name="iostat-examples"></a></p><h4>iostat Examples</h4>
<pre>iostat -p ALL 2 1000
avg-cpu:  %user   %nice    %sys %iowait   %idle
            8.34    0.08    1.26    2.27   88.05</pre>
<p>Display a single history since boot report for all CPU and Devices.</p>
<pre>$ iostat</pre>
<p>Display a continuous device report at two second intervals.</p>
<pre>$ iostat -d 2</pre>
<p>Display six reports at two second intervals for all devices.</p>
<pre>$ iostat -d 2 6</pre>
<p>Display six reports of extended statistics at two second intervals for devices hda and hdb.</p>
<pre>$ iostat -x hda hdb 2 6</pre>
<p>Display six reports at two second intervals for device sda and all its partitions (sda1, etc.)</p>
<pre>$ iostat -p sda 2 6</pre>






<p><a id="schedule-utils" name="schedule-utils"></a></p><h2>Schedule Utils</h2>
<p>These are the Linux scheduler utilities - schedutils for short.  These programs take advantage of the scheduler family of syscalls that Linux implements across various kernels.  These system calls implement interfaces for scheduler-related parameters such as CPU affinity and real-time attributes.  The standard UNIX utilities do not provide support for these interfaces -- thus this package.</p>
<p>The programs that are included in this package are chrt and taskset.  Together with nice and renice (not included), they allow full control of process scheduling parameters.  Suggestions for related utilities are welcome, although it is believed (barring new interfaces) that all scheduling interfaces are covered.</p>
<p>I've found that quite a few servers do not have this package installed, indicating to you that they might not know what they are doing.  Here is how you can install this incredible package, for non-root users.  Root users know how to do this, or they shouldn't be root.  Download and install in 1 line provided you have curl.  Or just use the following commands.</p>
<pre>mkdir -pv $HOME/{dist,source,bin,share/man/man1} &amp;&amp; cd ~/dist &amp;&amp; curl -O http://ftp.de.debian.org/debian/pool/main/s/schedutils/schedutils_1.5.0.orig.tar.gz &amp;&amp; cd ~/source &amp;&amp; tar -xvzf ~/dist/sch*z &amp;&amp; cd sch* &amp;&amp; sed -i -e &#039;s,= /usr/local,=${HOME},g&#039; Makefile &amp;&amp; make &amp;&amp; make install &amp;&amp; make installdoc</pre>
<pre>mkdir -pv $HOME/{dist,source,bin,share/man/man1}
cd ~/dist &amp;&amp; curl -O http://ftp.de.debian.org/debian/pool/main/s/schedutils/schedutils_1.5.0.orig.tar.gz
cd ~/source &amp;&amp; tar -xvzf ~/dist/schedutils_1.5.0.orig.tar.gz
cd ~/source/schedutils-1.5.0 &amp;&amp; sed -i -e &#039;s,= /usr/local,=${HOME},g&#039; Makefile
make || make -d &amp;&amp; make install || make install -d &amp;&amp; make installdoc || make installdoc -d</pre>


<p><a id="taskset-tool" name="taskset-tool"></a></p><h3>taskset</h3>
<p>Taskset  is  used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity.  CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system.  The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs.  Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons.  Therefore, forcing a specific CPU affinity is useful only in certain applications.</p>
<p>The  CPU  affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU.  Not all CPUs may exist on a given system but a mask may specify more CPUs than are present.  A retrieved mask will reflect only the bits that correspond to CPUs physically on the system.  If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned.  A user must possess CAP_SYS_NICE to change the CPU affinity of a process.  Any user can retrieve the affinity mask.</p>

<p><a id="taskset-usage" name="taskset-usage"></a></p><h4>taskset Usage</h4>
<pre>taskset [options] [mask | cpu-list] [pid | cmd [args...]]
&nbsp;
-p, --pid            operate on existing given pid
-c, --cpu-list     display and specify cpus in list format</pre>

<p><a id="taskset-examples" name="taskset-examples"></a></p><h4>taskset-examples</h4>
<p>The default behavior is to run a new command:</p>
 <pre>$ taskset 03 sshd -b 1024</pre>
<p>You can retrieve the mask of an existing task or set it:</p>
<pre>$ taskset -p 700
$ taskset -p 03 700</pre>
<p>List format uses a comma-separated list instead of a mask:</p>
<pre>$ taskset -pc 0,3,7-11 700</pre>




<p><a id="chrt-tool" name="chrt-tool"></a></p><h3>chrt</h3>
<p><code>chrt</code> sets or retrieves the real-time scheduling attributes of an existing PID or runs COMMAND with the given attributes.  Both policy (one of <code>SCHED_FIFO</code>, <code>SCHED_RR</code>, or <code>SCHED_OTHER</code>) and priority can be set and retrieved.  A user must possess CAP_SYS_NICE to change the scheduling attributes of a process.  Any user can retrieve the scheduling information.</p>

<p><a id="chrt-usage" name="chrt-usage"></a></p><h4>chrt Usage</h4>
<pre>chrt [options] [prio] [pid | cmd [args...]]
&nbsp;
-p, --pid operate on an existing PID and do not launch a new task
-f, --fifo set scheduling policy to SCHED_FIFO
-m, --max show minimum and maximum valid priorities, then exit
-o, --other set policy scheduling policy to SCHED_OTHER
-r, --rr set scheduling policy to SCHED_RR (the default)</pre>

<p><a id="chrt-examples" name="chrt-examples"></a></p><h4>chrt Examples</h4>
<p>The default behavior is to run a new command:   <code>chrt [prio] -- [command] [arguments]</code></p>
<p>You can also retrieve the real-time attributes of an existing task:</p>
<pre>chrt -p [pid]</pre>
<p>Or set them:</p>
<pre>chrt -p [prio] [pid]</pre>













<p><a id="ulimit-tool" name="ulimit-tool"></a></p><h2>ulimit - get and set user limits</h2>
<p>Ulimit provides control over the resources available to processes started by the shell, on systems that allow such control. One can set the resource limits of the shell using the built-in ulimit command.  The shell's resource limits are inherited by the processes that it creates to execute commands.</p>

<p><a id="ulimit-usage" name="ulimit-usage"></a></p><h4>ulimit Usage</h4>
<pre>ulimit [-SHacdfilmnpqstuvx] [limit]</pre>
<dl>
<dt>-S</dt><dd>use the `soft' resource limit</dd>
<dt>-H</dt><dd>use the `hard' resource limit</dd>
<dt>-a</dt><dd>all current limits are reported</dd>
<dt>-c</dt><dd>the maximum size of core files created</dd>
<dt>-d</dt><dd>the maximum size of a process's data segment</dd>
<dt>-f</dt><dd>the maximum size of files created by the shell</dd>
<dt>-l</dt><dd>the maximum size a process may lock into memory</dd>
<dt>-m</dt><dd>the maximum resident set size</dd>
<dt>-n</dt><dd>the maximum number of open file descriptors</dd>
<dt>-p</dt><dd>the pipe buffer size</dd>
<dt>-s</dt><dd>the maximum stack size</dd>
<dt>-t</dt><dd>the maximum amount of cpu time in seconds</dd>
<dt>-u</dt><dd>the maximum number of user processes</dd>
<dt>-v</dt><dd>the size of virtual memory</dd>
</dl>
<p>If LIMIT is given, it is the new value of the specified resource; the special LIMIT values `soft', `hard', and `unlimited' stand for the current soft limit, the current hard limit, and no limit, respectively.  Otherwise, the current value of the specified resource is printed.  If no option is given, then -f is assumed.  Values are in 1024-byte increments, except for -t, which is in seconds, -p, which is in increments of 512 bytes, and -u, which is an unscaled number of processes.</p>
<dl>
<dt>RLIMIT_AS</dt>
<dd>The maximum size of the process's virtual memory (address space) in bytes.  This limit affects calls to brk(2), mmap(2) and mremap(2), which fail with the error ENOMEM upon exceeding this limit.  Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)).  Since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.</dd>
<dt>RLIMIT_CORE</dt>
<dd>Maximum size of core file.  When 0 no core dump files are created. When non-zero, larger dumps are truncated to this size.</dd>
<dt>RLIMIT_CPU CPU</dt>
<dd>time limit in seconds.  When the process reaches the soft limit, it is sent a SIGXCPU signal.  The default action for this signal is to terminate the process.  However, the signal can be caught, and the handler can return control to the main program.  If the process continues to consume CPU time, it will be sent SIGXCPU once per second until the hard limit is reached, at which time it is sent SIGKILL. (This latter point describes Linux 2.2 through 2.6 behavior. Implementations vary in how they treat processes which continue to consume CPU time after reaching the soft limit.  Portable applications that need to catch this signal should perform an orderly termination upon first receipt of SIGXCPU.)</dd>
<dt>RLIMIT_DATA</dt>
<dd>The maximum size of the process's data segment (initialized data, uninitialized data, and heap).  This limit affects calls to brk(2) and sbrk(2), which fail with the error ENOMEM upon encountering the soft limit of this resource.</dd>
<dt>RLIMIT_FSIZE</dt>
<dd>The maximum size of files that the process may create.  Attempts to extend a file beyond this limit result in delivery of a SIGXFSZ signal. By default, this signal terminates a process, but a process can catch this signal instead, in which case the relevant system call (e.g., write(2), truncate(2)) fails with the error EFBIG.</dd>
<dt>RLIMIT_LOCKS</dt>
<dd>(Early Linux 2.4 only) A limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish.</dd>
<dt>RLIMIT_MEMLOCK</dt>
<dd>The maximum number of bytes of memory that may be locked into RAM.  In effect this limit is rounded down to the nearest multiple of the system page size.  This limit affects mlock(2) and mlockall(2) and the mmap(2) MAP_LOCKED operation.  Since Linux 2.6.9 it also affects the shmctl(2) SHM_LOCK operation, where it sets a maximum on the total bytes in shared memory segments (see shmget(2)) that may be locked by the real user ID of the calling process.  The shmctl(2) SHM_LOCK locks are accounted for separately from the per-process memory locks established by mlock(2), mlockall(2), and mmap(2) MAP_LOCKED; a process can lock bytes up to this limit in each of these two categories.  In Linux kernels before 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process.  Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock.</dd>
<dt>RLIMIT_MSGQUEUE</dt>
<dd>(Since Linux 2.6.8) Specifies the limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process.  This limit is enforced for mq_open(3).  Each message queue that the user creates counts (until it is removed) against this limit according to the formula:  <code>bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +             attr.mq_maxmsg * attr.mq_msgsize</code> where attr is the mq_attr structure specified as the fourth argument to mq_open(3).  The first addend in the formula, which includes sizeof(struct msg_msg *) (4 bytes on Linux/i386), ensures that the user cannot create an unlimited number of zero-length messages (such messages nevertheless each consume some system memory for bookkeeping overhead).</dd>
<dt>RLIMIT_NICE</dt>
<dd>(since Linux 2.6.12, but see BUGS below) Specifies a ceiling to which the process's nice value can be raised using setpriority(2) or nice(2).  The actual ceiling for the nice value is calculated as 20 - rlim_cur.  (This strangeness occurs because negative numbers cannot be specified as resource limit values, since they typically have special meanings.  For example, RLIM_INFINITY typically is the same as -1.)</dd>
<dt>RLIMIT_NOFILE</dt>
<dd>Specifies a value one greater than the maximum file descriptor number that can be opened by this process.  Attempts (open(2), pipe(2), dup(2), etc.)  to exceed this limit yield the error EMFILE. (Historically, this limit was named RLIMIT_OFILE on BSD.)</dd>
<dt>RLIMIT_NPROC</dt>
<dd>The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process.  Upon encountering this limit, fork(2) fails with the error EAGAIN.</dd>
<dt>RLIMIT_RSS</dt>
<dd>Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM).  This limit only has effect in Linux 2.4.x, x < 30, and there only affects calls to madvise(2) specifying MADV_WILLNEED.</dd>
<dt>RLIMIT_RTPRIO</dt>
<dd>(Since Linux 2.6.12, but see BUGS) Specifies a ceiling on the real-time priority that may be set for this process using sched_setscheduler(2) and sched_setparam(2).</dd>
<dt>RLIMIT_RTTIME</dt>
<dd>(Since Linux 2.6.25) Specifies a limit on the amount of CPU time that a process scheduled under a real-time scheduling policy may consume without making a blocking system call.  For the purpose of this limit, each time a process makes a blocking system call, the count of its consumed CPU time is reset to zero.  The CPU time count is not reset if the process continues trying to use the CPU but is preempted, its time slice expires, or it calls sched_yield(2). Upon reaching the soft limit, the process is sent a SIGXCPU signal.  If the process catches or ignores this signal and continues consuming CPU time, then SIGXCPU will be generated once each second until the hard limit is reached, at which point the process is sent a SIGKILL signal.  The intended use of this limit is to stop a runaway real-time process from locking up the system.</dd>
<dt>RLIMIT_SIGPENDING</dt>
<dd>(Since Linux 2.6.8) Specifies the limit on the number of signals that may be queued for the real user ID of the calling process.  Both standard and real-time signals are counted for the purpose of checking this limit.  However, the limit is only enforced for sigqueue(2); it is always possible to use kill(2) to queue one instance of any of the signals that are not already queued to the process.</dd>
<dt>RLIMIT_STACK</dt>
<dd>The maximum size of the process stack, in bytes.  Upon reaching this limit, a SIGSEGV signal is generated.  To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)).</dd>
</dl>

<p><a id="ulimit-examples" name="ulimit-examples"></a></p><h4>ulimit Examples</h4>
<p>Turn off core dumps</p>
<pre>ulimit -S -c 0</pre>








<h2>More Reading</h2>
<ul>
<li>Please see the <a href="http://pagesperso-orange.fr/sebastien.godard/">SYSSTAT Utilities Home for more performance monitoring tools</a> like sar, sadf, mpstat, iostat, pidstat and sa tools.</li>
<li><a href="http://gaarai.com/2009/03/06/multitasking-from-the-linux-command-line-plus-process-prioritization/">Multitasking from the Linux Command Line + Process Prioritization</a></li>
</ul>


<h2>Man Pages</h2>
<ol>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man2/sched_setscheduler.2.html">sched_setscheduler</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man7/cpuset.7.html">cpuset</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man7/signal.7.html">signal</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man2/getrlimit.2.html">getrlimit</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man3/ulimit.3.html">ulimit</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man2/ioprio_get.2.html">ioprio_get</a></li>
<li><a href="http://www.kernel.org/doc/man-pages/online/pages/man2/ioprio_set.2.html">ioprio_set</a></li>
</ol>


<h2>Kernel Documentation</h2>
<ul>
<li><a href='http://uploads.askapache.com/2009/08/sched-stats.txt'>information on schedstats (Linux Scheduler Statistics)</a></li>
<li><a href='http://uploads.askapache.com/2009/08/sched-rt-group.txt'>real-time group scheduling</a></li>
<li><a href='http://uploads.askapache.com/2009/08/sched-nice-design.txt'>How and why the scheduler's nice levels are implemented</a></li>
<li><a href='http://uploads.askapache.com/2009/08/sched-domains.txt'>information on scheduling domains</a></li>
<li><a href='http://uploads.askapache.com/2009/08/sched-design-CFS.txt'>goals, design and implementation of the Complete Fair Scheduler</a></li>
</ul>



<h2>Future Discussions:</h2>
<p><a href="http://www.cuddletech.com/blog/pivot/entry.php?id=820">IO Benchmarking: How, Why and With What</a></p><p><a href="http://www.askapache.com/optimize/optimize-nice-ionice.html"></a><a href="http://www.askapache.com/optimize/optimize-nice-ionice.html">Optimizing Servers and Processes for Speed with ionice, nice, ulimit</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/optimize/optimize-nice-ionice.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Serve External Javascript Files locally for Increased Speed</title>
		<link>http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html</link>
		<comments>http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html#comments</comments>
		<pubDate>Wed, 01 Oct 2008 06:55:46 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1542</guid>
		<description><![CDATA[<p><a class="IFL hs hs16" href="http://uploads.askapache.com/2008/10/google-external-javascript.png" title="Screenshot of bash shell script for downloading externally hosted javascript files"></a>One way I speed up AskApache.com is by downloading external third-party javascript files to host on my own server instead of externally.  In addition to the obvious speed boost, this lets you configure the caching and compression settings for the files.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html"></a><a href="http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html"><cite>AskApache.com</cite></a></p><p><a class="IFL hs hs16" href="http://uploads.askapache.com/2008/10/google-external-javascript.png" title="Screenshot of bash shell script for downloading externally hosted javascript files"></a>One of the ways I speed up AskApache.com is by downloading all the external javascript files to my server and then serve them from my own server instead of the external site.  Currently I'm using this method to serve the <a href="http://www.google-analytics.com/ga.js">Google Analytics</a> javascript file, and the <a href="http://edge.quantserve.com/quant.js">Quantcast</a> javascript file.<br class="C" /></p>


<h2>Speed Benefit</h2>
<p>The speed benefit occurs because normally a site visitor has to perform a DNS query for both the google-analytics.com and quantcast.com servers, create a connection to each server, and then download the files.  By hosting these scripts on your own server you remove all the extra DNS queries, and by hosting all the files on your single server you enable advanced HTTP 1/1 protocol features like Pipelining the requests, which means a single connection for multiple files.</p>


<h2>Cache and Compression Control</h2>
<p>Serving these javascript files from your own server gives you complete control over the caching and compression settings of those files, which may not seem like a big deal... unless you know about the advanced caching and compression techniques available to make your site super-fast.</p>

<h3>Caching Control</h3>
<p>By using the <a href="http://www.askapache.com/htaccess/mod_rewrite-fix-for-caching-updated-files.html">mod_rewrite cache hack</a> like I do, you can make sure that a fresh version of the <code>.js file</code> is used by your visitors.</p>

<h3>Compression Control</h3>
<p>You can also specify advanced compression for these files, which could mean using Apache's mod_deflate or mod_gzip modules, or you can even use a compression tool like Dojo's Shrinksafe, Packer, JSMin, or YUICompressor to further compress the javascript files.</p>


<h2>Automated Downloading with Crontab</h2>
<p>Using crontab I tell my web server to run this shell script every day, to make sure I have updated versions of all the files.  Here's the crontab entry I use to run this shell script every day.</p>
<pre>@daily /bin/sh /home/cron-scripts/grab_javascripts.sh 2&gt;&amp;1 &amp;&gt;/dev/null</pre>


<h2>Shell Script to Download JS</h2>
<p>I created a simple Bash shell-script to download the javascript files.  All you need to do is modify the variables to save to your directory and send your site as the Referring URL and you are good to go.  You can add as many files as you want by adding them to the JSFILE array.</p>
<p>You can also download the shell-script: <a href='http://uploads.askapache.com/2008/10/grab_javascripts.sh'>grab_javascripts.sh</a></p>
<pre>#!/bin/bash
# 2008-09-30
umask 022
VER="2.0"
&nbsp;
### Set as the referring url for downloads
SITE=http://www.askapache.com/
&nbsp;
### Directory to save the downloaded files
JSD=$HOME/j/
&nbsp;
### The files to download
JSFILE[0]=http://www.google-analytics.com/ga.js
&nbsp;
### run script with args to turn on debugging
[[ $# -ne 0 ]] &amp;&amp; set -o xtrace &amp;&amp; set -o verbose
&nbsp;
### SHELL OPTIONS
set +o noclobber # allowed to clobber files
set +o noglob # globbing on
set -e # abort on first error
shopt -s extglob
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# pt
#==-==-==-==-==-==-==-==-==-==-==#
function pt(){
  case ${1:-d} in
   i) echo -e "${C6}=&gt; ${C4}${2} ${C0}"; ;;
   m) echo -en "\n\n${C2}&gt;&gt;&gt; ${C4}${2} ${C0}\n\n"; ;;
   *) echo -e "\n${C8} DONE ${C0} \n"; ;;
  esac
}
&nbsp;
#=# CATCH SCRIPT KILLED BY USER
trap &#039;kill -9 $$&#039; SIGHUP SIGINT SIGTERM
&nbsp;
#=# MAKE MAIN SCRIPT NICE AS POSSIBLE SINCE IT DOESNT DO MUCH
renice 19 -p $$ &amp;&gt;/dev/null
&nbsp;
#=# TURNS ON COLORING ONLY FOR TERMS THAT CAN SUPPORT IT
C="\033[";C0=;C1=;C2=;C3=;C4=;C5=;C6=;C7=;C8=;C9=;
  C0="${C}0m";C1="${C}1;30m";C2="${C}1;32m";C3="${C}0;32m";C4="${C}1;37m"
  C5="${C}0;36m";C6="${C}1;35m";C7="${C}0;37m";C8="${C}30;42m";C9="${C}1;36m";
clear
echo -e "${C1} __________________________________________________________________________ "
echo -e "| ${C2}             ___       __    ___                 __             ${C1}         |"
echo -e "| ${C2}            / _ | ___ / /__ / _ | ___  ___ _____/ /  ___        ${C1}         |"
echo -e "| ${C2}           / __ |(_-&lt;/  &#039;_// __ |/ _ \/ _ \`/ __/ _ \/ -_)       ${C1}         |"
echo -e "| ${C3}          /_/ |_/___/_/\_\/_/ |_/ .__/\_,_/\__/_//_/\__/        ${C1}         |"
echo -e "| ${C3}                               /_/                              ${C1}         |"
echo -e "|                                                                          |"
echo -e "|                 ${C4} LOCAL JAVASCRIPT FILES SCRIPT Version ${VER} ${C1}              |"
echo -e "${C1} __________________________________________________________________________ ${C0} \n\n"
&nbsp;
#=# BUILD INCOMING DIRS
[[ ! -d "${JSD}" ]] &amp;&amp; pt m "BUILDING DIRS" &amp;&amp; mkdir -p $JSD &amp;&gt;/dev/null &amp;&amp; pt i "CREATED $JSD" &amp;&amp; pt
&nbsp;
#=# DOWNLOAD JAVASCRIPT FILES
cd $JSD &amp;&amp; pt m "DOWNLOADING JAVASCRIPT FILES"
for theurl in "${JSFILE[@]}"; do
pt i "${theurl}"
curl -m 60 --connect-timeout 10 --retry 10 --retry-delay 180 -s -S -L -e &#039;${SITE}&#039; -A &#039;Mozilla/5.0&#039; -O "${theurl}"
done
pt &amp;&amp; cd $OLDPWD
&nbsp;
######## curl options
# -S Show error. With -s, make curl show errors when they occur
# -s Silent mode. Don&#039;t output anything
# -e  Referer URL (H)
# -H &lt;line&gt; Custom header to pass to server (H)
# -L  Follow Location: hints (H)
# -A &lt;string&gt; User-Agent to send to server (H)
# -m &lt;seconds&gt; Maximum time allowed for the transfer
# --connect-timeout &lt;seconds&gt; Maximum time allowed for connection
# --globoff  Disable URL sequences and ranges using {} and []
# -O  Write output to a file named as the remote file
# --retry &lt;num&gt;  Retry request &lt;num&gt; times if transient problems occur
# --retry-delay &lt;seconds&gt; When retrying, wait this many seconds between each
# --retry-max-time &lt;seconds&gt; Retry only within this period
#############################################################################
&nbsp;
exit 0</pre><p><a href="http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html"></a><a href="http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html">Serve External Javascript Files locally for Increased Speed</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/javascript/serve-external-javascript-files-locally-for-increased-speed.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elite Log File Scrolling with Color Syntax</title>
		<link>http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html</link>
		<comments>http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html#comments</comments>
		<pubDate>Sat, 09 Aug 2008 04:56:10 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1067</guid>
		<description><![CDATA[<p><a class="IFR hs hs07" href="http://www.askapache.com/security/elite-log-file-scrolling-with-color-syntax.html" title="Colored Server Log Scrolling"></a>Scrolls the latest log entries for multiple log files to the current screen or to any other monitor or TTY <strong>in color using syntax highlighting</strong>, making debugging easier and saving a lot of time for multi-monitor workstations.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html"></a><a href="http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html"><cite>AskApache.com</cite></a></p><p><a class="IFR" rel="lb" href="http://uploads.askapache.com/2008/08/colored-logs2.png"><img src="http://uploads.askapache.com/2008/08/colored-logs2.png" alt="Scroll Logs on Alternate Monitor for Server Debugging" title="colored-logs2" width="350" height="143" /></a>Ok peeps, one of the coolest hacks yet.  If you have multiple PC's/Monitors at your workstation like I do, it can be helpful to display various information on one screen while you work on another.<br class="C" /></p>

<p>This article shows how I <strong>continuously scroll the logs</strong> for a server/site I am working on, thus saving me a lot of time by providing <em>real-time debugging</em> on a separate screen.  Not only does this scroll the latest log entries as they are created, it <strong>displays them in color using syntax highlighting</strong> to make your logs incredibly easy to understand and parse.</p>


<h2>Example Output</h2>
<p>The thing to realize is that this output is continuously scrolling on your monitor, and using some cool linux shell tricks you can make it output at a certain speed and show a certain number of lines.  Another cool feature is you can display multiple files at the same time, and the filename will be output for each file above the log output.</p>
<p><a rel="lb" href="http://uploads.askapache.com/2008/08/colored-logs1.png"><img src="http://uploads.askapache.com/2008/08/colored-logs.png" alt="Colored Apache Server Logs Scrolling Display" title="Colored Server Log Scrolling" width="640" height="570" /></a></p>


<h2>What Logs</h2>
<p>I've explained in various articles on this site how to create and use various custom log files using .htaccess files and other tricks for non-root users. I use shared hosting all the time too you know...  Here are some log files you can generally use if you can use .htaccess files <em>(if you can control .htaccess you can control a binary php-cgi, which could be a shell script thats execs the php binary of your choice with your own environment variables, which is a workaround to getting a custom php.ini, which is how you specify a php error log)</em>.</p>
<ul>
<li>php error log</li>
<li>mod_security audit log</li>
<li>mod_security debug log</li>
<li>apache error log</li>
<li>apache access log</li>
</ul>
<p>Any log file can be used with this method, actually ANY file containing text can be used with this method, even fifo pipes as we will see in a bit ;)</p>



<h2>Installation</h2>
<h3>PCRE</h3>
<p>First you need <a href="http://www.pcre.org/">PCRE</a> - Perl Compatible Regular Expressions, which you can download <a href="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/">here</a>.  Note that I had to install <a href="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-6.7.tar.gz">pcre version 6.7</a> to get it to work with ccze.</p>

<h3>CCZE</h3>
<p>Now you need <a href="http://freshmeat.net/projects/ccze/">CCZE</a>, which colorizes and outputs (emulating tail) the log files.</p>
<blockquote><p>CCZE is a robust and modular log colorizer with plugins for apm, exim, fetchmail, httpd, postfix, procmail, squid, syslog, ulogd, vsftpd, xferlog, and more.</p></blockquote>


<h2>Post-Install Setup</h2>
<p>Ok so once you've installed ccze you can start using it right away, or you can continue reading to see how I've set it up for readable scrolling.  If the following setup isn't to your taste, you can always just use <a href="http://www.teaser.fr/~amajorel/wtail/wtail-0.2.2.tar.gz">wtail</a>, a colorless multi-file scroller that uses separate scrolling windows in your terminal similar to the screen program.</p>

<h3>Create Logs Folder</h3>
<p>First I created a folder called <code>/logs/</code> for my site, then for all subsequent commands chdir to it.</p>
<pre class='prebash'>$ mkdir /home/site.com/logs
$ cd /home/site.com/logs</pre>

<h3>Create Symlinks to Logs</h3>
<p>In the new logs folder I created soft symlinks to all the various log files, not neccessary but makes everything much easier and organized.</p>
<pre class='prebash'>$ ln -s /actual/logs/site.com/http/access.log access.log
$ ln -s /actual/logs/site.com/http/error.log error.log
$ ln -s /home/site.com/php_error.log php_error.log
$ ln -s /home/site.com/modsec_audit.log modsec_audit.log
$ ln -s /home/site.com/modsec_debug.log modsec_debug.log</pre>

<h3>Make a fifo Pipe</h3>
<p>FIFO stands for first-in-first-out and is a somewhat complex feature of linux/bsd/unix shells that let you send data into it and read the data that comes out of it, just like a pipe.</p>
<pre class='prebash'>$ mkfifo pipe</pre>



<h2>Scrolling the Logs</h2>
<p><strong>First</strong> cd to the log directory you created.</p>
<pre class='prebash'>$ cd /home/site.com/logs</pre>

<p>Now we will use the tail program to output 120 lines of each of these log files every half of a second, only displaying changes/new log entries.  We send that output into the fifo pipe we created using shell redirection and then instruct the command to run in the background using the ampersand to access built-in <em>(bash/sh)</em> job control.</p>
<pre class='prebash'>$ tail -s .5 -n 120 -f access.log php_error.log error.log modsec_audit.log &gt;pipe &amp;</pre>

<h3>Displaying the Colorized Scrolling Output</h3>
<p>So every .5 seconds the tail command outputs any new log entries from those files into the fifo pipe, so now we need to use the ccze program to use the fifo pipe as its input log file.  Normally you run ccze like the cat command and it just outputs the input colorized, by using tail and a fifo pipe we are able to make this awesome trick work.</p>

<h4>To current TTY</h4>
<pre class='prebash'>$ ccze &lt;pipe</pre>

<h4>To any TTY</h4>
<pre class='prebash'>$ ccze &lt; pipe &gt;/dev/pts/2 &amp;</pre>



<h3>Save All Colored Output</h3>
<p>This appends the colorized output to the file in the CWD, super.log</p>
<pre class='prebash'>$ ccze -A &lt;pipe | tee -a super.log</pre>


<h3>Kill the Scrolling</h3>
<p>Immediately kills any processes used by tail and ccze.</p>
<pre class='prebash'>$ pkill -9 ccze\|tail</pre>





<h2>Going Further. Hackers only.</h2>
<p>Here are some other examples of using ccze with fifo pipes, more as an excercise than anything practical as most lines don't work, I just grabbed them from my shell history file.</p>
<pre class='prebash'>tail -f access.log &gt;pipe &amp; ccze &lt;pipe | tee -a super.log &gt;/dev/pts/2
tail -f access.log &gt;pipe &amp; ccze &lt;pipe | tee -a super.log &gt;/dev/pts/2 &amp;
&nbsp;
exec 3&lt;&gt; pipe; while ccze &gt;/dev/pts/2 &lt;&amp;3; do tail -f access.log &gt;pipe; done; exec 3&gt;&amp;-
exec 3&lt;&gt; pipe; ccze &lt;&amp;3 &gt;/dev/pts/2 &amp; ; tail -f access.log &gt;pipe; exec 3&gt;&amp;-
exec 3&lt;&gt; pipe; ccze &lt;&amp;3 &gt;/dev/pts/2 &amp; ; tail -f access.log &gt;pipe
exec 3&lt;&gt; pipe; ccze &lt;&amp;3 &gt;/dev/pts/2 &amp; tail -f access.log &gt;pipe
ccze &lt;&amp;3 &gt;/dev/pts/2 &amp;
ccze 0&lt;&amp;3 |tee -a super.log &gt; /dev/pts/2 &amp;
ccze &lt;&amp;3 |tee -a super.log &gt; /dev/pts/2 &amp;
ccze &lt;pipe |tee -a /dev/pts/2 &amp; tail -s .5 -n 120 -f php_error.log error.log modsec_audit.log &gt;pipe &amp;
ccze | tee -a /dev/pts/2 &lt;pipe
&nbsp;
ccze &lt; tee -a /dev/pts/2 pipe &gt;/dev/pts/2
ccze &lt;|tee -a /dev/pts/2 pipe &gt;/dev/pts/2
ccze &lt;tee -a /dev/pts/2 &lt;pipe
ccze &lt;pipe &gt;&gt; tee -a super.log &gt;/dev/pts/2
cat &lt;pipe | ccze
ccze &lt;pipe &gt;&gt;/dev/pts/2 &amp;
&nbsp;
tail -fq access.log | ccze  &amp;&gt;/dev/pts/1
tail -qf access.log |ccze &amp;&gt;/dev/pts/1 &amp;
tail -f access.log | ccze &gt; $SSH_TTY
&nbsp;
( ccze &gt; /dev/pts/2 )&lt;pipe
( ccze &gt; /dev/pts/2 )&lt;pipe
( ccze &gt; /dev/pts/2 )&lt;pipe &amp; tail -f access.log | pipe
( ccze &gt; /dev/pts/2 )&lt;pipe &amp; tail -f access.log | ./pipe
&nbsp;
ccze &lt;pipe &gt; /dev/pts/2 &amp; tail -f access.log &gt;pipe
ccze  &lt;pipe&gt; /dev/pts/2 &amp; tail -f access.log &gt;pipe
ccze  &lt; pipe &gt; /dev/pts/2 &amp; tail -f access.log &gt;pipe
ccze &lt; pipe &gt;/dev/pts/2 &amp; tail -f access.log &gt;pipe
ccze &lt; pipe &gt;/dev/pts/2 &amp; tail -f access.log &gt;pipe &amp;
ccze &lt; pipe &gt;/dev/pts/2 &amp; tail -n 40 -s 2 -f error.log modsec_audit.log php_error.log  &gt;pipe &amp;
ccze &lt; pipe &gt;| tee -a super.log | &gt;/dev/pts/2 &amp; tail -n 80 -s 1 -f error.log modsec_audit.log php_error.log  &gt;pipe &amp;
ccze &lt; pipe &gt;/dev/pts/2 &amp; tail -n 100 -s 1 -f error.log modsec_audit.log php_error.log &gt;pipe &amp;
ccze &lt; pipe &gt;/dev/pts/2 &amp; tail -n 100 -s 1 -f error.log modsec_audit.log php_error.log &gt;pipe &amp;
&nbsp;
tail -f access.log | ccze &gt; pipe &amp; /dev/pts/2/&lt;pipe
tail -f access.log | ccze &gt; pipe &amp; /dev/pts/2&lt;pipe
&nbsp;
ccze &lt; $(tail -n 20 -s 1 -f access.log)
ccze &lt; | $(tail -n 20 -s 1 -f access.log)
ccze &lt; |$(tail -n 20 -s 1 -f access.log)</pre>


<h3>Shell Script</h3>
<p>I also hacked together a little shell script that you may wish to hack to your needs.  Its actually pretty sweet if you can figure it out.</p>
<pre class='prebash'>#!/bin/bash -l
set -o xtrace
renice -p $$ 15
&nbsp;
pkill -9 tee\|ccze &amp;&gt;/dev/null || echo -n
disown -a &amp;&gt;/dev/null || echo
&nbsp;
[[ -r "~/pipe" ]] &amp;&amp; rm -rf ~/pipe
&nbsp;
mkfifo ~/pipe
&nbsp;
cd /home/askapache.com/logs
&nbsp;
[[ -r "superlog.log" ]] &amp;&amp; echo "Found old logfile, moving." &amp;&amp; mv superlog.log `command ls|wc -l`-superlog.log
&nbsp;
ccze &lt;$HOME/pipe &gt; $SSH_TTY &amp; tail -n 150 -s .5 -f  php_error.log error.log modsec_audit.log &gt;$HOME/pipe &amp;
disown %2; &amp;&amp; disown %3;
&nbsp;
disown -a
&nbsp;
sleep 60 &amp;
disown $! || disown %1
&nbsp;
for i in `seq 1 4`;do echo -n $&#039;\a&#039;; sleep 1;done
kill -9 $J1
kill -9 $J2
&nbsp;
logout
exit 0</pre><p><a href="http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html"></a><a href="http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html">Elite Log File Scrolling with Color Syntax</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/linux/elite-log-file-scrolling-color-syntax.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Encrypted WordPress / phpBB Backups</title>
		<link>http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html</link>
		<comments>http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html#comments</comments>
		<pubDate>Mon, 04 Aug 2008 16:28:00 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1049</guid>
		<description><![CDATA[<p><a rel="lb" class="IFL hs hs11" href='http://www.askapache.com/linux/encrypted-wordpress-site-backups.html' title="Bash Shell Script for Encrypted WordPress and phpBB Backups"></a>Enter your DOMAIN_ROOT and the location of your wp-config.php or config.php, and this script finds all the mysql settings by parsing the phpbb or wordpress config file, then creates GPG encrypted backups, and saves your settings for future automation.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html"></a><a href="http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html"><cite>AskApache.com</cite></a></p><p><a rel="lb" class="IFL hs hs11" href='http://uploads.askapache.com/2008/07/sbackup.png' title="Bash Shell Script for Encrypted WordPress and phpBB Backups"></a>Because backups contain all your sensitive information, its smart to encrypt any sql backups.. and while we're at it, also encrypt any site backups.<br /><br />This simple shell-script is a useful and easy way to securely backup your wordpress and/or phpBB site files and database without confusing you.  Just generate a GPG key once, enter in 3 settings once, and from then on it runs without any user-input whenever you want.<br class="C" /></p>


<h2>What it Does</h2>
<p>When run, this script asks you for the location of your websites document root and the location of your wp-config.php or config.php file.  It also asks you for your encryption UID.  Then this script saves those settings in a file called .sbackup so that the next time you run the script it will run without having to re-enter that information, making it nice for cronjobs or quick and easy on-demand backups.  Another cool feature that I added is this script automatically parses your wp-config.php file for the mysql database name, user, host, and password, meaning you don't have to compromise your security or take the time to type those settings in manually.</p>


<h2>What is Backed Up</h2>
<p>This script creates a tarred and gzipped archive of your entire document root in the folder <code>~/backups/domain.com/domain.com-date.tgz</code> and also creates a backup of your WordPress database and phpBB database in a format that is ideal for restoring from.  Both of these files are then encrypted using your GPG key and can then be safely downloaded as a password and key is required to decrypt them.</p>


<h2>Generating a GPG Key</h2>
<p>If you don't already have one setup for your shell account run this command remembering the uid which you will enter in the shell script.</p>
<pre>gpg --gen-key</pre>

<h3>Decrypting Files</h3>
<pre>gpg -r UID --output FILENAME.tgz --decrypt FILENAME.tgz.asc</pre>


<h2>The Shell Script</h2>
<p><a href='http://uploads.askapache.com/2008/12/_sbackup.sh'>site-backup.sh</a></p>

<pre>#!/bin/bash
# SiteBack Version 3.3, 2008-12-17
# GNU Free Documentation License 1.2
# 12-17-08 - AskApache (www.askapache.com)
umask 022
&nbsp;
### SHELL OPTIONS
set +o noclobber # allowed to clobber files
set +o noglob # globbing on
set +o xtrace # change to - to enable tracing
set +o verbose # change to - to enable verbose debugging
set -e # abort on first error
shopt -s extglob
&nbsp;
###########################################################################--=--=--=--=--=--=--=--=--=--=--#
###
### SETTINGS
###
###########################################################################==-==-==-==-==-==-==-==-==-==-==#
&nbsp;
DT=$(date +%x); DT=${DT//\/}
DTX=$(date +%x-%H%M); DTX=${DTX//\/}
BDIR=${HOME}/backups
RUN_FILE=${BDIR}/$$.bk.log
MY_CONFIG=".sbackup"
DOMAIN=;DB_NAME=;DB_USER=;DB_PASSWORD=;DB_HOST=;APP_CONFIG=;SQL_DEST=;ARC_DEST=;ENCRYPT_USER=
E_SUCCESS=0;E_YN=0;E_YES=251;E_NO=250;E_RETURN=65;C0=;C1=;C2=;C3=;C4=;C5=;C5=;C7=
&nbsp;
###########################################################################--=--=--=--=--=--=--=--=--=--=--#
###
### FUNCTIONS
###
###########################################################################==-==-==-==-==-==-==-==-==-==-==#
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# script_title
#==-==-==-==-==-==-==-==-==-==-==#
function script_title(){
 local e="\033["
 local l=&#039; ___________________________________________________________________ &#039;
&nbsp;
 # SET WINDOW TITLE AND COLORS IF CLIENT CAPABLE
 case $TERM in xterm*|vt*|ansi|rxvt|gnome*)
 C0="${e}0m";C1="${e}1;30m";C2="${e}1;32m";C3="${e}0;32m";C4="${e}1;37m";C5="${e}1;35m";C6="${e}30;42m"
 esac
&nbsp;
 echo -e "\n${C0}$l${C1}"
 echo -e "|             ${C2}___       __    ___                 __${C1}                |"
 echo -e "|            ${C2}/ _ | ___ / /__ / _ | ___  ___ _____/ /  ___${C1}           |"
 echo -e "|           ${C2}/ __ |(_-&lt;/  &#039;_// __ |/ _ \/ _ \`/ __/ _ \/ -_)${C1}          |"
 echo -e "|          ${C3}/_/ |_/___/_/\_\/_/ |_/ .__/\_,_/\__/_//_/\__/${C1}           |"
 echo -e "|                               ${C3}/_/${C1}                                 |"
 echo -e "|                                                                   |"
 echo -e "|       ${C1}+--${C0} SITE BACKUP SCRIPT Version 3.3${C1}                          |"
 echo -e "${C0}$l\n\n"
}
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# pm
#==-==-==-==-==-==-==-==-==-==-==#
function pm(){
 START=$(date +%s) &amp;&amp; touch ${RUN_FILE}
 case "${2:-title}" in
  "title") echo -en "\n\n${C2}&gt;&gt;&gt; ${C4}${1} ${C0} \n\n"; ;;
   "info") echo -e "${C5}=&gt; ${C4}${1} ${C0}"; ;;
   "item") echo -e "${C4}-- ${C0}${1} "; ;;
 esac
}
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# yes_no
#==-==-==-==-==-==-==-==-==-==-==#
function yes_no(){
 local ans
 echo -en "${1} [y/n] " ; read -n 1 ans
 case "$ans" in
  n|N) E_YN=$E_NO ;;
  y|Y) E_YN=$E_YES ;;
 esac
}
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# do_sleep
#==-==-==-==-==-==-==-==-==-==-==#
function do_sleep (){
 local END DIFF
 echo -en "${C5}${3:-.}"; while [ -r "$RUN_FILE" ]; do sleep ${2:-3}; echo -en "${3:-.}"; done;
 echo -e "${C0}"; sleep 1; END=$(date +%s);DIFF=$(( $END - $START ))
 echo -e "\n${C6} [T: ${SECONDS}] COMPLETED IN ${DIFF} SEC ${C0} \n\n"; sleep 1;
 return 0;
}
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# get_settings
#==-==-==-==-==-==-==-==-==-==-==#
function get_settings(){
 local cha HOSTED_SITES G GG
 clear; script_title
&nbsp;
 if [[ -r "$MY_CONFIG" ]]; then
&nbsp;
  OIFS=$IFS; while IFS=: read DOMAIN DOMAINROOT APP_CONFIG ENCRYPT_USER; do
   DOMAIN=${DOMAIN};
   DOMAINROOT=${DOMAINROOT};
   APP_CONFIG=${APP_CONFIG};
   ENCRYPT_USER=${ENCRYPT_USER};
   #E_YN=$E_YES;
   break
  done &lt;${MY_CONFIG};
  IFS=$OIFS
&nbsp;
 else
&nbsp;
  gpg --list-keys|grep uid.*|awk &#039;{print $2}&#039;
  echo -en "\n What userid to use for encryption?  ";
  read -e ENCRYPT_USER; echo
&nbsp;
  echo -en "\n What domain would you like to backup?  "; read -e DOMAIN; echo
&nbsp;
  echo $PWD
  until [ -d "$DOMAINROOT" ]; do echo -en "\n Folder where config file is located?  ";
  read -e DOMAINROOT; echo; done
&nbsp;
  [[ -r "$DOMAINROOT/config.php" ]] &amp;&amp; APP_CONFIG=$DOMAINROOT/config.php &amp;&amp; DOT=PHP
  [[ -r "$DOMAINROOT/wp-config.php" ]] &amp;&amp; APP_CONFIG=$DOMAINROOT/wp-config.php &amp;&amp; DOT=WP
&nbsp;
  echo $PWD
  until [[ -r "$APP_CONFIG" ]]; do echo -en "\n Where is the applications config file?  "; read -e APP_CONFIG; echo; done
&nbsp;
 fi
&nbsp;
  [[ -r "$DOMAINROOT/config.php" ]] &amp;&amp; APP_CONFIG=$DOMAINROOT/config.php &amp;&amp; DOT=PHP
  [[ -r "$DOMAINROOT/wp-config.php" ]] &amp;&amp; APP_CONFIG=$DOMAINROOT/wp-config.php &amp;&amp; DOT=WP
&nbsp;
  ### For phpBB
  if [[ "${DOT}" == "PHP" ]]; then
    GG=$(sed -e &#039;/$db\(n\|u\|pa\|h\)/!d&#039; -e "s/$db_\(name\|user\|passwd\|host\)\ =\ &#039;\([^&#039;]*\).*\$/\1=&#039;\2&#039;;/g" -e &#039;s/$db/DB_/g&#039; ${APP_CONFIG});
    G=$(echo ${GG}|sed -e &#039;s/DB_name/DB_NAME/g&#039; -e &#039;s/DB_user/DB_USER/g&#039; -e &#039;s/DB_passwd/DB_PASSWORD/g&#039; -e &#039;s/DB_host/DB_HOST/g&#039;);
  else
    G=$(sed -e "/define(&#039;DB_\(NAME\|USER\|PASSWORD\|HOST\)/!d" -e "s/[^&#039;]*&#039;DB_\(NAME\|USER\|PASSWORD\|HOST\)&#039;[^&#039;]*&#039;\([^&#039;]*\)&#039;.*$/DB_\1=&#039;\2&#039;;/g" ${APP_CONFIG})
  fi
  eval $G;
&nbsp;
 mkdir -p ${BDIR}/${DOMAIN}
 SQL_DEST=${BDIR}/${DOMAIN}/${DOMAIN}-${DT}.sql;
 [[ -r "${SQL_DEST}.asc" ]] &amp;&amp; SQL_DEST=${BDIR}/${DOMAIN}/${DOMAIN}-${DTX}.sql
&nbsp;
 ARC_DEST=${BDIR}/${DOMAIN}/${DOMAIN}-${DT}.tgz;
 [[ -r "${ARC_DEST}.asc" ]] &amp;&amp; ARC_DEST=${BDIR}/${DOMAIN}/${DOMAIN}-${DTX}.tgz
&nbsp;
 if [[ "$E_YN" != "$E_YES" ]]; then
  for a in "DOMAIN" "DOMAINROOT" "APP_CONFIG" "ENCRYPT_USER" "DB_NAME" "DB_USER" "DB_PASSWORD" "DB_HOST"; do echo -e "${a}: ${!a}"; done
  echo; yes_no "ARE THESE SETTINGS CORRECT"
 fi
&nbsp;
 while [[ "$E_YN" != "$E_YES" ]]; do
  for a in "DOMAIN" "DOMAINROOT" "APP_CONFIG" "ENCRYPT_USER" "DB_NAME" "DB_USER" "DB_PASSWORD" "DB_HOST"; do
   echo -en "\n (Enter for Default: ${!a} )\n ${a}:&gt; "
   read -e cha; echo; [[ ${#cha} -gt 2 ]] &amp;&amp; eval "$a"=$cha
  done
  yes_no "ARE THESE SETTINGS CORRECT"
 done
&nbsp;
 echo -e "${DOMAIN}:${DOMAINROOT}:${APP_CONFIG}:${ENCRYPT_USER}" &gt; $MY_CONFIG
}
&nbsp;
#--=--=--=--=--=--=--=--=--=--=--#
# exit_cleanup
#==-==-==-==-==-==-==-==-==-==-==#
function exit_cleanup(){
 cd $OLDPWD
 [[ -r "${SQL_DEST}" ]] &amp;&amp; rm ${SQL_DEST}
 [[ -r "${ARC_DEST}" ]] &amp;&amp; rm ${ARC_DEST}
}
&nbsp;
############################################################################################################
###
### MAIN CODE
###
############################################################################################################
&nbsp;
#=# CATCH SCRIPT KILLED BY USER
trap exit_cleanup SIGHUP SIGINT SIGTERM
&nbsp;
#=# MAKE MAIN SCRIPT NICE
renice 19 -p $$ &amp;&gt;/dev/null
&nbsp;
cd `dirname $0`
&nbsp;
get_settings
&nbsp;
pm "CREATING SQL BACKUP"
mysqldump --opt -u${DB_USER} -p${DB_PASSWORD} -h ${DB_HOST} -r ${SQL_DEST} --add-drop-table ${DB_NAME} 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; sleep 2 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; rm ${RUN_FILE} 2&gt;&amp;1&amp;
do_sleep 1 1 ":"
&nbsp;
pm "ENCRYPTING SQL BACKUP"
gpg --armor --recipient ${ENCRYPT_USER} --output ${SQL_DEST}.asc --encrypt ${SQL_DEST} 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; sleep 2 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; rm ${RUN_FILE} 2&gt;&amp;1&amp;
do_sleep 1 1 ":"; rm ${SQL_DEST}
&nbsp;
pm "CREATING ARCHIVE BACKUP"
tar -czf ${ARC_DEST} . 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; rm ${RUN_FILE} 2&gt;&amp;1&amp;
do_sleep 1 5 ":"
&nbsp;
pm "ENCRYPTING ARCHIVE BACKUP"
gpg --armor --recipient ${ENCRYPT_USER} --output ${ARC_DEST}.asc --encrypt ${ARC_DEST} 1&gt;&amp;2 &amp;&gt;/dev/null &amp;&amp; rm ${RUN_FILE} 2&gt;&amp;1&amp;
do_sleep 1 1 ":"; rm ${ARC_DEST}
&nbsp;
echo -e "${C1} __________________________________________________________________________ "
echo -e "|                                                                          |"
echo -e "|                 ${C4} COMPLETED SUCCESSFULLY ${C1}                                 |"
echo -e "${C1} __________________________________________________________________________ ${C0} \n\n"
&nbsp;
cd $OLDPWD
&nbsp;
exit $?</pre><p><a href="http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html"></a><a href="http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html">Encrypted WordPress / phpBB Backups</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/shellscript/encrypted-wordpress-site-backups.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Automated Folder Backup Shell-Script</title>
		<link>http://www.askapache.com/shellscript/snapshot-backups-shell-script.html</link>
		<comments>http://www.askapache.com/shellscript/snapshot-backups-shell-script.html#comments</comments>
		<pubDate>Thu, 12 Jun 2008 05:22:12 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1021</guid>
		<description><![CDATA[<p><a rel="lb" class="IFL hs hs12" href='http://uploads.askapache.com/2008/06/backup-script.png' title="Automated Directory Backups with Cron Shell-Script"></a>This simple unix shell script automatically <strong>creates backups of a specific folder at regular hourly, nightly, weekly, and monthly intervals</strong>. Instead of the usual method for copying directory trees using tar with fifo, pipes, rsync, or NFS methods this script uses <strong>cpio</strong> which is much much faster and has cool options like saving m/a/c times, symlinks, relative paths, and weird file names.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/shellscript/snapshot-backups-shell-script.html"></a><a href="http://www.askapache.com/shellscript/snapshot-backups-shell-script.html"><cite>AskApache.com</cite></a></p><p><a rel="lb" class="IFL hs hs12" href='http://uploads.askapache.com/2008/06/backup-script.png' title="Automated Directory Backups with Cron Shell-Script"></a>This simple unix shell script automatically <strong>creates backups of a specific folder at regular hourly, nightly, weekly, and monthly intervals</strong>.  DreamHost has this feature for their main accounts but for those of us running on the new Private Servers they haven't set it up yet. The old perl script they use on the main accounts does not work with the higher security of the PS kernels and virtual server setup.<br class="C" /></p>

<p class="anote">I just learned that using your dreamhost account for backups like this shell-script is a violation of the Terms of Service, so please don't use this method on DreamHost.</p>



<h2>Why Automated Backups</h2>
<p>I make a lot of mistakes while developing and hacking code for my sites and I've come to rely on having access to these backup versions so I can quickly revert.  Contacting support to get access to them is a waste of everyones time compared to finding a solution, so I did. The DreamHost wiki has a couple of complicated but workable solutions for backups but not for PS users, and nothing this simple.  Just another great feature of DreamHosts <em>debian linux based web hosting</em>.</p>

<h2>My Shell Script</h2>
<p>Instead of the usual method for copying directory trees using tar with fifo, pipes, or DreamHost's rsync and NFS method this script uses <strong>cpio</strong> which is much much faster and has a lot of cool options like saving m/a/c times and symlinks, and also being able to handle weird characters and file names.</p>



<h2>mysnapshot.sh shell script</h2>
<pre>#!/bin/bash
#
# GNU Free Documentation License 1.2
# 06-11-08 - AskApache (www.askapache.com)
#
#
#    .mysnapshot
#    |-- hourly.0    (one hour ago)
#    |-- nightly.0   (one night ago)
#    |-- weekly.0    (one week ago)
#    `-- monthly.0   (one month ago)
#
&nbsp;
### Source and Destination
source=$HOME/sites
dest=$HOME/.mysnapshot/${1:-hourly}.0/`basename $source`
&nbsp;
### Make Nice - lower load
renice 19 -p $$ &amp;&gt;/dev/null
&nbsp;
### Non-Absolute links, check source exists
cd $source || exit 1
&nbsp;
### Hide errs, copy dirtree
find . -depth -print0 2&gt;/dev/null | cpio -0admp $dest &amp;&gt;/dev/null
&nbsp;
cd $OLDPWD
&nbsp;
exit 0</pre>




<h3>mysnapshot.sh crontab entries</h3>
<pre>MAILTO=user@domain.com
# MY SNAPSHOTS
@hourly /home/user/scripts/mysnapshot.sh hourly &amp;&gt;/dev/null
@midnight /home/user/scripts/mysnapshot.sh nightly &amp;&gt;/dev/null
@weekly /home/user/scripts/mysnapshot.sh weekly &amp;&gt;/dev/null
@monthly /home/user/scripts/mysnapshot.sh monthly &amp;&gt;/dev/null</pre>


<hr class="C" />

<p class="anote">Just save the script to your server, <code>chmod u+x</code>, add the crontab entries, and you will have automated backups of any folder you want other than your HOME folder.</p>

<hr class="C" />
<p>Stay tuned, I'm learning some really incredible stuff right now <em>*if you run with open-source*</em> and will be posting more tutorials soon about some really cool stuff.</p><p><a href="http://www.askapache.com/shellscript/snapshot-backups-shell-script.html"></a><a href="http://www.askapache.com/shellscript/snapshot-backups-shell-script.html">Automated Folder Backup Shell-Script</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/shellscript/snapshot-backups-shell-script.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Upgrading to DreamHost Private Servers</title>
		<link>http://www.askapache.com/hosting/dreamhostps-private-server.html</link>
		<comments>http://www.askapache.com/hosting/dreamhostps-private-server.html#comments</comments>
		<pubDate>Sat, 31 May 2008 21:14:27 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Hosting]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1004</guid>
		<description><![CDATA[<p><a class="IFL hs hs01" rel="lb" href="http://uploads.askapache.com/2008/05/logo3.png" title="DreamHost Private Servers"></a>DreamHost PS gives you your own "virtual machine", protecting your CPU and RAM on your physical machine for faster websites.  Here's what I like and dislike about DreamHostPS, and some of the issues and solutions for migrating. <br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/hosting/dreamhostps-private-server.html"></a><a href="http://www.askapache.com/hosting/dreamhostps-private-server.html"><cite>AskApache.com</cite></a></p><p>I've recently relocated to the DreamHost private server setup, not for any reason other than its such a cool offer that I had to take advantage of it to learn more about it.</p>

<h2>DreamHost Private Server</h2>
<p><a class="IFL hs hs01" rel="lb" href="http://uploads.askapache.com/2008/05/logo3.png" title="DreamHost Private Servers"></a>DreamHost PS uses <a href="http://linux-vserver.org/Welcome_to_Linux-VServer.org">Linux-VServer</a> to give you your own "virtual machine", thereby protecting your CPU and RAM from all other users on your physical machine.<br class="C" /></p>


<h2>Linux-VServer</h2>
<p><a class="IFL" rel="lb" href="http://uploads.askapache.com/2008/05/linux-vserver.png" title="Linux-VServer"><img src="http://uploads.askapache.com/2008/05/linux-vserver.png" alt="Linux-VServer" title="linux vserver hosting" /></a><a href="http://linux-vserver.org/">Linux-VServer</a> provides virtualization for <a href="http://www.gnu.org/" title="GNU Operating System - GNU is free software">GNU/Linux</a> systems. This is accomplished by kernel level isolation. It allows to run multiple virtual units at once. Those units are sufficiently isolated to guarantee the required security, but utilize available resources efficiently, as they run on the same kernel.  This particular virtual server model is implemented through a combination of "security contexts", segmented routing, chroot, extended quotas and some other standard tools.<br class="C" /></p>


<h2>What Bothers Me about DreamHostPS</h2>
<ol>
<li>They don't have the skill/desire to let you run only certain sites/usernames on the PS, its all or nothing, which completely sucks!</li>
<li>It's so darn expensive, I spent more after the first 24 hours than I usually do in a MONTH</li>
<li>Although they say you still have the backups of your site, the .snapshot folder is no longer accessible and you have to manually contact support to ask for a backup! Ahh!</li>
<li>The environment on the shell doesn't have as much access to good stuff like using locate</li>
<li>Processes can get out of control and use up to much memory if you have a busy site like me, which results in the sending of 503's to everyone else!</li>
<li>Still uses NFS, the slowest thing in the world and not worth it now that backups aren't even accessible.. it can also cause some hard-to-detect issues with caching setups</li>
</ol>



<h2>What Thrills me about DreamHostPS</h2>
<ol>
<li>I have more access and control over my server, sites, memory, and files via SSH login</li>
<li>I can scale the CPU and Memory whenever I want, and just like the static IP's you only pay for the time you use, making experimenting affordable!</li>
<li>I can reboot the whole server at anytime!  Sometimes useful if a script I'm experimenting with locks the whole server..</li>
<li>Only my shell accounts can access the server!  Security is much stronger!</li>
<li>You can configure other webservers like lighttpd, nginx, and fnord to serve content on sites that are mostly static content!</li>
<li>I've only had it for 3 days, so I'm sure I'll find alot more awesome capabilities</li>
</ol>

<p><a href="http://uploads.askapache.com/2008/05/scale-it.png" rel="lb" title="Scale DreamHost CPU and Memory"><img src="http://uploads.askapache.com/2008/05/scale-it.png" alt="Scale DreamHost CPU and Memory" width="207" height="320" title="scale it hosting" /></a></p>

<h2>Problems migrating to DreamHostPS</h2>
<p>Here are some issues I experienced during migration and the solutions I've used.</p>
<p class="anote">During this process it is important to note how helpful the DreamHost Support Staff were in putting up with my sometimes overly technical and detailed support requests. Thanks John, Brian, and Robert R!</p>


<h3>PHP and HTTPD processes hogging all memory</h3>
<p>Unlike on shared hosting accounts, where DH technical people have set up a very robust system, it appears they are missing the expertise of a past employee or something because this new setup is not as robust.. YET!</p>
<p>For instance I started out my account CPU and memory at the MAX (2300 MB / 2300 Mhz) but my sites were all still taking forever to serve content, simply because instead of on the shared servers where user processes and HTTPD instances are more controlled, this account seems to not have very well-thought out limits on it.  So if 100 people asked for a page on my site, this server loads up 100 HTTPD processes under dhapache user and loads up 100 processes for the custom-compiled php.cgi I am running.  This sounds like a cool thing but in reality it takes up so much of my memory that my bash shell login under SSH runs out of memory and won't even let me do a simple ps, and it just keeps serving 503's to anyone else who requests something on my site.  DH will have to fix this soon or someone will launch a DDOS attack that will cripple them, unless a googlebot does it first!</p>
<p><strong>Solution</strong>
I contacted support and received a very friendly and prompt reply suggesting a bad script and offering to setup a process watcher and killer, which I accepted.  Eventually I located the problem to be an <code>ErrorDocument 500</code> directive in my .htaccess that was pointing to a php file instead of a static .html</p>


<h3>No crontabs or cronjobs</h3>
<p>None of my crontab files were moved to my new server and in fact I was receiving permission denied just to access my crontab.  </p>
<p><strong>Solution</strong>
I contacted support and they installed new crontabs for me and offered to copy my old ones.</p>


<h3>Static IP Changed for site with non-DreamHost DNS</h3>
<p>One site uses DNS from Network Solutions, so when my site was migrated and got a new static IP address, my site went down.  It would have been nice and should be expected that in this situation DreamHost would alert you that the change is going to happen so you can update your DNS without your site going offline.</p>
<p><strong>Solution</strong>
Logged into my Network Solutions account and updated the DNS for my site to point to the new Static IP.</p>


<h3>SSH Hosts, Authorized Keys Broken</h3>
<p>Some of my sites and user accounts use passwordless SSH to make some things work, and all of these were made useless when I moved to my new private server.</p>
<p><strong>Solution</strong></p>
<ol>
<li>Logged into my user accounts with SSH</li>
<li>Deleted the old files in folder .ssh</li>
<li>Created new keys and added them to other accounts</li>
<li>Logged in to new accounts to add to host files</li>
</ol>

<h3>Old Server and Static IP References in Site Files</h3>
<p>I have some pretty technical and complex cgi's, .htaccess files, shell scripts run by cronjobs, php scripts, etc., on some sites and shell accounts, and many of my files contain code to the Static IP and/or dreamhost server, either for access control or for faster connects by connecting straight to an IP instead of having to perform a DNS lookup.  So when both the Static IP's and dreamhost server changed it broke all my files.</p>

<p><strong>Solution</strong>
Basically I knew I had to search all of my files and replace the old IP with the new IP.  I also had to search files relacing the old server with my new server.  To make life simpler, I wrote a simple shell script that I run from my account while logged in using SSH that does this automatically with the added feature of asking me if I would like to make the replacement for each file it finds, which is nice because I don't want to replace this for old log files and misc stuff.</p>

<h4>dreamhostps migration shell script</h4>
<pre>#!/bin/bash
# Version 1.0 by AskApache 5/29/2008
&nbsp;
shopt -s extglob
renice 19 $$
&nbsp;
OLDSERVER=208.113.183.103
NEWSERVER=208.113.134.190
&nbsp;
FIXFILES=$(grep -R -l -i $OLDSERVER $HOME/!(Maildir|logs|backups|source|tmp|doit|php5|php526|ip_abuse) 2&gt;/dev/null)
&nbsp;
for thefile in ${FIXFILES[@]}; do
 if [ -f "$thefile" ]; then
  echo -e "\n\n\n\n"
  echo "___________________________________________________________________"
  echo "Name:  ${thefile}"
  echo "Type:  $(command file -b ${thefile})"
  echo "Size:  $(command du -hs ${thefile}|awk &#039;{ print $1}&#039;)"
  echo "Matching Lines:"
  grep -i --color=auto $OLDSERVER $thefile
  echo -e "___________________________________________________________________\n"
  echo -en "Replace occurances of $OLDSERVER with $NEWSERVER? [y/N] " ; read -n 1 ans
  case "$ans" in
   n|N) echo -e "\nSKIPPING..."; ;;
   y|Y) echo -e "\nREPLACING..."
        cp $thefile $thefile.b1 &amp;&gt;/dev/null
        cat $thefile.b1 | sed "s/${OLDSERVER}/${NEWSERVER}/g" 1&gt;$thefile
        rm $thefile.b1 &amp;&gt;/dev/null
        echo "DONE"; ;;
  esac
 fi
done
exit 0</pre>



<hr class="C" />


<h2>Other Webservers Allowed on DreamHostPS</h2>
Lightweight web servers are Web servers which have been designed to run with very small resource overhead because of hardware, environment, or simply for the challenge of it.

Many of these systems have been created as a mental exercise to determine if a modern webserver could be written to run on limited resources such as those provided in a graphing calculator, an ancient Commodore 64 machine, or in 64 kB (64 KiB) total of memory. Others have been written as commercial endeavors to create webservers with low overhead for embedded systems (network router configuration pages) or low memory environments.

<blockquote cite="http://wiki.dreamhost.com/DreamHost_PS_Troubleshooting">Apache webserver on DH is configured in process-based mode. This means that each process serves one simultaneous connection and uses significant amount of memory for that, thus limiting concurrent user count. You can download and install some lightweight web-servers like (lighttpd) or (nginx). These servers use async i/o and can handle large count of concurrent connections without consuming much RAM, especially when serving static files. But default port 80 is already occupied by apache, and you cannot change that. So, you can use another port for new lightweight server, but visitors will see ugly port number in address and such solution will not work for some corporate firewalls.</blockquote>

<ol>
<li><a href="http://www.lighttpd.net/">lighttpd</a> - small memory footprint compared to other web-servers, effective management of the cpu-load, and advanced feature set
<ul>
<li>FastCGI</li>
<li>SCGI</li>
<li>Auth</li>
<li>Output-Compression</li>
<li>URL-Rewriting</li>
<li><a href="http://trac.lighttpd.net/trac/wiki/Docs">many more</a></li>
</ul>
</li>
<li>nginx - lightweight web server/reverse proxy and e-mail (IMAP/POP3) proxy
<ul>
<li>Handling of static files, index files and auto-indexing</li>
<li>Reverse proxy without caching, load balancing, and fault tolerance</li>
<li>SSL support</li>
<li>FastCGI support</li>
<li>Name- and IP-based virtual servers</li>
<li>FLV streaming</li>
</ul>
</li>
<li><a href="http://www.fefe.de/fnord/">fnord</a> - lightweight webserver written by Felix von Leitner. It aims to be a small, but extremely fast and secure webserver.
<ul>
<li>Small! (13k static Linux-x86 binary without CGI, 18k with CGI)</li>
<li>Fast! Uses mmap (and on Linux, sendfile)</li>
<li>connection keep-alive</li>
<li>el-cheapo virtual domains (similar to thttpd)</li>
<li>IPv6 support (through tcpserver)</li>
<li>CGI (through pipes, not temp files like Apache)</li>
<li>Content-Range (not the full specs, just a-b or a- byte ranges)</li>
<li>transparent content negotiation</li>
<li>Directory index generation</li>
</ul>
</li>
</ol>


<h2>DreamHost PS Troubleshooting</h2>
<ol>
    <li><a href="#Symptoms_of_an_Overloaded_DreamHost_PS">Symptoms of an Overloaded DreamHost PS</a></li>
    <li><a href="#Checking_your_Daily_Monthly_Resource_Usage">Checking your Daily &amp; Monthly Resource Usage</a></li>
    <li><a href="#Changing_your_CPU_Memory_Allocation">Changing your CPU &amp; Memory Allocation</a></li>
    <li><a href="#Rebooting_your_DreamHost_PS">Rebooting your DreamHost PS</a></li>
    <li><a href="#Overloaded_DreamHost_PS">Overloaded DreamHost PS</a></li>
    <li><a href="#Idle_DreamHost_PS">Idle DreamHost PS</a></li>
    <li><a href="#Changing_web_server">Changing web server</a></li>
    <li><a href="#See_also">See also</a></li>
    <li><a href="#External_link">External link</a></li>
</ol>
<a name="Symptoms_of_an_Overloaded_DreamHost_PS" id="Symptoms_of_an_Overloaded_DreamHost_PS"></a>
<h3>Symptoms of an Overloaded DreamHost PS</h3>
<p>If you experience any of the following symptoms, most likely you will need to increase the resource allocation (CPU &amp; memory) to your Private Server:</p>
<ol>
    <li>Out of memory</li>
    <li>Internal server errors ("500" errors)</li>
    <li>Killed scripts</li>
    <li>Inability to log in ("ssh_exchange_identification: Connection closed by remote host")</li>
</ol>
<a name="Checking_your_Daily_Monthly_Resource_Usage" id="Checking_your_Daily_Monthly_Resource_Usage"></a>
<h3>Checking your Daily &amp; Monthly Resource Usage</h3>
<p>Check the daily &amp; monthly usage graph in the Control Panel under <a rel="nofollow" href="https://panel.dreamhost.com/index.cgi?tree=vserver.usage">(PRIVATE SERVERS &gt; CPU/MEMORY)</a> to see if your usage is going beyond your currently guaranteed CPU and memory allocation.</p>
<a name="Changing_your_CPU_Memory_Allocation" id="Changing_your_CPU_Memory_Allocation"></a>
<h3>Changing your CPU &amp; Memory Allocation</h3>
<p>You can increase or decrease your resource allocation by moving the green slider in the Control Panel under <a rel="nofollow" href="https://panel.dreamhost.com/index.cgi?tree=vserver.usage">(PRIVATE SERVERS &gt; CPU/MEMORY)</a>. As you move it the amount of CPU and memory will update automatically, along with the rate you'll be charged for that setting. Once you're happy with the setting click on the <em><strong>"Change {servername}'s CPU / Memory Now!"</strong></em> button to push the change into place. It will take a short period of time for the setting change to be reflected. Typically no reboot is necessary.</p>
<a name="Rebooting_your_DreamHost_PS" id="Rebooting_your_DreamHost_PS"></a>
<h3>Rebooting your DreamHost PS</h3>
<p>If you have a problem you can try rebooting your Private Server yourself in the Control Panel under <a rel="nofollow" href="https://panel.dreamhost.com/index.cgi?tree=vserver.reboot">(PRIVATE SERVERS &gt; REBOOT SERVER)</a>. No need to contact support for that! Isn't that cool?! It may temporarily fix things, but if it doesn't provide a long-term solution, you will need to increase the resources allocated to your Private Server by using the green slider as mentioned in the previous paragraph.</p>
<a name="Overloaded_DreamHost_PS" id="Overloaded_DreamHost_PS"></a>
<h3>Overloaded DreamHost PS</h3>
<p><a rel="nofollow" href="http://wiki.dreamhost.com/Image:DreamHostPS_Club_150.jpg" title="Club 150 (overloaded)"><img src="http://wiki.dreamhost.com/images/6/6f/DreamHostPS_Club_150.jpg" alt="Club 150 (overloaded)" width="907" height="460" title="DreamHostPS Club 150 hosting" /></a></p>
<p>The image above is what your daily usage graph might look if you've got your settings too low. Notice that the actual usage routinely exceeds the guaranteed amount of 150MHz/MB. This is bound to cause problems for your sites. To rectify the situation it's recommended that you increase your resource allocation. Then keep an eye on the graph for several hours, and test our sites to see if you've allocated enough resources for things to run smoothly. It is recommended that you start out by <strong>doubling</strong> your current resource allocation to see if it's enough. Once you've verified that things are running properly you can reduce your resource allocation to the point where your peaks just barely exceed what you have allocated. Of course you'll want to routinely monitor your usage and increase the resource allocation as your needs increase. <em><strong>It's best to over allocate then under allocate! You don't want to find out that you've under allocated by your visitors/customers complaining about your sites not working properly.</strong></em></p>
<p>Note that you're only charged for the period of time that you have the slider in a particular position. So it's safe to experiment. In fact, we recommend it. You can increase or decrease your resource allocation at any time.</p>
<p>You will typically see Apache processes running on your server and appearing to consume all of the memory.  This is generally not the case because Apache processes share a significant amount of memory between one another.  Additionally, we automatically configure Apache to work well within the memory allocation of your PS server.  It is still possible for a busy website to overwhelm a DreamHost PS server, but it is not generally the fault of the apache webserver itself.</p>
<p>Of course you may also want to try to reduce your load on the server as well so you can reduce the resource allocation and save some money.</p>
<a name="Idle_DreamHost_PS" id="Idle_DreamHost_PS"></a>
<h3>Idle DreamHost PS</h3>
<p><a rel="nofollow" href="http://wiki.dreamhost.com/Image:DreamHostPS_Club_15_no_use.jpg" title="Club 150 (idle)"><img src="http://wiki.dreamhost.com/images/6/64/DreamHostPS_Club_15_no_use.jpg" alt="Club 150 (idle)" width="885" height="460" title="DreamHostPS Club 15 no use hosting" /></a></p>
<p>The image above is what your daily usage graph might look if you've got <strong>nothing</strong> running under your DreamHost PS. This just shows the overhead resource usage. You could host a lightly loaded web page with this resource allocation (provided it uses static content), but probably not much more. If you've got your resource allocation set to 150MHz/MB it is recommendended that you monitor the usage very often.</p>

<p class="wikicop"><em>Some Content on this page included from <a href="http://wiki.dreamhost.com/DreamHost_PS_Troubleshooting" rel="nofollow">this article</a> by <a href="http://wiki.dreamhost.com/index.php?title=DreamHost_PS_Troubleshooting&amp;action=history">Author History</a> from the <a href="http://wiki.dreamhost.com/Main_Page" rel="nofollow">DreamHost Wiki</a> and is licensed under the <a href="http://www.askapache.com/gnu-fdl.txt" rel="nofollow">GNU FDL</a>.</em></p><p><a href="http://www.askapache.com/hosting/dreamhostps-private-server.html"></a><a href="http://www.askapache.com/hosting/dreamhostps-private-server.html">Upgrading to DreamHost Private Servers</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/hosting/dreamhostps-private-server.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Search And Replace shell script helpful for Upgrades</title>
		<link>http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html</link>
		<comments>http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html#comments</comments>
		<pubDate>Sat, 19 Apr 2008 05:09:52 +0000</pubDate>
		<dc:creator>AskApache</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://www.askapache.com/?p=1516</guid>
		<description><![CDATA[<p><a class="IFL hs hs14" href="http://uploads.askapache.com/2008/09/search-and-replace.png"></a>Search all files in a directory, replacing all occurances of string with a replacement string.<br class="C" /></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html"></a><a href="http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html"><cite>AskApache.com</cite></a></p><p>This bash shell script searches in a directory you specify for any files containing a string and replaces that string with another string.  It's nice because it asks you if you would like to be prompted for each operation, or to run without user-input.  I use this shell script to change static IP addresses or domains, file locations, database info, etc.</p>

<p class="inote">Note:  I'm going to be updating this soon.. </p>
<p>For now here is a one-liner for you that works very fast and is also low-resource-intensive!</p>
<pre>nice -n19 sh -c &#039;S=askapache &amp;&amp; R=htaccess; find . -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e s%${S}%${R}% FF&#039;</pre>



<h2><a id="screenshot">ScreenShot</a></h2>
<p><a href="http://uploads.askapache.com/2008/09/search-and-replace.png"><img src="http://uploads.askapache.com/2008/09/search-and-replace.png" alt="Search And Replace shell script helpful for Upgrades"  title="search and replace shellscript" /></a></p>





<h2><a id="code">Shell Script Code</a></h2>
<p>You can also download it: <a href='http://uploads.askapache.com/2008/09/find_and_replace1.sh'>FIND AND REPLACE SCRIPT Version 1.4</a></p>
<pre>#!/bin/bash
# Find and Replace Version 2, 2009-04-19
# Version 1.4
# GNU Free Documentation License 1.2
# 2009-04-19 - AskApache (www.askapache.com)
umask 0022
&nbsp;
# set -o xtrace &amp;&amp; set -o verbose #uncomment for verbose debugging
&nbsp;
# nicify to avoid getting killed
renice 19 -p $$ &amp;&gt;/dev/null
&nbsp;
### SHELL OPTIONS
set +o noclobber  # allowed to clobber files
set +o noglob     # globbing on
set -e            # abort on first error
shopt -s extglob  # more globbing
&nbsp;
PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/libexec:/usr/sbin
VER="1.4"
COLORON=yes
&nbsp;
SEARCH=${1:-QQQQQQQQ};
REPLACE=${2:-AskApache};
SDIR=${3:-/tmp};
&nbsp;
# script_title
#==-==-==-==-==-==-==-==-==-==-==#
function script_title(){
  clear;echo -e "${C[1]} __________________________________________________________________________ "
  echo -e "| ${C[2]}             ___       __    ___                 __             ${C[1]}         |"
  echo -e "| ${C[2]}            / _ | ___ / /__ / _ | ___  ___ _____/ /  ___        ${C[1]}         |"
  echo -e "| ${C[2]}           / __ |(_-&lt;/  &#039;_// __ |/ _ \/ _ \`/ __/ _ \/ -_)       ${C[1]}         |"
  echo -e "| ${C[3]}          /_/ |_/___/_/\_\/_/ |_/ .__/\_,_/\__/_//_/\__/        ${C[1]}         |"
  echo -e "| ${C[3]}                               /_/                              ${C[1]}         |"
  echo -e "|                                                                          |"
  echo -e "|                 ${C[4]} FIND AND REPLACE SCRIPT Version $VER ${C[1]}                    |"
  echo -e "${C[1]} __________________________________________________________________________ ${C[0]} \n\n"
}
&nbsp;
# pm
#==-==-==-==-==-==-==-==-==-==-==#
function pm()
{ case ${2:-d} in d) echo -en "\n\n${C[2]}&gt;&gt;&gt; ${C[4]}$1 ${C[0]}\n\n"; ;; i*) echo -e "\n${C[6]}=&gt; ${C[4]}${1} ${C[0]}\n"; ;; esac; return 0; }
&nbsp;
# ok_continue
#==-==-==-==-==-==-==-==-==-==-==#
function ok_continue()
{ local ans; echo -en "${C[4]} \n [ Press any key to continue ] ${C[0]} \n"; read -n 1 ans; return 0; }
&nbsp;
# yes_no
#==-==-==-==-==-==-==-==-==-==-==#
function yes_no()
{ local a YN=65; echo -en "${C[2]}&gt;&gt;&gt; ${C[4]}${1:-Q} [y/n] ${C[0]}"; read -n 1 a; case $a in [yY]) YN=0; ;; esac; return $YN; }
&nbsp;
# p_done
#==-==-==-==-==-==-==-==-==-==-==#
function p_done()
{ echo -e "\n${C[8]} DONE ${C[0]} \n"; return 0; }
&nbsp;
# get_settings
#==-==-==-==-==-==-==-==-==-==-==#
function get_settings(){
  local a cha
  script_title
  while true; do
  for a in "SDIR" "SEARCH" "REPLACE"; do
   cha=g; echo -en "\n ${C[4]}(Enter for Default: ${!a} )${C[0]}\n ${a}${C[6]}=&gt; ${C[0]} ";
   read -e cha; echo; [[ ${#cha} -gt 2 ]] &amp;&amp; eval "$a"=$cha;
  done
  yes_no "ARE THESE SETTINGS CORRECT" &amp;&amp; echo -e "\n\n" &amp;&amp; break
  done
}
&nbsp;
# search_and_replace
#==-==-==-==-==-==-==-==-==-==-==#
function search_and_replace(){
  for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do
  thefile=${FOUNDFILES[$i]}
&nbsp;
  if [[ $1 -eq 0 ]]; then
   echo -e "\n\n\n___________________________________________________________________\n"
   echo -e "${C[4]}FILE:  ${C[3]}${thefile} ${C[2]}($(command du -hs ${thefile}|awk &#039;{ print $1}&#039;))"
   echo -e "${C[4]}Type:  ${C[6]}$(command file -b ${thefile})\n${C[7]}Matching Lines:${C[0]}"
   command grep --color=always $SEARCH $thefile;echo
&nbsp;
   while true; do
    yes_no "Replace occurances of ${SEARCH} with ${REPLACE}?" &amp;&amp; echo -e "\nREPLACING...\n" &amp;&amp; sed -i -e "s%${SEARCH}%${REPLACE}%g" $thefile &amp;&amp; p_done &amp;&amp; break
    echo -e "\nSKIPPING..\n" &amp;&amp; p_done &amp;&amp; break
   done
&nbsp;
   else
    sed -i -e "s%${SEARCH}%${REPLACE}%g" $thefile
   fi
&nbsp;
  done
  return 0;
}
&nbsp;
############################################################################################################
### MAIN CODE
############################################################################################################
&nbsp;
if [[ $SEARCH == "QQQQQQQQ" ]]; then
  E=&#039;\033[&#039;;
  [[ -z "$TERM" ]]&amp;&amp;[[ -z "$1" ]]||declare -a C=(&#039;${E}0m&#039; &#039;${E}1\;30m&#039; &#039;${E}0\;32m&#039; &#039;${E}1\;32m&#039; &#039;${E}1\;37m&#039; &#039;${E}1\;36m&#039; &#039;${E}1\;35m&#039; &#039;${E}1\;37m&#039; &#039;${E}30\;42m&#039; &#039;${E}1\;36m&#039; )
  script_title            # DISPLAY SCRIPT TITLE
&nbsp;
  get_settings            # GET SCRIPT SETTINGS
  cd $SDIR
  declare -a FOUNDFILES=( `command grep -R -l $SEARCH . 2&gt;/dev/null` );
  declare -a FOUNDMATCHES=(  );
  for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do F=${FOUNDFILES[$i]}; FOUNDMATCHES[$i]=$(command grep -c . $F); done
  pm "FOUND ${SEARCH} IN ${#FOUNDFILES[@]} FILES" i
  for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do echo -e "${C[4]}${FOUNDFILES[$i]} ${C[6]}=&gt;${C[4]} ${FOUNDMATCHES[$i]} matches"; done
  echo -e "\n"
&nbsp;
 while true; do
  yes_no "Replace all occurances of ${SEARCH} with ${REPLACE} without prompting?" &amp;&amp; search_and_replace 1 &amp;&amp; p_done &amp;&amp; break
  search_and_replace 0 &amp;&amp; p_done &amp;&amp; break
 done
&nbsp;
else
 cd $SDIR
 command grep -H -R -l $SEARCH . 2&gt;/dev/null | xargs -iFFF sed -i -e "s%${SEARCH}%${REPLACE}%g" FFF
fi
&nbsp;
exit 0</pre>

<p><a class="IFL" href="http://uploads.askapache.com/2008/09/search-and-replace.png"><img src="http://uploads.askapache.com/2008/09/search-and-replace-187x250.png" alt="Search And Replace shell script helpful for Upgrades" width="187" height="250" title="search and replace 187x250 shellscript" /></a><br class="C" /></p>




<h2>Simplified for crontab</h2>
<p>*Mike*<br />To run from a crontab you wouldn't want a full-fledged script like this, I would just create a small bash script like so:</p>
<pre>#!/bin/sh
PATH=/usr/bin:$PATH:/usr/local/bin
command grep -H -R -l $1 $3 2&gt;/dev/null | xargs -iFFF sed -i -e "s%${1}%${$2}%g" FFF</pre>

<p>This is like 100x faster, to call from crontab every 35 minutes and search the directory /home/user/tmp for 'search' and replace it with 'replace' every 35 minutes. </p>
<pre>*/35 * * * * /bin/sh /home/user/search-replace.sh &#039;search&#039; &#039;replace&#039; &#039;/home/user/tmp&#039; 1&gt;/dev/null</pre><p><a href="http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html"></a><a href="http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html">Search And Replace shell script helpful for Upgrades</a> originally appeared on <cite>AskApache.com</cite> </p>]]></content:encoded>
			<wfw:commentRss>http://www.askapache.com/shellscript/search-and-replace-shell-script-helpful-for-upgrades.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

