6

I've been trying to create a script that runs sudo apt-get update and sudo apt-get autoremove whenever I log in. It goes like this:

#!/bin/bash
(sudo apt-get update)
(sudo apt-get autoremove)

But whenever I run the script it freezes my computer. I'm assuming it's because it's waiting for the script to supply a password. Would it work if I added PASSWD:mypassword after the commands like this:

#!/bin/bash
(sudo apt-get update)PASSWD:mypassword
(sudo apt-get autoremove)PASSWD:mypassword

or is there something else I should do? Is there even a way? If so please tell me!

kos
  • 35,891
  • 3
    Unattended Upgrades can do regular runs of both for you. Do you need to write your half-baked solution for this? – muru Apr 21 '15 at 17:52
  • What is Unattended Upgrades? – It's Willem Apr 21 '15 at 17:54
  • See https://help.ubuntu.com/lts/serverguide/automatic-updates.html. One of its configuration options (in /etc/apt/apt.conf.d/50unattended-upgrades) is to allow automatic runs of autoremove. – muru Apr 21 '15 at 18:01
  • 3
    Don't run your commands in subshells: remove the parentheses – glenn jackman Apr 21 '15 at 18:01
  • 2
    Alternately, give yourself privilege to run sudo apt-get without a password. Create a file named /etc/sudoers.d/willem containing willem ALL = NOPASSWD: /usr/bin/apt-get -- assuming "willem" is your username – glenn jackman Apr 21 '15 at 18:03
  • @glennjackman You actually guessed my username right... anyhow I will try that. Thanks. – It's Willem Apr 22 '15 at 21:24
  • But what if I don't have permission? I'm an admin but... – It's Willem Apr 22 '15 at 21:26
  • You will do sudo visudo -f /etc/sudoers.d/willem to create/edit that file – glenn jackman Apr 22 '15 at 21:56
  • Would it be doing it right to run 'sudo visudo -f /etc/sudoers.d/willem' and then type in 'willem ALL = NOPASSWD: /usr/bin/apt-get', press CTRL+S and close the terminal? – It's Willem Apr 23 '15 at 01:46
  • 1
    How you run the script? And remove the parentheses. – A.B. Apr 23 '15 at 07:43
  • I have it configured so that I click on it and it runs – It's Willem Apr 23 '15 at 17:28
  • Ok, so I put a file in /etc/sudoers.d/ that says "willem ALL = NOPASSWD: /usr/bin/apt-get" but then when I type a sudo command it returns "sudo: /etc/sudoers.d/willem is mode 0664, should be 0440". What do I do about this? – It's Willem May 08 '15 at 17:24

3 Answers3

3

You don't actually have to include your password in your script (from what I have seen its generally not advisable to have your password in a script). Instead you can edit your sudoer file to allow you to run the apt-get command without the need for a password.

For more information go to this website.

open a terminal (ctrl + alt + T)

Enter the command

sudo visudo

At the end of the sudoers file type

username ALL=(ALL) NOPASSWD: /usr/bin/apt-get

Where username is your username (obviously)

editing this file will allow you to run any of the apt-get commands without entering a password. This means you can also install or upgrade packages without entering your password.

You can also add other comands to this list by entering the path to the command after a comma. The path to a command can be found by entering the following command in a terminal.

which command

Where command is the name of the command( for example reboot).

Rumesh
  • 1,439
  • Another solution here would be make the script with apt-get update & autoremove commands, and save it so only root could modify it, then link that in the sudoers file. Very similar thing here: http://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password – Wilf Apr 24 '15 at 11:09
  • Thanks for the info I didn't really know about that method though it seems a little complex – Rumesh Apr 24 '15 at 11:15
0

The command to start a bash sccript :

#!/bin/bash

The command to ask for the users root password :

[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"

Example commands of what can be next :

apt-get update

apt-get upgrade --yes

shutdown -r now

Good luck with your script

-1

Try this script:

#!/bin/bash
echo "yourpassword" | sudo -S apt-get update
echo "yourpassword" | sudo -S apt-get autoremove

Here, you echo your password and the read the password using -S option (stdin option for sudo).

Rajesh N
  • 144
  • 5