How to run a command asking password in background?

12,145

Solution 1

Try typing sudo -i first end then apt-get update & or just running the command:

sudo -i && apt-get update &

Something else you could try is the command:

gksudo && sudo apt-get update &

But I'm not sure if that'll work.

Or you could try adding your user to the list of users that aren't required to enter a password when using sudo.

To do this add type sudo visudo into the terminal. Then add the line

%super_sudoers ALL=(ALL) NOPASSWD: ALL

Or whatever you want to call the group (you can change "super_sudoers") and then add your user to that group by typing

usermod -a -G super_sudoers yourusername (obviously replacing yourusername with your username)

and reboot (or maybe log out/log in works, not sure) to find you can type suo commands without needing to enter a password. This, of course, means other people can so ensure you always leave your computer locked and don't have automatic login enabled.

Solution 2

First of All, "This is not the best practice".
But you can use the command sudo -S

Sudo -S:
The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.

So, Go to the terminal and type:

echo 'yourpassword' | sudo -S apt-get update 1>> /home/user/myaptupd.log 2>&1 &

it will run the command apt-get update without prompt for a password and backgroud,
and will redirect (append) the stdout and the stderr to a log file in your home named myaptupd.log, just in case you can check if everything is allright with the apt-get update command.

Do to this is a large command, you can put this command as an Alias in your .bashrc file. Eg: In the terminal type:

gedit /home/user/.bashrc

Add this line to the end of the file

alias myaptupd='echo 'yourpassword' | sudo -S apt-get update 1>> /home/user/myaptupd.log 2>&1 &'

Reload your .bashrc
In the terminal go to your home directory just type:

cd

Then type:

source .bashrc

or

. .bashrc

Now in the terminal just type the Alias as a command:

myaptupd

You can check with the command jobs the status.

On the other hand you can create a bash script and named myaptupd.sh
Eg:

#! /bin/bash
echo 'yourpassword' | sudo -S apt-get update 1>> /home/user/myaptupd.log 2>&1 &

For security you can set the perms to:

chmod 700 /path/to/the/myaptupd.sh

Then you can call the script with:

 bash /path/to/the/myaptupd.sh

Or make an Alias in your .bashrc

alias myaptupd='bash /path/to/the/myaptupd.sh'

Reload your .bashrc (like explain it before) and you can type the name of the alias to run the script:

myaptupd

Hope this will helpful!

Regards.

Solution 3

Best and secure way is:

su -c "sudo apt-get update &"

You can also pipe the output to a log file:

su -c "sudo apt-get update > update.log &"
Share:
12,145

Related videos on Youtube

bubble
Author by

bubble

Updated on September 18, 2022

Comments

  • bubble
    bubble over 1 year

    I want to run a command sudo apt-get update &. However after hitting enter, when i hit for jobs, it shows the status of these commands as stopped. My guess is that by pushing the process into background I did not provide it an opportunity to ask password. Is there a way to run a process in background which requires a password ?

  • bubble
    bubble over 11 years
    You mean both in separate lines .. ?
  • bubble
    bubble over 11 years
    That will mean logging as a root first.. But I would like to avoid it
  • bubble
    bubble over 11 years
    your solutions a very risky, I believe