9

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?

isedev
  • 18,848
  • 3
  • 60
  • 59
user2966143
  • 93
  • 1
  • 1
  • 4
  • possible duplicate of [pass password to su/sudo/ssh](http://stackoverflow.com/questions/233217/pass-password-to-su-sudo-ssh) – Sam Sep 29 '14 at 22:26
  • Did you log in your virtual machine as "root"? If so, you are running with root privilege, and be careful with that. – 4af2e9eb6 Sep 29 '14 at 22:26
  • 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)? – datenwolf May 10 '21 at 14:54

3 Answers3

19

You can run sudo bash -c 'synaptic &'

Thyrst'
  • 2,253
  • 2
  • 22
  • 27
11

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"
Anurag_BEHS
  • 1,390
  • 2
  • 22
  • 36
  • 1
    apparently you can just run the command after `sudo -b`, you don't need the inner su -c – joe_zeroh Nov 24 '21 at 16:04
  • @joe_zeroh 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. – linguisticturn Apr 12 '22 at 16:43
  • 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. – marlar Feb 09 '23 at 17:29
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 &).

isedev
  • 18,848
  • 3
  • 60
  • 59