简体   繁体   中英

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?

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

If I understand correctly you want your program to attach its stdin to tty1? Ie 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM