简体   繁体   English

如何更改代码以便在重新启动后它可以工作并且在终端中可见?

[英]How to change the code so that after the reboot it works and is visible in the terminal?

below is my code.下面是我的代码。 I use a crontab for autostart, but everything works not as I would like.我使用 crontab 进行自动启动,但一切都不像我想要的那样工作。 The goal was that after starting raspberry, the terminal would open, and the program would run in it so that when you typed "bye" the program ended (which I already have).目标是在启动 raspberry 后,终端会打开,程序会在其中运行,这样当你输入“再见”时,程序就会结束(我已经有了)。

import os
import sys
import subprocess
if os.geteuid() == 0:
    while True:
        file = open("here is my file path")
        print(file.read())
        my_input = input("Do you want to close this program? Type bye if yes")
        if my_input== 'bye':
            print("bye")
            break
else:
    subprocess.call(['sudo', 'python3'] + sys.argv)

How to change the code so that after the reboot it works and is visible in the terminal?如何更改代码以便在重新启动后它可以工作并且在终端中可见?

using a daemon service would probably be more appropriate than "cron".使用守护程序服务可能比“cron”更合适。 but it doesn't matter because your script checks if the user is root ( geteuid() ) and tries to do a sudo if this is not the case, so the sudo will only fail because the script doesn't have the rights.但这没关系,因为您的脚本会检查用户是否为 root ( geteuid() ),如果不是这种情况,则会尝试执行 sudo,因此 sudo 只会失败,因为脚本没有权限。 If your script has a standard user uid, and it is executed by root, then you must make a call to seteuid(0), to set the rights to root如果你的脚本有一个标准的用户 uid,并且它是由 root 执行的,那么你必须调用 seteuid(0) 来设置 root 的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于终端,但不适用于 VS 代码 - Works in terminal but not in VS code 如何更改 Visual Code 终端中的输出颜色? - How to change the ouput color in the terminal of Visual Code? 重启后使用cron在终端窗口中启动python脚本 - Using cron to launch python script in terminal window after reboot 我正在尝试使用 while 循环而不是 for 循环。 我将如何更改此代码以使其在 python 中使用 while 循环工作? - I'm trying to use a while loop instead of a for loop. How would I change this code so that it works using a while loop in python? 如何打包python源代码,以便将它们更改为“ .so”文件 - How to pack python source code so that they change into “.so” files 我如何才能更改此替代作品? - How am I able to change this so the substituting works? 如何在Python代码中修改文件路径,使其在其他计算机上工作? - How to modify a filepath in Python code so that it works on other computers? Python:如何修复此代码,使其可在Windows上使用? - Python: How can I fix this code so it works on Windows? 如何修复此数字排序python代码,以便其正常工作 - how to fix this number sorting python code so that it works correctly Python 在 vs 代码中工作,但在 vs 代码终端中不工作 - Python works in vs code but not in vs code terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM