How to Run a Command on First Boot?

Title: How to Run a Command on First Boot?

I downloaded ‘DietPi_RPi-ARMv8-Bookworm.img’ for my Raspberry Pi 4 and burned it onto an SD card using Etcher on a Windows 10 machine. After a successful write, I can see the SD card mounted as a ‘boot’ drive with a collection of files, including ‘dietpi-wifi.txt’. I edited the ‘dietpi-wifi.txt’ file and provided my WiFi’s SSID and password. Pi is running headless without keyboard or anything attached.

However, after inserting the SD card into my Raspberry Pi, and powering it on, the Raspberry Pi does connect to the WiFi, but I am unable to determine its IP address. I am in a corporate environment and can not run WiFi Scanner tools. I tried SSHing using hostname, ‘dietpi’, as set in the ‘dietpi.txt’ file but to no avail although both are connected to same WiFi.

Desired Solution:
I want to execute the following commands right after the Raspberry Pi boots up and connects to my WiFi so that i get notified about the local WLAN IP address of the Raspberry Pi.

dietpi_ip=$(ip addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
curl -d "$dietpi_ip" > /dev/null 2>&1

Request for Assistance:
Do you have any suggestions on how I can accomplish this?

you have a screen attached? Maybe a keyboard as well?

Otherwise you could use the automatic install process and execute your script at the end of the setup. Executing a script before login is not really intended.

Pi is running headless without keyboard attached. How can I set it up without SSHing and configuring it? Honestly i am looking for a solution that involves tinkering with the files present on my SD card.

Right after flashing the image, you can adjust various settings within dietpi.txt. This needs to happen before first initial boot as these AUTO install settings are applied only once :slight_smile:

1 Like

I have created the following two .sh files at /boot.

with same contents, i.e.,

dietpi_ip=$(ip addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
curl -d "$dietpi_ip" > /dev/null 2>&1

and then put the SD card in Pi and powered it on, but these scripts never run. Any idea where am i doing wrong?

  • Allows you to automatically execute a custom script before network is up on first boot. This way you can’t determine an IP address.
  • Allows you to automatically execute a custom script at the end of DietPi install. You would need to enable automatic install as well

This should start the install process automatically and you should see it on screen.

Yes, but how can i login without the IP address of raspberry pi? I have set AUTO_SETUP_AUTOMATED=1 but I guess I need to login first and then automatic installation of software begin along with running of my custom script.
Sorry I am noob to this. All I want is that when I put SD after burning image to my PI and power it on, it connects to Wi-Fi and let me know its IP address. Then I can SSH using IP address provided and install software and configure dietpi manually.

No, using AUTO_SETUP_AUTOMATED=1 will start whole process automatically without any user input.

Your case is special as you don’t know the IP withing your environment. Therefore, I recommend, to use the fully automatic install process first to get the system up and running. Having a screen attached should show the install process.

Another option you could think of is

This will automatically perform a user login on next reboot (after setup finished) and will show a banner like this

 DietPi v8.20.1 : 11:50 - Fri 08/11/23
 - Device model : NanoPi R5S/R5C (aarch64)
 - Uptime : up 2 hours, 21 minutes
 - CPU temp : 37 °C / 98 °F : Cool runnings
 - LAN IP : 192.168.x.x (eth0)

hi dear @Joulinar
i was looking at this Raspbian Firstboot and realised that this is what i was looking for. This solution is for raspbian os. Can we do the same for dietpi? or any easy method to do it on dietpi?
Note/Disclosure: I have also opened a feature request at github.

Have you already tried the automatic setup function? It would be much easier than creating new images with new functions.

Furthermore, you can also recreate the function of Raspbian Firstboot yourself. It is quite well described on the GitHub page. GitHub - nmcclain/raspberian-firstboot: A lightly modified Raspbian-light image supporting first boot customization.

We already have scripts that can be executed at the first initial boot. But they are executed before the network is online, which was not helpful in your case.

none of the solution worked. unfortunately, not even the one I was looking at i.e., method. I have tried systemd, init.d, cron etc but these tricks do not work on first boot. Yes, they all do work perfectly if I set them up after first login through SSH. For now, I guess there is no easy solution to my problem.

Still the question did you tried the automatic installation process? This will setup your system quite easy in 5 minutes time.

Yes, this worked. After setting the flag to 1, I inserted the SD card into my Raspberry Pi and waited for the specified duration mentioned below. This allowed the automated setup to complete, and it executed my script ( as expected.

Wait Times

  • Raspberry Pi 0W v 1.1 512 MB RAM (2017) _________ Wait time: 15 minutes.
  • Raspberry Pi 3 Model B+ 1GB RAM (2017) __________ Wait time: 5 minutes.
  • Raspberry Pi 4 Model B - 4GB RAM (2018) __________ Wait time: 5 minutes.

at least a solution for your challenge :slight_smile:

1 Like