登录时在终端中运行python脚本

登录时在终端中运行python脚本

问题描述:

我写了一个python脚本,其中包含一个无限循环菜单.我的目标是在用户登录时在终端中启动脚本并开始与其进行交互.我已经创建了launcher.sh文件,它将在启动时执行并将其注册到crontab中.但是,系统重新启动时会出现以下错误:

I have written a python script which contains a menu in an infinite loop. My goal is start the script in terminal when a user logs in and start interacting with it. I have created a launcher.sh file which will execute on startup and registered it in crontab. However, when system reboots it gives following error:

1: START
2: POWEROFF
Please make a choice: Traceback (most recent call last):
  File "menu.py", line 27, in <module>
    main()
  File "menu.py", line 16, in main
    choice = input ("Please make a choice: ")
EOFError: EOF when reading a line

这是我的剧本:

#!/usr/bin/python3
def main():
    while True:
        print("1: START")
        print("2: POWEROFF")
        choice = input ("Please make a choice: ")
        print(choice)
        ... other operations

这是我的launcher.sh文件:

Here is my launcher.sh file:

#!/bin/sh
# launcher.sh
cd /home/rao/Desktop/project
sudo python3 menu.py
cd /

和crontab行:

@reboot sh /home/rao/Desktop/project/launcher.sh >> /home/rao/Desktop/project/logs/status.log 2>&1

我要去哪里错了?

当我在启动应用程序

x-terminal-emulator -e "python3 /path-to-script/menu.py"