I'd like to schedule a command to run after reboot on a Linux box. I know how to do this so the command consistently runs after every reboot with a @reboot
crontab entry, however I only want the command to run once. After it runs, it should be removed from the queue of commands to run. I'm essentially looking for a Linux equivalent to RunOnce in the Windows world.
In case it matters:
$ uname -a
Linux devbox 2.6.27.19-5-default #1 SMP 2009-02-28 04:40:21 +0100 x86_64 x86_64 x86_64 GNU/Linux
$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
$ cat /etc/SuSE-release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 0
Is there an easy, scriptable way to do this?
Best Answer
Create an
@reboot
entry in your crontab to run a script called/usr/local/bin/runonce
.Create a directory structure called
/etc/local/runonce.d/ran
usingmkdir -p
.Create the script
/usr/local/bin/runonce
as follows:Now place any script you want run at the next reboot (once only) in the directory
/etc/local/runonce.d
andchown
andchmod +x
it appropriately. Once it's been run, you'll find it moved to theran
subdirectory and the date and time appended to its name. There will also be an entry in yoursyslog
.