Run Script in Foreground On Boot Raspberry Pi

余生颓废 提交于 2020-01-13 20:38:27

问题


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?


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/35841581/run-script-in-foreground-on-boot-raspberry-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!