(resolved) Running a command at login Topic is solved

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
Post Reply
panmanthe2nd

(resolved) Running a command at login

Post by panmanthe2nd »

Hello, :)

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.
User avatar
Fourdee
Site Admin
Posts: 2782
Joined: Tue Feb 06, 2007 1:36 pm

Re: Skipping login and running a command at startup

Post by Fourdee »

How can I get DietPi to login to the terminal without needing a username or password?
- download https://www.fosshub.com/KiTTY.html
- 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):

Code: Select all

nano /etc/rc.local
Quick example, this run the command "hello" once boot is completed:

Code: Select all

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:

Code: Select all

echo -e "Hello" >> /root/.bashrc
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
panmanthe2nd

Re: Skipping login and running a command at startup

Post by panmanthe2nd »

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

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:

Code: Select all

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?
User avatar
Fourdee
Site Admin
Posts: 2782
Joined: Tue Feb 06, 2007 1:36 pm

Re: Skipping login and running a command at startup

Post by Fourdee »

panmanthe2nd wrote: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.
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.

Code: Select all

cp /DietPi/dietpi/conf/inittab_desktop /etc/inittab
The command I'm using to open the text editor (NANO) is:

Code: Select all

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 :

Code: Select all

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

Code: Select all

chmod +x /root/typewriter.sh
Finally, add your script to the end of the .bashrc file, so it runs during login:

Code: Select all

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:

Code: Select all

mkdir -p /Writing
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
panmanthe2nd

Re: Skipping login and running a command at startup

Post by panmanthe2nd »

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.
panmanthe2nd

Re: Skipping login and running a command at startup

Post by panmanthe2nd »

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). :( 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?

:)
User avatar
Fourdee
Site Admin
Posts: 2782
Joined: Tue Feb 06, 2007 1:36 pm

Re: Skipping login and running a command at startup

Post by Fourdee »

panmanthe2nd wrote: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). :( 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?

:)
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.

Code: Select all

#!/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
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
panmanthe2nd

Re: Skipping login and running a command at startup

Post by panmanthe2nd »

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

You're amazing! Thank you. :D
Post Reply