I need to run a program or script only once at next reboot of my Raspi.

For instance, if i would need to rename a file and change its permissions, the script

cp file.old file.new && chmod +rwx file.new && rm file.old

will do the trick, but... how do I make sure it runs only once at next reboot, and not at subsequents reboots?

Maybe doing some work on /etc/rc.local or /etc/crontab, but not sure how to implement it.

I´ve tried creating this file "runonce" in /etc/init.d, with chmod +x:

# Provides:          runonce
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: one-time script
# Description:       hope it works
echo 'Copying file' > /home/pi/runonce.log
sudo cp /home/pi/file.old /home/pi/file.new
sudo chmod +rwx /home/pi/file.new
sudo rm /home/pi/file.old
echo 'Deleting script' >> /home/pi/runonce.log
sudo rm /etc/init.d/runonce
echo 'Done!' >> /home/pi/runonce.log

but it seems it´s not in the required format. Nothing happens. Any ideas?


  • what is the actual problem? ... your statement at the end of your post seems to say that you are not able to run any scripts at boot time
    – jsotola
    Oct 24, 2020 at 18:54
  • @jsotola: Maybe I failed to be clear. I want a given set of instructions to be run once and only once at the next reboot. The /etc/init.d/runonce file that I wrote is not working. None of it contents are executed. My Idea was to delete the file itself after the tasks are completed (as you can see in the file).. but the script never runs. Maybe it needs something besides the LSB header. Thanks.
    – PiBer2
    Oct 24, 2020 at 19:32
  • this may help ... occasionalcoherence.com/2011/11/27/runonce-for-linux
    – jsotola
    Oct 24, 2020 at 19:44
  • @jsotola: that´s a great resource Thanks!
    – PiBer2
    Oct 24, 2020 at 20:44

2 Answers 2


You are using the old style SysV init system, but that is deprecated since years and not really installed. For compatibility it is only emulated by the new systemd init system. If you really want to use SysV you should have a look at man systemd-sysv-generator why your scripts are not running.

I would use direct a simple systemd service to do what you want. For testing I used an echo bash script:

rpi ~$ sudo bash -c 'echo echo RunOnce executed > /var/local/runonce.sh'

Then create the Unit file for the service with:

rpi ~$ sudo systemctl edit --force --full runonce.service

In the empty editor insert these statements, save them and quit the editor:

Description=Run script once on next boot

ExecStart=/bin/bash /var/local/runonce.sh
ExecStop=/bin/rm /var/local/runonce.sh


Enable the service with:

rpi ~$ sudo systemctl enable runonce.service

Reboot and check the status of the service:

rpi ~$ systemctl status runonce.service

Now you can just copy a bash script to /var/local/runonce.sh and it will be executed only one time on next reboot.

By the way, there is no need to copy and delete your script for renaming. You can rename it with:

rpi ~$ mv file.old file.new && chmod +rwx file.new

End your script with a line which deletes it. For a bash script which is run from the directory where it resides, the self-delete command is simply

rm $0

Most autorun systems will ignore a non-existent script which they are supposed to run, or can be instructed to ignore it. For instance, in /etc/rc.local you could write:

test -f /path/to/script && /path/to/script
  • Thanks a lot @Dmitry Grigoryev, you are right in that trying to run a non-existing script is not critical. The "rm $0" will do the trick.
    – PiBer2
    Oct 24, 2020 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.