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 #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)

Is there an easy, scriptable way to do this?

  • 1
    RunOnce is an artifact of Windows resulting from problems completing configuration before a reboot. Is there any reason you can't run your script before reboot? The above solution appears to be a reasonable clone of RunOnce.
    – BillThor
    Commented Jun 5, 2010 at 0:49
  • I'm surprised there isn't a nice tool for this. Or maybe there is and I haven't discovered it yet. Commented May 28, 2022 at 2:12

10 Answers 10


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 using mkdir -p.

Create the script /usr/local/bin/runonce as follows:

for file in /etc/local/runonce.d/*
    if [ ! -f "$file" ]
    mv "$file" "/etc/local/runonce.d/ran/$file.$(date +%Y%m%dT%H%M%S)"
    logger -t runonce -p local3.info "$file"

Now place any script you want run at the next reboot (once only) in the directory /etc/local/runonce.d and chown and chmod +x it appropriately. Once it's been run, you'll find it moved to the ran subdirectory and the date and time appended to its name. There will also be an entry in your syslog.

  • 3
    Thanks for your answer. This solution is great. It technically solves my problem, however it seems like there's a lot of preparation of infrastructure required to make this work. It's not portable. I think your solution would ideally be baked into a Linux distribution (I'm not sure why it isn't!). Your answer inspired my ultimate solution, which I've also posted as an answer. Thanks again! Commented Jun 9, 2010 at 18:51
  • What made you choose local3, versus any of the other facilities between 0 and 7? Commented Jun 10, 2010 at 19:49
  • 3
    @Christopher: A dice roll is always the best method. Seriously, though, for an example it didn't matter and that's the key my finger landed on. Besides, I don't own any eight-sided die. Commented Jun 10, 2010 at 20:19
  • @Dennis: Got it, thanks. Coincidentally, local3 is the local facility that appears in man logger. Commented Jun 10, 2010 at 22:39
  • Does the $file variable contain full path or just the file name? Commented Feb 4, 2017 at 12:09

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 ultimately felt that it required too many steps to set up.
  • It requires root access.

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.


if [[ $# -eq 0 ]]; then
    echo "Schedules a command to be run after the next reboot."
    echo "Usage: $(basename $0) <command>"
    echo "       $(basename $0) -p <path> <command>"
    echo "       $(basename $0) -r <command>"

    while getopts ":r:p:" optionName; do
        case "$optionName" in
            r) REMOVE=1; COMMAND=$OPTARG;;
            p) SCRIPTPATH=$OPTARG;;

    SCRIPT="${HOME}/.$(basename $0)_$(echo $COMMAND | sed 's/[^a-zA-Z0-9_]/_/g')"

    if [[ ! -f $SCRIPT ]]; then
        echo "PATH=$SCRIPTPATH" >> $SCRIPT
        echo "cd $(pwd)"        >> $SCRIPT
        echo "logger -t $(basename $0) -p local3.info \"COMMAND=$COMMAND ; USER=\$(whoami) ($(logname)) ; PWD=$(pwd) ; PATH=\$PATH\"" >> $SCRIPT
        echo "$COMMAND | logger -t $(basename $0) -p local3.info" >> $SCRIPT
        echo "$0 -r \"$(echo $COMMAND | sed 's/\"/\\\"/g')\""     >> $SCRIPT
        chmod +x $SCRIPT

    CRONTAB="${HOME}/.$(basename $0)_temp_crontab_$RANDOM"
    ENTRY="@reboot $SCRIPT"

    echo "$(crontab -l 2>/dev/null)" | grep -v "$ENTRY" | grep -v "^# DO NOT EDIT THIS FILE - edit the master and reinstall.$" | grep -v "^# ([^ ]* installed on [^)]*)$" | grep -v "^# (Cron version [^$]*\$[^$]*\$)$" > $CRONTAB

    if [[ $REMOVE -eq 0 ]]; then
        echo "$ENTRY" >> $CRONTAB

    crontab $CRONTAB
    rm $CRONTAB

    if [[ $REMOVE -ne 0 ]]; then
        rm $SCRIPT

Save this script (e.g.: runonce), chmod +x, and run:

$ runonce foo
$ runonce "echo \"I'm up. I swear I'll never email you again.\" | mail -s \"Server's Up\" $(whoami)"

In the event of a typo, you can remove a command from the runonce queue with the -r flag:

$ runonce fop
$ runonce -r fop
$ runonce foo

Using sudo works the way you'd expect it to work. Useful for starting a server just once after the next reboot.

myuser@myhost:/home/myuser$ sudo runonce foo
myuser@myhost:/home/myuser$ sudo crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/root/.runonce_temp_crontab_10478 installed on Wed Jun  9 16:56:00 2010)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
@reboot /root/.runonce_foo
myuser@myhost:/home/myuser$ sudo cat /root/.runonce_foo
cd /home/myuser
/home/myuser/bin/runonce -r "foo"

Some notes:

  • This script replicates the environment (PATH, working directory, user) it was invoked in.
  • It's designed to basically defer execution of a command as it would be executed "right here, right now" until after the next boot sequence.
  • Your script looks really handy. One thing to note is that it destructively strips comments out of the crontab. Commented Jun 9, 2010 at 21:39
  • @Dennis: Thanks. I originally didn't have that extra grep call in there, but all of the comments were piling up; three for every time I ran the script. I think I'll change the script to just always remove comment lines that look like those three auto-generated comments. Commented Jun 9, 2010 at 21:47
  • @Dennis: Done. The patterns could probably be better, but it works for me. Commented Jun 9, 2010 at 22:02
  • @Dennis: Actually, based on crontab.c, I think my patterns are just fine. (Search for "DO NOT EDIT THIS FILE" at opensource.apple.com/source/cron/cron-35/crontab/crontab.c.) Commented Jun 10, 2010 at 23:24

Create e.g. /root/runonce.sh:

#your command here
sed -i '/runonce.sh/d' /etc/rc.local

Add to /etc/rc.local:

  • 2
    This is pure genius. Don't forget to chmod +x the /root/runonce.sh. This is perfect for apt-get upgrade on azure machines that hang because walinuxagent blocks the dpkg
    – CarComp
    Commented Nov 9, 2017 at 19:19
  • This is too hacky (though it's also what I came up with at first).
    – iBug
    Commented Jul 1, 2019 at 16:15
  • This is deprecated? To run a script at startup, create it under /etc/systemd/system, create a service, etc.
    – Danijel
    Commented Nov 9, 2022 at 12:37

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:

# chkconfig: 345 99 10
# description: This script is designed to run once and then never again.

# Beginning of your custom one-time commands

plymouth-set-default-theme charge -R
dracut -f

# End of your custom one-time commands

# This script will run once
# If you would like to run it again.  run 'chkconfig run-once on' then reboot.
chkconfig run-once off
chkconfig --del run-once
  1. Name the script run-once
  2. Place the script in /etc/init.d/
  3. Enable the script chkconfig run-once on
  4. reboot

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) use at @reboot or at reboot, but you can use at now + 2 minutes and then shutdown -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.

  • Beware that if your system takes longer than 2 minutes until atd is stopped your command may run during shutdown. This could be avoided by stopping atd before sheduling the command with atd now and then rebooting.
    – mleu
    Commented Mar 20, 2018 at 10:25

To flesh out the suggestion from @mleu, for CentOS 7 you can do the following:

  1. Stop 'atd':
  • systemctl stop atd.service
  1. Register action with 'atd':
  • echo "bash '/var/tmp/myOnBootScript.sh'" | at now
  1. Reboot:
  • 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:

  1. systemd status:
  • systemctl status mywebserver.service
  1. In-built status tools:
  • pg_isready -U "${db_Super_User}" -d "${database_Name}"
  1. Direct verification the service is responding as expected:
  • psql -U "${db_Super_User}" -d "${database_Name}" -c "\\d pg_class"

Adapting solutions from @kaffeslangen, @geotheory and @CameronKerr, below is one that I used at the end of a VM bootstrapping script. The build script is executed via cloud-init on a new EC2 instance from an Ubuntu AMI. It installs and configures a bunch of stuff on first boot, and then reboots the system ready for use.

I wanted to send an email after the reboot with the status of the server and the log output from the build process, but only after the system has fully completed booting (otherwise the systemctl status command's output indicates a 'Starting' status and we don't yet know whether the system fully booted without other service failures). Hence the use of the 'at' command below.

Also, since my VM otherwise does not use /etc/rc.local, I can simply write it and then self-delete it, rather than creating a separate script and adding/removing an invocation for that within /etc/rc.local, as was done by @kaffeslangen.

cat <<EOT > /etc/rc.local
systemctl status --all 2>&1 | mail -s 'VM Build Result' [email protected] --attach=/var/log/cloud-init-output.log
rm \$0

chmod +x /etc/rc.local


in redhat and debian systems you can do that from /etc/rc.local, it's a kind of autoexec.bat.

  • 7
    That's going to get executed at each boot not just the next one only. Commented Jun 5, 2010 at 22:46
  • 1
    my bad, I misread the question. Thanks for the correction Commented Jun 6, 2010 at 8:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .