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?
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
.I really appreciate the effort put into Dennis Williamson's answer. I wanted to accept it as the answer to this question, as it is elegant and simple, however:
I think his solution would be great as an out-of-the-box feature of a Linux distribution.
That being said, I wrote my own script to accomplish more or less the same thing as Dennis's solution. It doesn't require any extra setup steps and it doesn't require root access.
Save this script (e.g.:
runonce
),chmod +x
, and run:In the event of a typo, you can remove a command from the runonce queue with the -r flag:
Using sudo works the way you'd expect it to work. Useful for starting a server just once after the next reboot.
Some notes:
Create e.g.
/root/runonce.sh
:Add to
/etc/rc.local
:I think this answer is the most elegant:
Place script in
/etc/init.d/script
and self-delete with last line:rm $0
Unless the script is 100% fail-proof, probably wise to handle exceptions to avoid a fatal error loop..
I used
chkconfig
to have my system automatically run a script once after boot and never again. If your system uses ckconfig (Fedora, RedHat, CentOs, etc) this will work.First the script:
run-once
/etc/init.d/
chkconfig run-once on
When the system boots your script will run once and never again.
That is, never again unless you want it to. You can always re-enable the script with the
chkconfig run-once on
command.I like this solution because it puts one and only one file on the system and because the run-once command can be re-issued if needed.
Set up the script in /etc/rc5.d with an S99 and have it delete itself after running.
You can do this with the
at
command, although I notice you can't (at least on RHEL 5, which I tested on) useat @reboot
orat reboot
, but you can useat now + 2 minutes
and thenshutdown -r now
.This doesn't require that you system take longer than 2 minutes to run.
It may be useful where you want 0 set-up, although I do rather wish the 'runonce' command was standard kit.
To flesh out the suggestion from @mleu, for CentOS 7 you can do the following:
systemctl stop atd.service
echo "bash '/var/tmp/myOnBootScript.sh'" | at now
systemctl reboot
This will run the script on boot, as soon as the 'atd' service is up and running. If you need to delay the script while other things finish starting up, like a web server or something, you can include that in your script. For example:
systemctl status mywebserver.service
pg_isready -U "${db_Super_User}" -d "${database_Name}"
psql -U "${db_Super_User}" -d "${database_Name}" -c "\\d pg_class"
in redhat and debian systems you can do that from /etc/rc.local, it's a kind of autoexec.bat.