I'm always amazed when I learn something new about a tool that I've been using for a rather long time. Take for example, cron. Not your run of the mill, everyday cron. I'm talking about the smarter cron that comes with most Linux distributions nowadays: Vixie cron.
(Vixie cron is named after Paul Vixie, the creator of BIND and other Unix goodness.)
While looking at the manual page for the crontab file format, I discovered a chunk that I'd never seen before:
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 * * * *".
Hmm. @reboot. Isn't that handy. There's an easy way to give users the ability to run something at boot time without root access.
Posted by jzawodn at October 19, 2003 06:19 AM
Well I'll be damned. I've used cron for probably a decade and never knew - nice catch there :-)
Neat, though I like to stagger-start my nightly cleanup tasks. @reboot solves a problem, though.
I only came across that too about a month ago:
http://ben.milleare.com/archives/000171.html
It's amazing the things we miss :-)
The only cron job I can think of to run at roboot as a user would be:
echo "Manage the server better!" | mail admin@myhost.com
:)
I don't see anything in "man 5 crontab" (or the other man pages for that matter). It says 24 Jan 1994 at the bottom, maybe there's a new version.
It's curious to see that's documented in FreeBSD but undocumented in Linux, despite the fact that the both use Vixie's cron. I use @reboot to run fetchmail on startup and to receive notifications about unexpected reboots...
Draco:
What distro are you using? It's clearly there on my Debian box(es).
Jeremy: The distro is Red Hat 9 and there is no mention about @frequency features in crontab(5). But you're completely right, It appears in Debian's man pages. Sometimes I forget how different Linux distros are.
it doesn't appear in the Mandrake man pages either. great tip btw.
So what is the "easy way to give users the ability to run something at boot time without root access" ??
At login time I know there is .bash_profile, then .xinitrd or kde/autostart/ ... but at boot time ??
Thanks
Bruno
Yes.
This is perfect for those users that want their mudd to autostart after a reboot without giving them any root access to start it.
I run a teamspeak server and it has to start as a user, but not as root. It rarely ever dies, but the crontab I made was always trying to start it at intervals. This will start it once and be done.
To all those who suggest using @reboot to do start-up things:
Most modern GNU/Linux distributions have /etc/init.d/ or something like it, which is a collection of scripts controlling various services.
For example, on a Gentoo box, you could do:
/etc/init.d/apache2 restart
And it'd restart Apache.
You can add your own of these, which should be a whole lot cleaner, and of course, these can be scheduled to be run at system start - that's half their point.
They'll also allow you to set up dependencies, so that you might load your TeamSpeak server after the PostgreSQL database has started. (Not that TeamSpeak currently supports PostgreSQL, but you get my point.)
Please be aware that the 'reboot' doesn't happen when the machine boots up - it happens when cron boots up.
/etc/init.d/cron stop
/etc/init.d/cron start
Who says you have to use this in the root crontab? A normal user can have a crontab and use the @reboot.