Sam Perry
Sam Perry

Reputation: 2614

Run Script in Foreground On Boot Raspberry Pi

I've a script to run on boot and I'd like to use the keyboard to interact with the script. I've successful set this up to run in crontab; however, the script runs in the background and I can't use the keyboard to interact with the script. Here's a simplified example of the script:

def write_to_txt(item_to_write):
    with open("my_txt_file.txt", "a") as myfile:
        myfile.write('\n'+str(item_to_write))

while True:
    keys_to_enter = raw_input()
    write_to_txt(keys_to_enter)

Please could someone point me in the right direction?

Upvotes: 2

Views: 3594

Answers (3)

tfranch
tfranch

Reputation: 1

You can run a script in foreground at boot by adding a line to /etc/rc.local

This works in my experience, in particular if the Raspberry pi is configured to wait for network to be available when booting

Upvotes: 0

Sam Perry
Sam Perry

Reputation: 2614

I found out how to run the script on boot and allow the keyboard to interact with the program. To the ~/.bashrc file, I appended:

sudo python /home/pi/example.py

Upvotes: 2

Charith A.
Charith A.

Reputation: 106

If I understand correctly you want your program to attach its stdin to tty1? I.e. the terminal which you see on screen if you have a display hooked up - this is where by default keyboard input would end up if X windows is not installed or the tty is not switched with Ctrl+Alt+Fx?

Is moving the ownership of the background script process to the shell on tty1 an option? If so, the easiest may be to auto-login the Pi (or the user will need to login with the keyboard on startup). Then auto-start the program on tty1 so its stdin/stdout is tied to tty1.

To achieve the latter, I think you can put its invocation into one of the bash startup scripts, something like what is suggested here: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=7192

Upvotes: 0

Related Questions