How to run a command asking password in background?
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 &"
Related videos on Youtube
bubble
Updated on September 18, 2022Comments
-
bubble over 1 year
I want to run a command
sudo apt-get update &
. However after hitting enter, when i hit forjobs
, 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 over 11 yearsYou mean both in separate lines .. ?
-
bubble over 11 yearsThat will mean logging as a root first.. But I would like to avoid it
-
bubble over 11 yearsyour solutions a very risky, I believe