FreeBSD and cron
Author: jason
Date: 2003-11-23
Category: Technical

Want to run scripts at startup? Want to do it under your username and not root? I'm using FreeBSD 4.8, but this should work with most Linux boxes.

I wanted to run iroffer (dcc server) at startup. iroffer can not run as root. So I couldn't make a script and place it in /usr/local/etc/rc.d. I had to use cron. *or figure out how to run a script in rc.d as a common user and not root, but that isn't recommended*

So when I was scanning through the iroffer files I saw iroffer.cron. I opened it up and read it. Seemed pretty easy, but it was missing a couple items.

Code

#!/bin/sh

# --- How To Use Cron ---
#
# Edit the iroffer.cron file's iroffer_dir, iroffer_exec, and
# iroffer_pid variables
#
# then crontab -e and place the following line in the editor
#
# */5 * * * * /full/path/to/iroffer/iroffer.cron

iroffer_dir="/full/path/to/iroffer/"
iroffer_exec="./iroffer -b sample.config"
iroffer_pid="mybot.pid"


That's the info they wanted me to use. So I did it, but it didn't work!!

Put your iroffer files somewhere, like /usr/local/bin. I would recommend placing the pid/log files in a iroffer-files folder where your username has control over them. Edit the iroffer.cron file, replacing the info. Then edit your cron.

First remember to leave root! Just be yourself! What's the matter, don't you like being you? So running "crontab -e" will make a tab for you. You could also run "crontab -u USERNAME -e". To make someone else a tab.

But then I typed "*/5 * * * * /full/path/to/iroffer/iroffer.cron" And it STILL didn't work!!

So I started reading about cron and crontab.
http://www.freebsd.org/cgi/man.cgi?query=cron&sektion=8
http://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=1
http://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=5
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-starting-services.html

Here's a quick run down of the * ...

Code


field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)



So there are 5 fields, each one can be set to reference a particular piece of time.

5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

Runs five minutes after midnight, every day. And some more types:

Code


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 * * * *"



I found out I could use a "@reboot"!!! I made sure I could execute iroffer.cron: chmod 755 iroffer.cron. Then I edited my crontab. As soon as I finished, boom, iroffer started up.

So to recap:
>crontab -e
Now you'll be in vi, so hit the Insert key. Then type:
@reboot /path/to/cron/file/iroffer.cron
Hit the Escape key, and type:
:wq!
You should get the following: crontab: installing new crontab. And if you set your permissions and path info correctlyt iroffer should startup immediately!

Now you're a cron geek!

*********
written by jasonthomasfrance
date 23 Nov 2003
systems RedHat 9.0, FreeBSD 4.8
*********



jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009