LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2017, 11:09 PM   #1
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Rep: Reputation: 0
Simple script needed to detect OS


Really simple script needed to so the following.

We run both CentOS and Ubuntu here and I need to put together a single shell script with some simple logic to tell the OS's apart and run the appropriate patching/updating commands once that's done and then a reboot. The whole thing will get fired off by crontab at a given date and time for each system.

I've got a few ideas to tell each apart:
For CentOS I found this:

more /etc/system-release
CentOS release 6.9 (Final)

and for Ubuntu I found
uname -a
Linux cnxatlsgi01 4.4.0-75-generic #96-Ubuntu SMP Thu Apr 20 09:56:33 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux


And the update script without the logic is:

#!/bin/bash
#
#
#
#CentOS
yum -y update
#
#
#
#Ubuntu
apt-get update
sleep 3
apt-get -y dist-upgrade
sleep 3
apt-get -y autoremove
#
#
#
reboot

but I'm not sure of the best way to do the OS selection or if there is totally better way to go about this. We can't do any automated updating due to processes running the background (fuse) that really hate stuff messing with them when up.

Any help as always appreciated.

George
 
Old 05-28-2017, 12:22 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,389
Blog Entries: 3

Rep: Reputation: 1051Reputation: 1051Reputation: 1051Reputation: 1051Reputation: 1051Reputation: 1051Reputation: 1051Reputation: 1051
In Ubuntu, you have a short utility to get the release information:

Code:
lsb_release -rd
That can be processed with sed or awk to extract what you need to make the decision.

See also about using command substituion in your script.
 
2 members found this post helpful.
Old 05-28-2017, 03:27 AM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142
uname -a
 
Old 05-28-2017, 04:25 AM   #4
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 5.11
Posts: 335

Rep: Reputation: 122Reputation: 122
Why?
On the centos server, set up your cron to execute yum

On the Ubuntu, set up your cron to execute apt-get

AFAIK, a reboot is not necessary following an update on CentOS...but I could be wrong about that.
 
1 members found this post helpful.
Old 05-29-2017, 01:56 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 6,254
Blog Entries: 2

Rep: Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515Reputation: 1515
i also think the approach is wrong.

but you have to look at /etc/*release* in each distro.
also packages like lsb_release might exist for other distros.
also a web search...
 
2 members found this post helpful.
Old 05-29-2017, 03:44 AM   #6
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,698

Rep: Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291Reputation: 3291
As post #5, @ondoho
Code:
$ cat /etc/*release*
 
1 members found this post helpful.
Old 05-29-2017, 05:57 AM   #7
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 376

Rep: Reputation: 69
Quote:
Originally Posted by scasey View Post
Why?
On the centos server, set up your cron to execute yum

On the Ubuntu, set up your cron to execute apt-get

AFAIK, a reboot is not necessary following an update on CentOS...but I could be wrong about that.
Normally you have to reboot if a new kernel had been installed for that new kernel to be the active one.
 
Old 05-30-2017, 11:45 AM   #8
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by scasey View Post
Why?
On the centos server, set up your cron to execute yum

On the Ubuntu, set up your cron to execute apt-get

AFAIK, a reboot is not necessary following an update on CentOS...but I could be wrong about that.
WE have A mix of different types of servers and want one script we can push from a central source to all of then to run at the same time via a cron command where the script will decide which update command to run....now the dirty way to do it just to let it fall through and fail on the systems that don't have the other OS update utilities but I'm trying to not do that. We need and require reboots after every update run here in case a kernel is updated.
 
Old 05-30-2017, 12:02 PM   #9
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Original Poster
Rep: Reputation: 0
I must have been half asleep when I wrote the original request, or the notebooks screen format confused me. The utilities are fine for OS selection:

more /etc/system-release
CentOS release 6.9 (Final)

uname -a
Linux cnxatlsgi01 4.4.0-75-generic #96-Ubuntu SMP Thu Apr 20 09:56:33 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

What I need help with since I know no sed/awk is the selection and if/then portion to decide which group of commands to run based on the given OS type and then end with a reboot and if neither if found exit with out a reboot.
 
Old 05-30-2017, 12:14 PM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,589

Rep: Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817
if you want to know if the current os is either centos or ubuntu you need only to grep centos in /etc/some-file (or similar).
 
Old 05-30-2017, 02:34 PM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,245

Rep: Reputation: 983Reputation: 983Reputation: 983Reputation: 983Reputation: 983Reputation: 983Reputation: 983Reputation: 983
Or simply check for some string in some file.
 
Old 05-31-2017, 06:52 AM   #12
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Original Poster
Rep: Reputation: 0
Angry

Quote:
Originally Posted by pan64 View Post
if you want to know if the current os is either centos or ubuntu you need only to grep centos in /etc/some-file (or similar).
That's very cute. Yes, I know that and showed it in the post above. What I needed help with is the logic for the if/then and awk etc.
 
Old 05-31-2017, 06:53 AM   #13
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Original Poster
Rep: Reputation: 0
Angry

Quote:
Originally Posted by NevemTeve View Post
Or simply check for some string in some file.
That's very cute. Yes, I know that and showed it in the post above. What I needed help with is the logic for the if/then and awk etc.
 
Old 05-31-2017, 07:00 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,589

Rep: Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817Reputation: 2817
https://linuxacademy.com/blog/linux/...if-statements/ ?
http://bencane.com/2014/01/27/8-exam...t-you-started/
 
1 members found this post helpful.
Old 05-31-2017, 07:11 AM   #15
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 94

Original Poster
Rep: Reputation: 0
Pan Thank you! looked all over for this!!

I think the first will do what I need to get this done, need to look at it later, the second does not resolve, 404 error - sorry, looks like its incomplete....ahhh...you went and fixed while I was typing thank you second one there is exactly what I need.

Last edited by gkasica; 05-31-2017 at 07:16 AM. Reason: typo
 
  


Reply

Tags
shell script, update


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help needed for simple IP reset script Joeblah Linux - Networking 5 07-16-2012 11:27 AM
Help needed with simple bash script clifford227 Linux - Newbie 4 10-03-2011 10:46 AM
simple shell script help needed vbsaltydog Linux - Networking 10 05-02-2006 01:47 AM
simple script needed drum2jc Linux - Software 1 01-05-2006 12:28 AM
simple backup script needed ferret_dude Programming 4 05-24-2004 08:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration