Saturday, May 18, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 602  / 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
ticrew questions
Mon, Jan 10, 22, 07:20, 2 Years ago
Wed, Dec 8, 21, 19:25, 2 Years ago
Wed, May 5, 21, 08:17, 3 Years ago
Fri, Mar 10, 23, 17:21, 1 Year ago
Fri, Nov 4, 22, 06:43, 2 Years ago
;