Cron, crontab example reference and how to run things periodically in GNU/Linux and Unix

Cron is a job scheduler found in most Unix-like operating systems. “Chronos”, which is the Greek word for “time”, is where the name cron comes from. Cron makes it possible to schedule jobs, which can be commands, a series of commands, or scripts, that you want to run periodically. Common uses are backups, notifications, periodic checks on availability of services, networks, machines, or other things, and administration and maintenance tasks like rotating of logs, to mention a few. It is very general-purpose though, (like Unix tools is and should be) and can be used for whatever you can think of that needs to run periodically.
Image showing the Big Ben clock.

Jobs that should be run by cron are specified in crontab (cron table) files. A crontab is a configuration file that specifies commands (shell commands, including programs, scripts, or pipelines thereof) to be run, and when they should be executed.

Each line in a crontab file represents a “job”, and is composed of a CRON expression (specifying the time) followed by the command or commands to be executed. There are two different crontab files, the ones installed by system software, and editable only by root (but in most cases best left alone), and crontab files written by and belonging to users (including root). The authorization control to cron, who will be allowed to have a crontab or not, are done with the files /etc/cron.allow and /etc/cron.deny.

The manpages of cron and the crontab format are nice. “crontab” is both the name of the program installing, listing and editing user’s crontabs, and the file format manpage, so do read them:

man cron
man 1 crontab
man 5 crontab

The page in section 5, about the format is most interesting. However I prefer to keep a short reference in my user’s crontab file itself for quick reference, feel free to grab it:

# mycrontab.txt
# beardy
# My example crontab, kept in ~/

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Send the mail to another address than the local user
##MAILTO=foo@foo.bar

##############################################################################
#
# The following example should run "true" every day of every month, at half
# past eight, every day of the week.
#
# Example:
#
# 30 8  * * *   true
# |  |  | | |    |
# |  |  | | |    +--- command, or script, to run
# |  |  | | +-------- day of week, 0-7 or mon, Tue, FRI
# |  |  | +---------- month, 1-12, or names, jan, Mar, AUG (case doesn't matter)
# |  |  +------------ date, 1-31
# |  +--------------- hour, 0-23
# +------------------ minute, 0-59
#
# see "man 5 crontab"
#
#     Instead of the first five fields, one of eight special strings may
#     appear:
#
#           string          meaning
#           ------          -------
#           @reboot         Run once, at startup.
#           @yearly         Run once a year, "0 0 1 1 *".
#           @annually       (same as @yearly)
#           @monthly        Run once a month, "0 0 1 * *".
#           @weekly         Run once a week, "0 0 * * 0".
#           @daily          Run once a day, "0 0 * * *".
#           @midnight       (same as @daily)
#           @hourly         Run once an hour, "0 * * * *".
#
##############################################################################

# Run beep every Christmas Eve, at 14:40 (20 min before Kalle Anka)
#40 14  24 12 * beep -f 1000 -r 5 -l 150 -D 250
# next job goes after this line
40 14   24 12 * echo "God Jul!"

To install a new crontab, the crontab(1) program is used:

crontab mycrontab.txt

To later list it, and edit it (with the editor configured with the $EDITOR environment variable usually):

crontab -l
crontab -e

Read more about cron and crontab:

cron(8)
crontab(1)
crontab(5)
Time – An important Subject with any OS

Fixing missing menu entries of Opera and Skype in Debian GNU/Linux

Unfortunately the third-party packages of Opera and Skype do not contain menu entries for the Debian menu system. Not very surprisingly one might think, but I just think it’s sloppy. If you, like me, like the Debian menu system and want Opera and Skype available in it, do the following to solve that, until the packagers have included the menu entry files themselves:

(Update: I noticed that Opera has apparently had a menu entry in its package before, but has now removed it. It’s explained in this post, but I still think that was a bad choice.)
Continue reading “Fixing missing menu entries of Opera and Skype in Debian GNU/Linux”

A revised opinion on apache2-mpm-prefork and apache2-mpm-worker, and php5 and php5-cgi on Debian, regarding performance

It’s now over a year since I wrote about “Exchanging apache2-mpm-prefork for apache2-mpm-worker, and using php5-cgi on Debian to improve performance“. Since then I have re-evaluated my opinion about running apache2-mpm-worker and php5-cgi on a low memory machine, specifically a virtual machine running under Xen, running Debian.
Continue reading “A revised opinion on apache2-mpm-prefork and apache2-mpm-worker, and php5 and php5-cgi on Debian, regarding performance”

