<?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>gHacks technology news &#187; automated tasks</title>
	<atom:link href="http://www.ghacks.net/tag/automated-tasks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghacks.net</link>
	<description>A technology blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description>
	<lastBuildDate>Tue, 24 Nov 2009 03:24:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Cron to Automate Linux Tasks</title>
		<link>http://www.ghacks.net/2009/01/11/using-cron-to-automate-linux-tasks/</link>
		<comments>http://www.ghacks.net/2009/01/11/using-cron-to-automate-linux-tasks/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 21:32:12 +0000</pubDate>
		<dc:creator>Jack Wallen</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Tutorials Advanced]]></category>
		<category><![CDATA[Tutorials Basic]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[automated tasks]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[cron job]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[script automation]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://www.ghacks.net/?p=9823</guid>
		<description><![CDATA[Linux is one of the most flexible and useful operating systems available. One of the tools that places Linux at the top of the flexibility and usability scale is cron. The cron system allows you to automate tasks without the aid of third party software. But because few know how to use cron, it seems [...]]]></description>
			<content:encoded><![CDATA[<p>Linux is one of the most flexible and useful operating systems available. One of the tools that places Linux at the top of the flexibility and usability scale is cron. The cron system allows you to automate tasks without the aid of third party software. But because few know how to use cron, it seems too complex to bother with. Not so. The cron system is actually quite easy to use. Let&#8217;s take a look at cron.</p>
<p>The cron system works by reading various crontab files either found in a users&#8217; directory or within the <strong>/etc </strong>directory. These cron files can be edited with either the <em>crontab</em> command or by editing the various files within <strong>/etc</strong>. Only the root user can edit the <strong>/etc</strong> files because those files (found in the subdirectories <strong>/etc/cron.d, /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, /etc/cron.monthly</strong>) are used for system services.  Instead of focusing on system services, I will illustrate how to automate the execution of a user-created bash script.</p>
<p><span id="more-9823"></span>This imaginary bash script will be called <em>myscript.sh</em>. This script will be housed in <strong>/home/jlwallen/ </strong>and will have executable permission (by way of <em>chmod u+x myscript.sh</em>).  What we want to do is automate the execution of this script so that it executes once per day at the same time every day (we&#8217;ll say 7am).</p>
<p>Before we get into editing with crontab we first need to chat about how cron views time.The cron system looks at time like this:</p>
<ul>
<li><em>Minute(0-59)</em></li>
<li><em>Hour (0-23)</em></li>
<li><em>Day of the month (1-31)</em></li>
<li><em>Month (1-12)</em></li>
<li><em>Day of the week (0-6 with Sunday being 0)</em></li>
</ul>
<p>The structure of cron time looks like this:</p>
<p>Minute Hour Day of month Month Day of the week</p>
<p>The trick for cron is that when you do not have an entry for a section you add an &#8220;*&#8221;. So the time entry for a cron job that should run at 7am every day of the week would look like:</p>
<p><em>0 7 * * * </em></p>
<p>That is not the complete cron entry, just the time portion. Now, on to editing with crontab.</p>
<p><strong>Crontab</strong></p>
<p>Users edit their cron entries with the <em>crontab</em> command. The crontab command has a few switches:</p>
<ul>
<li>e &#8211; Edit your crontab</li>
<li>l &#8211; List your crontab</li>
<li>r &#8211; Delete all crontab entries</li>
</ul>
<p>To add a new crontab entry enter the command <em>crontab -e</em> which will place you in the vi text editor with your user crontab file opened. If you&#8217;re not familiar with vi I&#8217;ll give you the crash course as we edit the crontab.</p>
<p>First click the &#8220;i&#8221; key to go into the insert mode. You can now add text. Enter the cron entry to run the myscript.sh every day at 7am. This entry will look like:</p>
<p>* 7 * * * ~/myscript.sh</p>
<p>Now hit the Esc key to get out of the insert mode. Once out of insert mode you need to write the file and quite crontab. To do this hit the &#8220;:&#8221; key followed by &#8220;wq&#8221; (for write and quite). Finish the job by hitting the Enter key.</p>
<p>You should see &#8220;crontab: installing new crontab&#8221; at your bash prompt.</p>
<p>Congratulations, you&#8217;ve just created your first cron job!</p>

	Tags: <a href="http://www.ghacks.net/tag/automated-tasks/" title="automated tasks" rel="tag">automated tasks</a>, <a href="http://www.ghacks.net/tag/cron/" title="cron" rel="tag">cron</a>, <a href="http://www.ghacks.net/tag/cron-job/" title="cron job" rel="tag">cron job</a>, <a href="http://www.ghacks.net/tag/crontab/" title="crontab" rel="tag">crontab</a>, <a href="http://www.ghacks.net/tag/linux/" title="Linux" rel="tag">Linux</a>, <a href="http://www.ghacks.net/tag/script-automation/" title="script automation" rel="tag">script automation</a>, <a href="http://www.ghacks.net/tag/vi/" title="vi" rel="tag">vi</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.ghacks.net/2009/01/12/backup-mysql-databases-in-linux-regularly/" title="Backup MySQL Databases In Linux Regularly (January 12, 2009)">Backup MySQL Databases In Linux Regularly</a> (11)</li>
	<li><a href="http://www.ghacks.net/2009/01/22/back-up-your-apache-web-directory-and-database-with-this-simple-script/" title="Back up your Apache web directory and database with this simple script (January 22, 2009)">Back up your Apache web directory and database with this simple script</a> (0)</li>
	<li><a href="http://www.ghacks.net/2008/02/07/yoggie-pico-personal-mobile-security-computer/" title="Yoggie PICO Personal Mobile Security Computer (February 7, 2008)">Yoggie PICO Personal Mobile Security Computer</a> (3)</li>
	<li><a href="http://www.ghacks.net/2009/10/30/with-ubuntu-9-10-arrives-wubi-9-10/" title="With Ubuntu 9.10 Arrives Wubi 9.10 (October 30, 2009)">With Ubuntu 9.10 Arrives Wubi 9.10</a> (2)</li>
	<li><a href="http://www.ghacks.net/2006/12/07/widgets-for-linux-superkaramba/" title="Widgets for Linux: SuperKaramba (December 7, 2006)">Widgets for Linux: SuperKaramba</a> (6)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ghacks.net/2009/01/11/using-cron-to-automate-linux-tasks/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
