<?xml version="1.0"?>
<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/"
	>
   <channel>
      <pubDate>Sat, 11 Oct 2008 06:45:05 GMT</pubDate>
      <lastBuildDate>Sat, 11 Oct 2008 06:45:05 GMT</lastBuildDate>
      <language>en</language>
      <docs>http://www.rssboard.org/rss-specification</docs>
      <title>CrunchBang ~ crontab</title>
      <link>http://crunchbang.org/tags/crontab/</link>
      <description>Code, Design &amp; GNU/Linux</description>

<item>
    <title>Howto Set-up a Crontab File</title>
    <link>http://crunchbang.org/archives/2007/10/26/howto-setup-a-crontab-file/</link>
    <pubDate>Fri, 26 Oct 2007 17:18:28 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/10/26/howto-setup-a-crontab-file/</guid>
    <description><![CDATA[
    <p>Yesterday I mainly worked on some PHP backup scripts [<em>not very exciting, but necessary.</em>] The scripts are run at regular intervals and perform various data backups across numerous domains. To accomplish this I set-up some cron jobs that will automatically execute the scripts at set intervals.</p>

<p>This is not the first time that I&#39;ve had to set-up cron jobs [<em>I&#39;ve got several that date back a few years.</em>] I think that having the ability to set-up scheduled tasks is really quite important. Therefore I thought I&#39;d blog about it for future reference.</p>

<h3>What is cron?</h3>

<p>Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.</p>

<h3>Creating a crontab file</h3>

<p>You can create a crontab file by entering the following terminal command:</p>

<pre><code>crontab -e
</code></pre>

<p>Entering the above command will open a terminal editor [<em>Ubuntu uses Nano by default</em>] with a new blank crontab file [<em>or it will open an existing crontab if you already have one.</em>] You can now enter the commands to be executed [<em>see syntax below</em>] before saving the file and exiting the editor. As long as your entries were entered correctly your commands should now be executed at the times/dates you specified. You can see a list of active crontab entries by entering the following terminal command:</p>

<pre><code>crontab -l
</code></pre>

<h3>Crontab syntax</h3>

<p>A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:</p>

<pre><code>*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
</code></pre>

<h3>Crontab examples</h3>

<p>Writing a crontab file can be a somewhat confusing for first time users [<em>and the above table probably doesn&#39;t help much!</em>] Therefore I&#39;ve listed below some crontab examples:</p>

<pre><code>* * * * * &lt;command&gt; #Runs every minute
30 * * * * &lt;command&gt; #Runs at 30 minutes past the hour
45 6 * * * &lt;command&gt; #Runs at 6:45 am every day
45 18 * * * &lt;command&gt; #Runs at 6:45 pm every day
00 1 * * 0 &lt;command&gt; #Runs at 1:00 am every Sunday
00 1 * * 7 &lt;command&gt; #Runs at 1:00 am every Sunday
00 1 * * Sun &lt;command&gt; #Runs at 1:00 am every Sunday
30 8 1 * * &lt;command&gt; #Runs at 8:30 am on the first day of every month
00 0-23/2 02 07 * &lt;command&gt; #Runs every other hour on the 2nd of July
</code></pre>

<p>As well as the above there are also special strings that can be used:</p>

<pre><code>@reboot &lt;command&gt; #Runs at boot
@yearly &lt;command&gt; #Runs once a year [0 0 1 1 *]
@annually &lt;command&gt; #Runs once a year [0 0 1 1 *]
@monthly &lt;command&gt; #Runs once a month [0 0 1 * *]
@weekly &lt;command&gt; #Runs once a week [0 0 * * 0]
@daily &lt;command&gt; #Runs once a day [0 0 * * *]
@midnight &lt;command&gt; #Runs once a day [0 0 * * *]
@hourly &lt;command&gt; #Runs once an hour [0 * * * *]
</code></pre>

<h3>Multiple commands</h3>

<p>A double-ampersand &#34;<code>&amp;&amp;</code>&#34; can be used to run multiple commands consecutively. The following example would run <code>command_01</code> and then <code>command_02</code> once a day:</p>

<pre><code>@daily &lt;command_01&gt; &amp;&amp; &lt;command_02&gt;
</code></pre>

<h3>Disabling email notifications</h3>

<p>By default a cron job will send an email to the user account executing the cronjob. If this is not needed put the following command at the end of the cron job line:</p>

<pre><code>&gt;/dev/null 2&gt;&amp;1
</code></pre>

<h3>Specifying a crontab file to use</h3>

<p>As mentioned at the top of this post, you can create a new crontab file with the &#34;<code>crontab -e</code>&#34; command. However, you may already have a crontab file, if you do you can set it to be used with the following command:</p>

<pre><code>crontab -u &lt;username&gt; &lt;crontab file&gt;
</code></pre>

<p>Therefore the following command&#8230;</p>

<pre><code>crontab -u tux ~/crontab
</code></pre>

<p>&#8230;would set Tux&#39;s crontab file to that of the file named &#34;crontab&#34; residing in Tux&#39;s home directory.</p>

<h3>Removing a crontab file</h3>

<p>To remove your crontab file simply enter the following terminal command:</p>

<pre><code>crontab -r
</code></pre>

<h3>Further information</h3>

<p>Refer to the man page for further information about crontab. Enter the terminal command:</p>

<pre><code>man crontab
</code></pre>

<h3>External links</h3>

<p>Some external links for your browsing pleasure:</p>

<ul>
<li><a href="http://en.wikipedia.org/wiki/Cron " title="Cron article on Wikipedia">Cron article on Wikipedia</a></li>
<li><a href="https://help.ubuntu.com/community/CronHowto " title="CronHowto">Ubuntu Documentation - CronHowto</a></li>
<li><a href="http://www.debianhelp.co.uk/schedulejobs.htm " title="Scheduling Backup Jobs using at and crontab in debian">Debian Help - Scheduling Backup Jobs using at and crontab in debian</a></li>
<li><a href="http://www.mattcutts.com/blog/how-to-fetch-a-url-with-curl-or-wget-silently/ " title="How to fetch a url with curl or wget silently">Matt Cutts - How to fetch a url with curl or wget silently</a></li>
<li><a href="http://www.sitepoint.com/article/introducing-cron " title="Getting PHP and Cron to Work Together">Sitepoint - Getting PHP and Cron to Work Together</a></li>
</ul>

<p>I think that pretty much covers the subject of cron jobs and crontab. Please feel free to comment if I&#39;ve missed anything/made any boobs.</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/crontab/" title="Browse all posts tagged with &#8220;crontab&#8221;">crontab</a>, <a href="http://crunchbang.org/tags/linux/" title="Browse all posts tagged with &#8220;linux&#8221;">linux</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/ubuntu/" title="Browse all posts tagged with &#8220;ubuntu&#8221;">ubuntu</a></p>
    ]]></description>
</item>

 </channel>
</rss>