CRON+SSMTP
Home Up thttpd Web Server Cherokee web CRON+SSMTP Samba Velleman K8055 Perl Module GD in Perl Native

 

Config ssmtp

CRON and SSMTP

I'm lumping these two programs together:

cron is a scheduler. It accepts instructions from one or more job files. Each instruction consists of a date and time and a command to be run at that date and time. If the command returns text then this can be sent as a report via email provided the system has a mail service installed.

ssmtp is a cut-down mail sender. It simply forwards all messages to another server capable of delivering them. Typically that would be your ISP's mail server.

Installing cron:

root@zebidee:~# ipkg install cron
Installing cron (3.0pl1-r7) to root...
Downloading http://ipkg.nslu2-linux.org/feeds/slugos-bag/cross/3.10-beta/cron_3.0pl1-r7_armeb.ipk
Configuring cron
Adding system startup for /etc/init.d/cron ...
/etc/rc2.d/S65cron -> ../init.d/cron
/etc/rc3.d/S65cron -> ../init.d/cron
/etc/rc4.d/S65cron -> ../init.d/cron
/etc/rc5.d/S65cron -> ../init.d/cron
/etc/rc0.d/K65cron -> ../init.d/cron
/etc/rc1.d/K65cron -> ../init.d/cron
/etc/rc6.d/K65cron -> ../init.d/cron
System startup links for /etc/init.d/cron already exist.
Stopping Vixie-cron.
Starting Vixie-cron.
root@zebidee:~# 

Installing ssmtp:

root@zebidee:~# ipkg install ssmtp
Installing ssmtp (2.61-r6) to root...
Downloading http://ipkg.nslu2-linux.org/feeds/slugos-bag/cross/3.10-beta/ssmtp_2.61-r6_armeb.ipk
Installing libssl0.9.7 (0.9.7g-r1) to root...
Downloading http://ipkg.nslu2-linux.org/feeds/slugos-bag/cross/3.10-beta/libssl0.9.7_0.9.7g-r1_armeb.ipk
Configuring libssl0.9.7
Configuring ssmtp
update-alternatives: Linking //usr/sbin/sendmail to /usr/bin/ssmtp
root@zebidee:~# 

In both cases the package installed itself fully. The cron package included the startup script and a start command so it will be running now.

ssmtp needs a bit of configuration

If you really can't figure out what's going on it's possible to install an evaluation copy of a mailserver on windows and then set the windows PC as the mail hub. By setting the domain to "*" the server can be set to accept anything and that way you can diagnose a bad configuration.

Using cron

cron looks for scheduled jobs in several places:

  1. in a text file /etc/crontab
  2. in any text files in the directory /etc/cron/
  3. in a text file stored by the crontab command, located in /var/cron/tabs and named after a username

I'll concentrate on 3 for now. 

The crontab command:

crontab -e

Runs a text editor, allowing you to edit your crontab file.

crontab -l

Lists the contents of your crontab file.

crontab -r

removes your crontab file.

crontab <filename>

replaces your crontab file with the contents of the named file.

crontab -

replaces your crontab file with input from stdin (best used with "pipes").

other options: -u <username> 

edits the crontab file belonging to another user. This option probably requires you to be "root".

The basic crontab file format

Each line consists of either:

  1. a comment line beginning with a #.
  2. an environment variable such as "MAILTO"
  3. a time and date followed by a command to be executed at that time and date
  4. a special symbol line beginning with an @ sign 

Comments:

You may want to paste the following at the beginning of your crontab for reference:

# (Use to post in the top of your crontab)
# ----------------- minute (0 - 59)
# |  -------------- hour (0 - 23)
# |  |  ----------- day of month (1 - 31)
# |  |  |  -------- month (1 - 12)
# |  |  |  |  ----- day of week (0 - 7) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed

Environment variables

MAILTO sets the destination for cron reports

MAILTO="<email address>"

I strongly recommend that you set the MAILTO variable as it ensures that the report will go where you expect. The default destination may well be wrong.

Time and date lines

A normal entry consists of five numbers giving the minute, hour, day, month, day of week followed by the command to be executed.

The numbers are separated by spaces NOT commas. An asterisk "*" denotes a wildcard so to run "command" every day at 6am you might write:

  0  6  *  *  *  command

As a general rule you should use the day of week OR day of month options but not both, if both are specified then cron will execute the command if either match.

You can match multiple values using commas, so to run the above job on Mondays, Wednesdays and Fridays write:

  0  6  *  * 1,3,5 command

You can match ranges using a dash so to run every weekday:

  0  6  *  * 1-5 command

You can modify a wildcard to match special intervals using a slash, so to run every three hours:

  0 */3 *  *  * command

You can combine conditions, so to run every three hours but only on weekdays:

  0 */3 *  * 1-5 command

Special lines:

Name: When it runs: Equivalent to:
@yearly or @annually   Run once a year.
0 0 1 1 *
@monthly   Run once a month.
0 0 1 * *
@weekly   Run once a week.
0 0 * * 0
@daily or @midnight Run once a day.
0 0 * * *
@hourly Run once an hour.
0 * * * *
@reboot   Run once, at startup. No equivalent

 The @reboot line is particularly interesting as it's a handy way to start a service without installing a start-stop script or run a service as a user other than root.

The master crontab format

By "Master" I am referring to the crontabs located in /etc

These are not edited by the crontab command so they are a good place to put crontab lines that start administrative tools that you do not want tampered with.

The format is similar to the above except an extra column is added between the date and the command giving the username that the command should run as eg:

  0 */3 *  * 1-5 root command

These commands can be put in a text file called /etc/crontab,
or in a text file in a directory /etc/cron/ (normally with the same name as the program it runs).

Please note: I could not get the @reboot special line to function when put in /etc/crontab

 

Home Up thttpd Web Server Cherokee web CRON+SSMTP Samba Velleman K8055 Perl Module GD in Perl Native