0

I created a python code that requires sudo permission to run. The problem is that its an automated code which will run whenever the computer starts, so is there a way by which I can give it sudo permission only once and will run with sudo permission later without asking for any password?

I've already tried giving it +x permissions (I know that wouldn't work but anyway) by running sudo chmod +x main.py but that didn't helped

the work my code does is detecting a presence of removable devices(For eg. pendrive) in the computer and prints the filenames that are present in that device. My code prints all the stuff and does well when the device is already connected but if its connected later, it gives a traceback:

Traceback (most recent call last):
  File "/home/rapidfire69/RaPiD/coding/Projects/Python/PenDrive_Thief/PenDrive_Thief.py", line 17, in <module>
    myFunction()
  File "/home/rapidfire69/RaPiD/coding/Projects/Python/PenDrive_Thief/PenDrive_Thief.py", line 7, in myFunction
    raw_files = (os.listdir(base_path+devices[0]))
PermissionError: [Errno 13] Permission denied: '/media/username/pendrive'

My OS is Ubuntu 20.04. Python 3

Please help....

RPD_215
  • 13
  • 4
  • How do you run your script at startup? – jlandercy Sep 08 '20 at 05:51
  • by using the application named "starup application" which is present in ubuntu 20.04 by default.. – RPD_215 Sep 08 '20 at 06:44
  • To solve your problem you may consider using `systemctld` which is the modern way to orchestrate services on Debian/Ubuntu. It is easy to take the train on, reliable, and also manage rotating logs natively. That could be a great option to solve your problem. – jlandercy Sep 08 '20 at 07:08

1 Answers1

0

You may edit your /etc/sudoers file as described here and give a user, who executes your script passwordless sudo persmissions

 username ALL=(ALL) NOPASSWD: ALL
Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20
  • I haven't looked in detail at what is linked at that page, but it is worth emphasising that when you edit the sudoers file you should use the `visudo` command. If you don't like the editor which it offers, this can be changed by setting the `EDITOR` environment variable. – alani Sep 08 '20 at 06:00
  • Thanks, that would work but it will never ask for a password again from that user which is really unsafe, is there a way to do that but for only one file or script? – RPD_215 Sep 08 '20 at 06:50
  • take a look at SUID property of shell script, e.g. here: https://stackoverflow.com/questions/33565729/why-do-my-setuid-root-bash-shell-scripts-not-work – Łukasz Ślusarczyk Sep 08 '20 at 08:30