Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there an idiomatic way in Ubuntu to run a script only the first time a machine is booted? (EC2).

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

No. But you might want to place your script in /etc/init.d/script, and self-delete it:

#!/bin/bash

echo "Bump! I'm your first-boot script."

# Delete me
rm $0
share|improve this answer
    
Please note, $0 is bash-specific (version >= 3). For compatibility purpose you can provide script file name instead, making this less generic: rm /etc/init.d/script –  Andrejs Cainikovs Jun 27 '12 at 16:07
add comment

Create a tracking file when the script runs. If the file already exists, exit the script.

share|improve this answer
    
While it might not seem so at first glance, this may be a better solution than deleting the script as it retains the possibility to trigger it again, should you ever want to. –  msanford May 8 at 14:43
add comment

Combining the first two answers Assuming you name your script /usr/local/bin/firstboot.sh put it at the end of /etc/rc.local (this scripts runs on every boot) the scripts goes like this

#!/bin/bash

FLAG="/var/log/firstboot.log"
if [ ! -f $FLAG ]; then
   #Put here your initialization sentences
   echo "This is the first boot"

   #the next line creates an empty file so it won't run the next boot
   touch $FLAG
else
   echo "Do nothing"
fi
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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