Run script on boot, as ordinary user

Karl Vogel vogelke+unix at
Fri May 8 21:51:44 UTC 2009

>> On Thu, 7 May 2009 17:22:01 -0700, 
>> Nerius Landys <nlandys at> said:

N> Seems that @reboot in cron is what I need.  It's too bad that there's
N> no straightforward shutdown hook.

   I use something like the script below to send me a popup message
   whenever one of my boxes shuts down, planned or otherwise.  It assumes
   that if you can run cron jobs, you can be trusted to run something
   as yourself at system shutdown.

   For safety, nothing is run as root, and cron users can only run a
   script called called '/home/./$username/rc.d/shutdown'.  Add this
   to /etc/rc.shutdown:
      run-rc-shutdown | sh

   If you don't have "setuidgid" installed, replace with "su -c ..."

Karl Vogel                      I don't speak for the USAF or my company

The little boat gently drifted across the pond exactly the way a bowling
ball wouldn't.     --bizarre expressions found in student English papers

#!/usr/bin/perl -w
# run-rc-shutdown: print commands to run any non-root shutdown scripts.

use strict;
my ($dir, $dh, $home, $script, $uid, $usr);

$dir = '/var/cron/tabs';               # BSD.
#$dir = '/var/spool/cron/crontabs';    # Solaris.

opendir($dh, "$dir") || die "opendir $dir: $!\n";
my @users = sort (grep (!/^\./, readdir($dh)));

foreach (@users) {
    ($usr, $uid, $dir) = (getpwnam($_))[0,2,7];
    next unless $dir =~ m!/home/./$usr!;
    next unless $uid > 0;
    $script = $dir . '/rc.d/shutdown';
    print "/usr/local/bin/setuidgid $usr $script\n" if -x $script;


More information about the freebsd-questions mailing list