How to set up a SIP trunk in the Asterisk PBX

In my previous article we configured Asterisk with some SIP-devices, and created a basic dialplan so that they could dial eachother. We also created two additional extensions for test purposes. This time I will show you how to configure a SIP trunk, and add extensions in the dialplan so that the telephones can dial out through the trunk. And if you also have a telephone number (DID) associated with the trunk, for others to be able to dial your phones, through your Asterisk PBX.

What is a SIP trunk?

A SIP trunk is often defined using many buzz- and marketing words throughout the web, but, what it basically is, is a two-way connection to a VOIP-provider, that routes the calls you send to it, out on the PSTN for you, and charges you for the calls you make. If you also have a DID (Direct Inward Dialing) number at the provider, calls made to you are forwarded to your Asterisk PBX, then you switch the calls as you see fit. Through a trunk, many calls can be sent, the limit is only your bandwidth and computer resources at the machine where your Asterisk runs, unless your VOIP-provider, or you for that matter, limit the number of calls in some way (by configuring the PBX at either end of the trunk), that are allowed to go through it. Continue reading “How to set up a SIP trunk in the Asterisk PBX”

An introduction to Asterisk, The Open Source Telephony Project

Asterisk is software that turns an ordinary computer into a voice communications server. Asterisk is the world’s most powerful and popular telephony development tool-kit. It is used by small businesses, large businesses, call centers, carriers and governments worldwide. Asterisk is open source and is available free to all under the terms of the GPL.

That is what the Asterisk website describes Asterisk as. I thought I should write a little about it, since it is quite a high first step, to start using and experimenting with it the first time, atleast I found it so, when I first did. Like with many things, there are good, and bad, documentation, and sources thereof. Sometimes both kinds are found in the same place, and it is up to you to judge on the quality. This is quite frustrating when you are looking for answers and don’t already know, or are able to judge, which is correct, and which is not.

Get the book

The first thing to start reading is Asterisk: The Future of Telephony, 2nd Edition, published by O’Reilly Media, Inc. and written by Jim van Meggelen, Jared Smith, and Leif Madsen. The book is  released under a Creative Commons license, it is available for free in PDF-, and HTML-form here. I suggest you buy the book in paper form though, as it is most often nicer to read a book away from the screen. But the PDF version is great as a reference when moving on with the configuration/experimentation.

I will now describe, in a how-to manner, to get you started, how to install Asterisk in Debian GNU/Linux, connect two SIP devices (telephones), and create a minimal dialplan so that they can call each other. I do not use any web-frontend or GUI, just the configuration files. You will find that it gives the best control and understanding of how things work, if you just take it one step at a time, and learn what you are doing.

What we need is:

  • A machine running Debian GNU/Linux
  • Asterisk itself (Asterisk is packaged in Debian.)
  • Two SIP telephones, “softphones”, or “hardware” telephones using SIP

I presume that you have basic knowledge of package installation and standard Unix tools, and know how to use a text editor of your choice. Allright, let’s begin.

Continue reading “An introduction to Asterisk, The Open Source Telephony Project”

Installing Debian Lenny on an Asus EeePC 900, part two

I have had my EeePC for a while now, and I have set it up how I want it, for the most part. When I wrote the first part, about installing Debian on an Asus EeePC 900, I wasn’t very detailed about what I had, or were going to do, to make it run as I liked. Hopefully that will be remedied now.

I have done a couple of things mentioned in the DebianEeePC wiki http://wiki.debian.org/DebianEeePC/ to reduce the disk writes to the SSD disk a bit.

Continue reading “Installing Debian Lenny on an Asus EeePC 900, part two”

Installing Debian Lenny on an Asus EeePC 900, part one

On Friday I got an Asus EeePC 900 that I had ordered a few days earlier. Now after having looked at the Xandros system that is installed on them when they are sold, it’s time to install Debian on it and become happy.

I’m following the instructions on http://wiki.debian.org/DebianEeePC/HowTo/Install and will install by booting from a USB-stick with the installer on it.

Continue reading “Installing Debian Lenny on an Asus EeePC 900, part one”