Issue
below is my code. I use a crontab for autostart, but everything works not as I would like. 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).
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?
Solution
using a daemon service would probably be more appropriate than "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. 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
Answered By - Jerome Favrou