Running a program in the background as sudo
Asked Answered
P

4

10

I am on a linux virtual machine and I'm trying to run the command sudo synaptic & which should start synaptic in the background. However, it doesn't ask for the password and the program doesn't seem to start. I have not typed my password earlier, as running any other command withouth the & at the end ask for my password. What is the problem?

Photomontage answered 29/9, 2014 at 22:23 Comment(3)
possible duplicate of pass password to su/sudo/sshMuticous
Did you log in your virtual machine as "root"? If so, you are running with root privilege, and be careful with that.Gifford
Trying to background something – anything – with sudo immediately raises a couple of red flags. What's the actual problem you're trying to solve? Did you try running it as a service managed by your supervisor suite (systemd, runit, OpenRC or whatever else your system is using)?Caresse
E
19

You can run sudo bash -c 'synaptic &'

Exaggerate answered 12/6, 2017 at 12:14 Comment(0)
T
15

sudo --help says about option -b

-b, --background              run command in the background

This worked for me

sudo -b su -c "command what you want to run"
Twelvemo answered 10/5, 2021 at 14:47 Comment(3)
apparently you can just run the command after sudo -b, you don't need the inner su -cVermiculite
@Vermiculite You do need the inner su -c. Here is an example with trying to open gedit. Open a new terminal and execute sudo -b gedit &. All you will see is e.g. [1] 9258 in the terminal; there will be no prompt for password and the gedit window will not open. Now open another terminal and execute sudo -b "gedit &". This time you will be prompted for password, but after that, you will get the error sudo: gedit &: command not found. Finally, execute sudo -b su -c "gedit &". Now everything works as it should.Emanative
For me it works without the inner su. I just tried with sudo -b xed /etc/hosts. The password prompt comes in the terminal before xed is started and set to background. BTW, this is the best answer IMO.Phosphorism
P
5

The problem is that the sudo command itself is being run in the background. As a result, it will be stopped (SIGSTOP) when it tries to access the standard input to read the password.

A simple solution is to create a shell script to run synaptic & and then sudo the script in the foreground (i.e. without &).

Primogenitor answered 29/9, 2014 at 22:29 Comment(1)
Could you include a link to describe 'synaptic &'Rorrys
P
0

From the previous answer, only adding -b to a command starting with sudo might work, as below:

sudo -b COMMAND

Worked for the command below:

sudo -b mongod --config /etc/mongod.conf

Note. For a non-sudo command, there's nohup, as below:

nohup COMMAND &

Adding nohup before the command, and an & after the command might work for a non-sudo command.

Perfectionism answered 29/2, 2024 at 4:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.