I'm curious about the best way (most secure way) to execute a python program and after it ends shutdown the system (using sudo in a ssh session).
What I would do initially is:
nohup python py_name.py && sudo shutdown -h now & or
nohup python py_name.py ; sudo shutdown -h now &
sudo because I'm in a ssh session and I can't shutdown without it and
& because I want to leave the system after executing)
The problems is that the python program is like 5-8 hour long and I don't want the shell prompt for pw after that (is somehow possible to prompt when executing first command?). My system is ubuntu 18.04 so I guess that "sudoing" both can't work because of the timeout of sudo (15 min?).
One option is to enter superuser mode for both commands or scripting it and executing
sudo ./script, but is it secure taking into account that it is executing a python program? (It's an easy home PC setup so maybe it's not a problem)
Is there any other way I'm not aware of?
----- EDIT -----
I tried to run
sudo sh -c 'python py_name.py && sudo shutdown -h now'.
sudo sh -c 'command' executes another shell to run the command and it could work this way because all the shell is in sudo. The problem is that I use a python environment and this new shell does not come activated with it.
The real solution is as said (by solution), changing the sudoers file
sudo visudo to add shutdown to the list that does not need password when it runs
sudo shutdown. Each sudoers file is different, but in my case, I needed to add
my_username ALL=(ALL) NOPASSWD: /sbin/shutdown, where the first "space" is tab, and
/sbin/shutdown is the path to the command you want to execute (you can check path with
command -v shutdown).