Bash script that login root and execute commands [SOLVED]

Need help with C, C++, perl, python, etc?

Bash script that login root and execute commands [SOLVED]

Postby veleno » 2020-08-13 10:30

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.
veleno
 
Posts: 3
Joined: 2020-08-13 09:01

Re: Bash script that login root and execute commands

Postby Bloom » 2020-08-13 10:41

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.
User avatar
Bloom
 
Posts: 297
Joined: 2017-11-11 12:23

Re: Bash script that login root and execute commands

Postby arochester » 2020-08-13 10:45

Have to installed sudo and added yourself to the sudo list?
arochester
 
Posts: 1741
Joined: 2010-12-07 19:55

Re: Bash script that login root and execute commands

Postby veleno » 2020-08-13 11:12

@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
veleno
 
Posts: 3
Joined: 2020-08-13 09:01

Re: Bash script that login root and execute commands

Postby pylkko » 2020-08-13 12:25

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
pylkko
 
Posts: 1791
Joined: 2014-11-06 19:02

Re: Bash script that login root and execute commands

Postby Bloom » 2020-08-13 14:02

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'
User avatar
Bloom
 
Posts: 297
Joined: 2017-11-11 12:23

Re: Bash script that login root and execute

Postby veleno » 2020-08-13 14:38

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
veleno
 
Posts: 3
Joined: 2020-08-13 09:01


Return to Programming

Who is online

Users browsing this forum: No registered users and 8 guests

fashionable