Bash script that login root and execute commands [SOLVED]

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
veleno
Posts: 3
Joined: 2020-08-13 09:01

Bash script that login root and execute commands [SOLVED]

#1 Post by veleno »

Hi Folks!! :mrgreen: New here but old in Linux, first of all, as by rules, I searched for my question without find something similar so I'm opening this new thread hoping in some suggestion/help:
I'd like to create a bash script that I launch as normal user, ask me root password and if it's correct execute: apt update && apt upgrade && apt autoremove.
I tried different ways but unsuccesfully!
Ex I tried:
$ gedit update.bash

Code: Select all

#!bin/bash
sudo root
apt update && apt upgrade && apt autoremove
# chmod +x update.bash

or I also tried:

Code: Select all

#!bin/bash
su - && update && apt upgrade && apt autoremove
exit
Please, any suggestion/help?
Thank you so much!! :D
Last edited by veleno on 2020-08-13 14:39, edited 1 time in total.

User avatar
Bloom
Posts: 365
Joined: 2017-11-11 12:23

Re: Bash script that login root and execute commands

#2 Post by Bloom »

Code: Select all

#!/bin/bash
sudo apt update && sudo apt upgrade && sudo apt autoremove
This will ask your password once and do the two others without asking it again: Debian caches the sudo password during 5 minutes so you can do other sudo tasks without having to type the password over and over again.

arochester
Global Moderator
Global Moderator
Posts: 2016
Joined: 2010-12-07 19:55
Has thanked: 1 time
Been thanked: 2 times

Re: Bash script that login root and execute commands

#3 Post by arochester »

Have to installed sudo and added yourself to the sudo list?

veleno
Posts: 3
Joined: 2020-08-13 09:01

Re: Bash script that login root and execute commands

#4 Post by veleno »

@Bloom: thank you for your answer!! I did it but it asks me my pw instead of root pw!! I don't have and I don't want my user in sudo list (or I must have it?)

@arochester: thank you for your answer!! is it mandatory to do that if I want to do the script? I'd like to leave my user outside sudoers

User avatar
pylkko
Posts: 1801
Joined: 2014-11-06 19:02

Re: Bash script that login root and execute commands

#5 Post by pylkko »

there is also a package called "unattended-upgrades" for, well unattended upgrades'. This is not an answer to exactly what you ask, but you can run commands as other users. So you can set up a systemd timer to automatically run commands as root (no sudo needed)

https://wiki.debian.org/UnattendedUpgrades

User avatar
Bloom
Posts: 365
Joined: 2017-11-11 12:23

Re: Bash script that login root and execute commands

#6 Post by Bloom »

veleno wrote:@Bloom: thank you for your answer!! I did it but it asks me my pw instead of root pw!! I don't have and I don't want my user in sudo list (or I must have it?)
Then use this:

Code: Select all

su -c 'apt update && apt upgrade && apt autoremove'

veleno
Posts: 3
Joined: 2020-08-13 09:01

Re: Bash script that login root and execute

#7 Post by veleno »

Bloom wrote:
veleno wrote:@Bloom: thank you for your answer!! I did it but it asks me my pw instead of root pw!! I don't have and I don't want my user in sudo list (or I must have it?)
Then use this:

Code: Select all

su -c 'apt update && apt upgrade && apt autoremove'
Super Bloom!!! Top top top!!! Thank you so much!! :D :D :D

Post Reply