Saturday, May 4, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 599  / 2 Years ago, fri, january 7, 2022, 7:36:02

It works fine from terminal, but when called from a thread in python, it fails.



history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00


from python, here's what I did:



halt = "history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00"
thread.start_new_thread(lambda: commands.getoutput(halt),())


I used lambda after trying it for other commands like opening nautilus or gedit, which works fine, but shutdown command isn't!


More From » command-line

 Answers
7

This:



history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00


is not actually a command, it's a fragment of shell script. So you need to have the shell execute it. Something like this:



import os
halt = "history -d $((HISTCMD-1)) && echo 'password' | sudo -S shutdown -h 08:00"
os.system(halt)

[#29193] Sunday, January 9, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ticrew

Total Points: 190
Total Questions: 111
Total Answers: 99

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;