(resolved) Running a command at login

Hello, :slight_smile:

I’m new here, and could do with some help.

I’m planing on using my Raspberry Pi without a screen. How can I get DietPi to login to the terminal without needing a username or password?
I tried following a guild about how to do it in Raspbian, and it wouldn’t boot afterwards! Had to re-install DietPi.

Also, I need my Pi to run a command in the terminal at startup. How can I do this?

Thank you.

How can I get DietPi to login to the terminal without needing a username or password?

  • download KiTTY download latest version
  • extract to desktop
  • open the folder
  • open kitty_portable.exe
  • click/select “dietpi” from the list
  • click “load”
  • change the IP address at the top to the IP address of your RPi
  • click “Save(d)”
  • double click dietpi from the list to connect

You should now be connected to your RPi using a SSH connection.

If your root password is different from dietpi, you’ll will need to change it under the “data” menu on the left.

Also, I need my Pi to run a command in the terminal at startup. How can I do this?

For doing something once boot is completed:
You can add bash commands to /etc/rc.local.

To edit the rc.local file (make sure “exit 0” is at the bottom):

nano /etc/rc.local

Quick example, this run the command “hello” once boot is completed:

sed -i "/exit 0/c\Hello" /etc/rc.local
echo -e "\nexit 0" >> /etc/rc.local

If you want to do something after login. you’ll need to edit the /root/.bashrc file:
Quick example, this run the command “Hello” once a user logs in:

echo -e "Hello" >> /root/.bashrc

That you very much for your quick reply Fourdee. :slight_smile:

Your answer fully answers my second question, but I don’t think I asked the first question well enough.

I want to be able to log in to Raspberry Pi, on the Pi itself (not via SSH using a computer), without any typing as I’m using it without a screen, and won’t be able to see if I’ve typed the password in wrong.

Basically I’m using it as a typewriter. I want to be able to turn the Pi on, do some writing, save the writing, and turn the Pi off.

The command I’m using to open the text editor (NANO) is:

now=$(date +"%Y-%m-%d %r")
nano -B -t /Writing/"$now.txt"

I tried adding this to the rc.local file, and it did open NANO without me having to enter a username or password, but it wouldn’t let me save or exit NANO (none of the non letter keyboard keys were working, so I couldn’t do ‘Ctrl X’ to exit. I’m guessing they weren’t working because I wasn’t logged in yet).

Is it possible to remove the need to login all together, or for it to automatically log me in?

Now I understand!
Sounds like a strange yet exciting setup, but yes, the system can be setup to automatically login after boot.
The code below will copy the DietPi auto login inittab file to your system. Once done, reboot and your system will automatically login after boot each time.

cp /DietPi/dietpi/conf/inittab_desktop /etc/inittab



The command I’m using to open the text editor (NANO) is:

now=$(date +“%Y-%m-%d %r”)
nano -B -t /Writing/“$now.txt”

>

rc.local runs before any login occurs. So you will need to move your code to /root/.bashrc as this will run during login.

**Lets move your code to a script file that we can execute. Save the file as /root/typewriter.sh :**

```text
#!/bin/bash
#Typewriter startup script
#-------------------------------
now=$(date +"%Y-%m-%d %r")
nano -B -t /Writing/"$now.txt"

Allow it to execute:

chmod +x /root/typewriter.sh

Finally, add your script to the end of the .bashrc file, so it runs during login:

echo -e "/root/typewriter.sh" >> /root/.bashrc



I tried adding this to the rc.local file, and it did open NANO without me having to enter a username or password, but it wouldn’t let me save or exit NANO (none of the non letter keyboard keys were working, so I couldn’t do ‘Ctrl X’ to exit. I’m guessing they weren’t working because I wasn’t logged in yet).

Make sure the folder /Writing has been created:

mkdir -p /Writing

Thank you so much Fourdee!That works perfectly and I’ve learned a lot in the process! Oh, and I’m loving DietPi by the way.

I’ve just spotted that the date/time bit of my code isn’t updating on startup (it’s the same as when I turned off last time). :frowning: If I run the code after the Pi has fully started up it does update.

Do you know of a way to fix this?

Could I write a different bit of code that gets the Pi to update the date/time and then run my code?

:slight_smile:

DietPi launches ntpd during boot. Your login script is probably being run before ntpd could obtain the actual time over the internet.

You’ll need to add a while loop that waits for the ntpd process to finish, before starting your script.

#!/bin/bash
#Typewriter startup script
#-------------------------------
#Wait for NTPD to complete updating time over net
while (( $(ps aux | grep -ci -m1 "[n]tpd") == 1 )); do
    echo -e "Waiting for NTPD to finish.."
    sleep 1
done

#Now run my section
now=$(date +"%Y-%m-%d %r")
nano -B -t /Writing/"$now.txt"

The ensures grep doesn’t include its own process from being trigged as 1

Thank you again Fourdee! I’ve just tested it and it works perfectly.

You’re amazing! Thank you. :smiley: