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 ?
5 Answers
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.
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.
-
To be explicit on the safety issue: the password will be saved in history, which is not good.– silencejMar 11 at 4:15
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 &"
Try:
xterm -e "sudo -b nohup php -S localhost:80 -t /media/malcolm/Workspace/sites &>/dev/null"
when you close xterm, the php web server still alive,
Don't put "nohup" before "sudo" or else php web server will be killed after closing xterm.
I added the following to my .bash_profile file.
sudobg() {
sudo echo "Password entered"
sudo $@ &
}
Then I can simply call:
prompt$ sudobg echo "Hello from the background."
And I get:
Password:
Password entered
[1] 6773
prompt$ Hello from the background.
Disclaimer: I'm using bash but not Ubuntu. There may be better locations for this function definition.