Bash script to install packages and report to log file
First off, you mustn't use sudo in a bash script, it's non-interactive and so if you're required to use a password you won't be able to elevate.
Below would be a basic example to install several packages, to install more you would add a package to the "for package in" line. The output is set to append to the existing file.
#!/bin/bash
apt-get update
for package in nano httpd vim mysql-server
do
apt-get install -qq --print-uris $package >> script.log 2>>script_error.log
done
You should save this file however you like, then you need to set the permissions to allow execution of the script. If you called the file script.sh you would run:
chmod +x script.sh
Then to run the script you would do:
sudo ./script.sh
This is a very basic example, it doesn't log as well as is possible and there are many more complex and better ways to accomplish it, but it will do the job if you're happy to look through the log post-installation. The first log contains minimal logging on normal installation (even if you're not looking at it you may as well keep it for checking) and the second log file contains only the errors from apt-get.
Related videos on Youtube
Prop
Updated on September 18, 2022Comments
-
Prop over 1 year
I want to be able to log result of running bash script to file. Script itself is pretty simple, its used to install basic packages on fresh install machines and it scheme looks like this
sudo apt-get install -y nano
The thing im trying to achieve is the bash script to report whether installation went smoothly or in case it didnt success - to point what went wrong. So I would like it to create file like "log" which would very briefly report status, for example
Package "nano" successfully installed!
or
Package "nano" failed to install. // Here comes the line of apt-get install that caused fail
Thanks in advance!
-
Admin almost 10 yearsWhat do you have so far?
-
Prop almost 10 yearsTo be honest nothing. Came up with this idea this morning, did some googling around but couldnt find an answer. I could use a tee like command but I dont want all the output of apt-get install to be logged. I just want a summary.
-
Prop almost 10 years@DennisNolte Could you please write a simple example ?
-
mcdave almost 10 years-1 this is extremely basic shell programming, and you're asking an extremely specific question without having tried anything yet. If you need to know about redirection, please read
man bash
and these links.
-
-
Dennis Nolte almost 10 yearswont work exactly, sadly "is already installed" f.e. is not stderr, so instead one could tail only the last few lines.
-
Alex Berry almost 10 yearsTrue, although that's not necessarily a bad thing, if the package is already installed then the script has done it's job. I agree this could be done a lot better but this script is sufficient for bulk install of a few packages.
-
Alex Berry almost 10 yearsApologies, that was my typo, it should have been --print-uris. It just adds extra info to script.log for further diagnosis. Either copy the script above again (I've corrected the error, I also moved apt-get update out of the loop so it doesn't run for each package, saves a little time and bandwidth) or replace with with i.