启动时在终端上运行python脚本

问题描述 投票:0回答:2

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

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文件:

#!/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

我要去哪里错了?

python python-3.x cron ubuntu-16.04
2个回答
0
投票

看起来您的程序在input()行终止,可能会导致EOFError


0
投票

只需将该脚本放入.bashrc(如果您使用的是bash),那么它将在用户每次打开交互式终端时执行。

Cron作业是非交互执行的,这就是为什么它无法读取用户输入的原因。

© www.soinside.com 2019 - 2024. All rights reserved.