helpthepi
Posts: 50
Joined: Sat Apr 08, 2017 6:29 pm

Run any command with Sub-Processes

Wed May 23, 2018 4:08 pm

How do i run any command using subprocces.
CUrrent script:

Code: Select all

if ctx.message.author.id == "412372079242117123":
    mesg = ''.join(args)
    mesg = str(mesg)
    command_output = subprocess.check_output(mesg, shell=True, universal_newlines=True)
    await bot.say(command_output)
else:
    await bot.say("No noob")
Above dosent work and output

Code: Select all

aise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an 
 exception: CalledProcessError: Command 'vcgencmdmeasure_temp' returned 
  non-zero exit status 127
#LorienLegacies #BringBack8

helpthepi
Posts: 50
Joined: Sat Apr 08, 2017 6:29 pm

Re: Run any command with Processes

Wed May 23, 2018 4:09 pm

vcgencmd command was for test purposes only. want to know how to run any command
#LorienLegacies #BringBack8

B.Goode
Posts: 5818
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Run any command with Processes

Wed May 23, 2018 4:36 pm

Maybe the method you have used can be used to run any command? (You have only provided an inadequate extract, not the whole of the relevant part of the script.)

But the command you pass to the Operating System will need to be correct.

vcgencmdmeasure_temp is not. It will also fail if you enter it directly at a shell prompt.

scotty101
Posts: 2854
Joined: Fri Jun 08, 2012 6:03 pm

Re: Run any command with Processes

Wed May 23, 2018 4:42 pm

I'm not near a *nix compatible machine to test but the arguments are usually passed to to subprocess as a list of string i.e.

Code: Select all

command_output = subprocess.check_output(['vcgencmd', 'measure_temp'], shell=True, universal_newlines=True)
You could either try creating a list from args using

Code: Select all

mesg = list(args)
or joining the args with a space between then

Code: Select all

mesg = ' '.join(args)

Alternatively you can use a function like this which uses os.popen instead of subprocess.

Code: Select all

def exec_cmd(cmd):
	p = os.popen(cmd)
	result = p.readline().rstrip('\n')
	return result
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”

Who is online

Users browsing this forum: scotty101 and 16 guests