I everyone,
I try to use sudo command inside my python script but it didn’t work. Here is my code:
#!/usr/bin/python
import os,sys
import subprocess
def main():
"Run the application"
print ('hello! this is a snap.')
os.system("sudo mkdir /home/kevinbertrand/test")
if __name__ == '__main__':
main()
When I run this code directly with “sudo python3 app.py” or even “python3 app.py” it works. The message is printed and the directory is created. But when I compile the snap, with this snap craft.yaml:
name: helloworld
version: '0.0.1'
#version-script: git describe --abbrev=1 --tags
summary: python test package
description: |
test app for python
base: core18
grade: stable
confinement: strict
apps:
helloworld:
command: bin/hello
plugs: [home]
parts:
helloworld:
plugin: python
python-version: python3
source: .
stage-packages: [ncbi-blast+]
The message is printed but the directory is not created and I get this message for the “sudo mkdir”:
sh: 1: sudo: Permission denied
Have you an idea to avoid this permission denied problem ?
The error you see is because, in the confined snap, you don’t have permissions to execute the sudo
executable.
However, using sudo
doesn’t really make a lot of sense: If you successfully run a command as root it will still be constrained by the snap’s confinement.
Maybe if you tell us what problem you hoped to solve with sudo
there will be a more useful answer.
Yes that’s right, the confinement was not set in devmode. So I changed it and test it and it worked .
But now the problem is when I add more command in my program. So I follow this tests:
- Test the command directly in the terminal:
netmgr -i ioyt_address set: 1234:1234:1234:1234:1234:1234
-> internal error, please report: running “netmgr” failed: open /snap/netmgr/x1/meta/snap.yaml: permission denied
- Test the sudo command directly in the terminal:
sudo netmgr -i ioyt_address set: 1234:1234:1234:1234:1234:1234
-> AFTR set
- Test it in a setup.py file with the no-sudo command:
netmgr -i ioyt_address set: 1234:1234:1234:1234:1234:1234
Run the script with: python3 test.py
->internal error, please report: running “netmgr” failed: open /snap/netmgr/x1/meta/snap.yaml: permission denied
- Test it in a setup.py file:
netmgr -i ioyt_address set: 1234:1234:1234:1234:1234:1234
Run the script with: sudo python3 test.py
-> AFTR set
- Test it in a setup.py file with the sudo command:
sudo netmgr -i ioyt_address set: 1234:1234:1234:1234:1234:1234
Run the script with: python3 test.py
-> AFTR set
- snap it with “grade: devel” and “confinement: devmode”. Run the snap with “iotr-configuration”
-> sh: 1: netmgr not found
- After finding netmgr (/snap/bin/netmgr), re-install the snap and once again run the snap with “iotr-configuration”
-> internal error, please report: running “netmgr” failed: open /snap/netmgr/x1/meta/snap.yaml: permission denied
- Maybe by starting the snap with “sudo iotr-configuration”:
-> cannot open /run/snapd/ns/snap.netmgr.info: Permission denied
Here is my tests and I’m stuck here now…