sudo su user -c with arguments not working

2024/10/15 7:29:19

I am trying to execute command from python as another "user" with:

command = "sudo su user -c player --standard=1 -o 2"
subprocess.Popen(command.split(), shell=False, stdin=None, stdout=None, stderr=None, close_fds=True)   

but everything after -c causes troubles. Any idea how to execute this command with arguments for my program player?

When I create separate 2.py script with "player --standard=1 -o 2" (in subprocess of course) and call this script from my first script via "sudo su user -c /home/user/2.py", it works ok.

Answer

command.split() produces the list ['sudo', 'su', 'user', '-c', 'player', '--standard=1', '-o', '2'], which is not what you need. su expects the command to run as a single argument after -c.

The simplest thing to do is probably to just construct the list by hand, without using split():

command = ['sudo', 'su', 'user', '-c', 'player --standard=1 -o 2']

If you really want to start from a string, the shlex module can be helpful:

import shlex
command = shlex.split("sudo su user -c 'player --standard=1 -o 2'")
https://en.xdnf.cn/q/117857.html

Related Q&A

Grouping data on column value

Hi I have data (in excel and text file as well) like C1 C2 C31 p a1 q b2 r c2 s dAnd I want the output like:C1 C2 C31 p,q a,b2 r,s c,dHow can I group the data…

Memory Error Python Processing Large File Line by Line

I am trying to concatenate model output files, the model run was broken up in 5 and each output corresponds to one of those partial run, due to the way the software outputs to file it start relabelling…

python assign literal value of a dictionary to key of another dictionary

I am trying to form a web payload for a particular request body but unable to get it right. What I need is to pass my body data as below data={file-data:{"key1": "3","key2&quo…

python regex findall span

I wanna find all thing between <span class=""> and </span> p = re.compile(<span class=\"\">(.*?)\</span>, re.IGNORECASE) text = re.findall(p, z)for exampl…

Why cant I view updates to a label while making an HTTP request in Python

I have this code :def on_btn_login_clicked(self, widget):email = self.log_email.get_text()passw = self.log_pass.get_text()self.lbl_status.set_text("Connecting ...")params = urllib.urlencode({…

plotting multiple graph from a csv file and output to a single pdf/svg

I have some csv data in the following format.Ln Dr Tag Lab 0:01 0:02 0:03 0:04 0:05 0:06 0:07 0:08 0:09 L0 St vT 4R 0 0 0 0 0 0…

parallel python: just run function n times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

how to specify the partition for mapPartition in spark

What I would like to do is compute each list separately so for example if I have 5 list ([1,2,3,4,5,6],[2,3,4,5,6],[3,4,5,6],[4,5,6],[5,6]) and I would like to get the 5 lists without the 6 I would do …

Keeping just the hh:mm:ss from a time delta

I have a column of timedeltas which have the attributes listed here. I want the output in my pandas table to go from:1 day, 13:54:03.0456to:13:54:03How can I drop the date from this output?

How to return the index of numpy ndarray based on search?

I have a numpy 2D array, import numpy as np array1 = array([[ 1, 2, 1, 1],[ 2, 2, 2, 1],[ 1, 1, 1, 1],[1, 3, 1, 1],[1, 1, 1, 1]])I would like to find the element 3 and know its location. So,…