1

I want to run a script after pi boots but before log in (Raspbian Stretch Lite).

It should run after all the other configurations are done. So I thought about finding and changing the RPi log in Menu. I want to personalize this menu, besides running some other configurations.

For me, running a script at init.d didn't work:

$ cd /etc/init.d
# chmod a+x TEST  
# chmod 777 TEST
# update-rc.d TEST defaults
# reboot

Then it goes directly to the normal log in menu without any print. Also when I run

# service --status-all I get [ + ] TEST. So it does run, but can't print anything to the Terminal.

Adding my script to /etc/bash.bashrc does work, but this file is executed after log in.

UPDATE: found that the easiest way to run script before login is simply edit the /etc/rc.local file. echo command works (print at the terminal works) but after that, at the read command, I get:

[ FAILED] Failed to start /etc/rc.local Compatibility.

What can I do? Thanks!

TEST script

#!/bin/bash
# /etc/init.d/TEST

echo -e "\n THIS IS A TEST"
echo -e "\n THIS IS A TEST"
echo -e "\n THIS IS A TEST"
read var
echo -e "\n You typed $var"
2

You can use systemd to run a service before getty can prompt for a login. Create your service by adding a file called pre-login-script.service in /lib/systemd/system/, with the following contents:

[Unit]
Description=Pre Login Script
After=sysinit.target

[Service]
Type=oneshot
RemainAfterExit=yes
StandardInput=tty-force
StandardOutput=tty-force
TTYReset=yes
ExecStart=/path/to/script

[Install]
WantedBy=getty.target

(you'll need to change /path/to/script as appropriate).

This tells systemd that you want your script to run before getty does, and to take the console's stdin and stdout and pass control to your program. It also resets the TTY, blanking out any previous contents.

You'll need to enable the service with:

sudo systemctl enable pre-login-script.service
  • I did a autologin.conf at /etc/systemd/system/getty@tty1.service.d. What is the difference from the folders /lib/systemd/system/ and /etc/systemd/system? I will try your suggestion! – Ivo Dutra Jan 25 '18 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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