VDS Sphera Knowledge Base

Main / Browse Categories / Other Software / Cron / How do I use Cron?

Print article Print Article    Email article Email Article

How do I use Cron?
Cron allows you to set up programs to run at regular intervals as you need them to, at any time of day or night. You start by logging into your VDS via SSH and changing directories:
cd /var/spool/cron/
You will see a file that is named the same as your VDS username. You can edit that file using VI:
vi filename
If you have never edited the file, it most likely has one setting:
22 0 * * * /bin/logrotate /etc/logrotate.conf 2>&1 > /dev/null
A crontab entry is in the basic format:
time action
To set a time, you use the format:
MI HR DM MO DW
MI = A minute from 0 through 59 
HR = An hour in 24 hour time, from 0 to 23 
DM = A day of the month, from 1 through 31 (or 28,29,30) 
MO = A month from 1 to 12. 
DW = A day of the week, starting with Sunday, from 0 to 6. 
You can also use an asterisk (*) for "Everything," and both commas and dashes to indicate sequences and ranges. So, this gives us:
MI HR DM MO DW	action
The action is whatever you want cron to run when this time comes. Typically, the output will be e-mailed to you unless you specify something else to happen.

An example of what you might put in the time portion of a crontab file if you wanted to remind yourself two hours early to pick the kids up from late school activities every Monday, Wednesday, and Friday afternoon at 5 pm is:

00 15 * * 1,3,5
Remember, you want to send your reminder at 3 pm or 15:00 hours. Since we're not restricting ourselves to particular days of the month, there's an asterisk in that slot. We're also not restricting what months this happens in. Then, we want the first, third, and fifth day of the week.

Now the action and for a reminder the user "john" can use mail to send an e- mail to himself. John would have to be a POP user in your VDS. If they are not an actual user, you would use their complete email address. For example, john@domain.com.

echo "Do not forget" | mail -s "Pick up kids at 5 pm" john
echo "Do not forget" | mail -s "Pick up kids at 5 pm" john@domain.com
This format places the text "Do not forget" in the message body and "Pick up kids at 5 pm" in the subject line.

So, putting it all together, you'd have:

00 15 * * 1,3,5	echo "Do not forget" | mail -s "Pick up kids at 5 pm" john
Now save and quit from your crontab file and run the command:
crontab

User Comments
How do I use Cron?  
There are no user comments for this topic.
 

Attachments
No attachments.

Related Articles
 How do I use Cron inside my VDS?
 Can I edit my cron file using Pico?

Why Choose Our Hosting?

  • Unmatched Customer Support!
  • Virtual Private Server technology provides a rich, dedicated-like environment for every account.
  • We are known for our superb quality, performance and flexible hosting environment. We manage the server, software, and network you do the rest.
Continue reading »

Featured Hosting Plan


Learn more about our VPS Extreme

What is Included?

  • PHP 5/MySQL 5
  • Parallels® Plesk Panel 10
  • Unique IP Address
  • Daily VPS Backups (Off Server)
  • SAS Hardware Raid diskspace
  • CentOS 5 x86_64 bit
  • Multi-homed Tier 1 Bandwidth
  • Full Upgrade capabilities
Continue reading »