Rpm queryformat and boot


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rpm queryformat and boot
# 1  
Old 11-12-2015
Rpm queryformat and boot

Hello,

I have a script in which I am doing a queryformat and if a java rpm has been installed within 10 minutes, I would like the server to reboot. If a java rpm has not been installed then do not reboot. I can get it for glibc but for java I am having an issue... any help would be appreciated.
Hopefully I got the code tags right...

Code:
#!/bin/bashMAX_Time=$(bc <<< '10*60')
 RPMage=$(($date +%s) - $rpm -qa --queryformat "%{name}-%{INSTALLTIME:date}\n"} |grep java
if [[ ${RPMage} -lt ${MAX_Time} ]]; then
echo "Rebooting server since package was installed within last 10 Minutes"
/sbin/shutdown -r +2 &
else
echo "NOT Rebooting server since package was installed over 10 Minutes ago"
fi

Thanks

gartie
# 2  
Old 11-12-2015
Quote:
Originally Posted by gartie
Hello,

I have a script in which I am doing a queryformat and if a java rpm has been installed within 10 minutes, I would like the server to reboot. If a java rpm has not been installed then do not reboot. I can get it for glibc but for java I am having an issue... any help would be appreciated.
Hopefully I got the code tags right...

Code:
#!/bin/bashMAX_Time=$(bc <<< '10*60')
 RPMage=$(($date +%s) - $rpm -qa --queryformat "%{name}-%{INSTALLTIME:date}\n"} |grep java
if [[ ${RPMage} -lt ${MAX_Time} ]]; then
echo "Rebooting server since package was installed within last 10 Minutes"
/sbin/shutdown -r +2 &
else
echo "NOT Rebooting server since package was installed over 10 Minutes ago"
fi

Thanks

gartie
You got the CODE tags right. Thank you.
But the code has several syntax and logic errors:
  1. running lines together where a <newline> is required,
  2. missing closing parentheses,
  3. piping the output of a variable assignment (which produces no output) into a grep command,
  4. and using the values of variables that have not been assigned values ($date and $rpm).

I don't use rpm, and none of this has been tested, but it should get rid of most of the bash syntax problems and might come close to what you're trying to do:
Code:
#!/bin/bash
MAX_Time=$((10*60))
RPMage=$(($(date +%s) - $(rpm -qa --queryformat "%{name}-%{INSTALLTIME:date}\n"} | grep java)))
if [[ ${RPMage} -lt ${MAX_Time} ]]
then	echo "Rebooting server since package was installed within last 10 Minutes"
	/sbin/shutdown -r +2 &
else
	echo "NOT Rebooting server since package was installed over 10 Minutes ago"
fi

# 3  
Old 11-12-2015
server rebooted.

Hi Don,

It didn't install but still rebooted the server...
I cant get past that piece of logic where it looks at the time and if it greater than 10 minutes it does not reboot, if less then it does reboot.
thanks...
# 4  
Old 11-12-2015
Show us the output from the command:
Code:
rpm -qa --queryformat "%{name}-%{INSTALLTIME:date}\n"

I'm guessing that the output produced is not a number, so the subtraction from the current time isn't going to work correctly.

We need to know what the output will be in a case where java is installed and in the case where java has not been installed. (I would assume that there just won't be a line with java in the output if it is not installed. Is this a correct assumption? If so, and if java has not been installed, you'll get a syntax error in the arithmetic substitution.)

Update: If multiple versions of java have been installed, could you end up with multiple lines coming out of the grep?

Last edited by Don Cragun; 11-12-2015 at 02:54 PM.. Reason: Add another question.
# 5  
Old 11-13-2015
Quote:
Originally Posted by gartie
Hi Don,

It didn't install but still rebooted the server...
I cant get past that piece of logic where it looks at the time and if it greater than 10 minutes it does not reboot, if less then it does reboot.
thanks...
Unfortunately you are under the impression that rpm -qa --queryformat "%{name}-%{INSTALLTIME:date}\n"} |grep java will produce a number in seconds that can be subtracted from the current now time in second, but it is not so.
I do not have java installed but I will demonstrate with a package I have installed:
Code:
rpm -q --queryformat "%{INSTALLTIME:date}\n" tmux
Sat 07 Nov 2015 10:57:45 AM MST

This string needs to be converted into seconds before it can be used to subtract from now.

Perhaps something like:
Code:
install_time=$(date -d "$(rpm -q --queryformat %{INSTALLTIME:date} tmux)" "+%s")

Code:
$ echo $install_time
1446919065

Notice that I am querying the wanted package directly (in your case Java), since I know what I want, instead of querying all installed packages and then filtering via grep.

Then you can subtract, assuming that $install_time got a good input:
Code:
RPMage=$(($(date "+%s") - $install_time))

In my opinion there's no advantage of creating a calculation with MAX_Time=$(bc <<< '10*60') or the correct alternative MAX_Time=$((10*60)) when I know that 10 minutes is 600 seconds.
Code:
MAX_Time=600 #10 minutes

I can even add a comment along side to advertise its meaning and remove the magic out of it.

Last edited by Aia; 11-13-2015 at 12:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Rpm issue with autoconf-2.63-1.AIX6.1.noarch.rpm

dears i am trying to install the autoconf-2.63-1.aix6.1.noarch.rpm but its shows the below error message . P700_dev/svn/dependencies>rpm -i autoconf-2.63-1.aix6.1.noarch.rpm error: failed dependencies: m4 is needed by autoconf-2.63-1 P700_dev/svn/dependencies> (6 Replies)
Discussion started by: thecobra151
6 Replies

2. Boot Loaders

Reboot and Select Proper Boot device or insert Boot media in select Boot device and press a key

Hello, I have kubuntu on my laptop and now I decided to switch to Windows 7. I made the bios settings properly (first choice is boot from cd\vd) but I see the error " reboot and select proper Boot device or insert Boot media in select Boot device and press a key " I have tried CD and... (0 Replies)
Discussion started by: rpf
0 Replies

3. Shell Programming and Scripting

How to make RPM not write to RPM database if RPM fails to deploy?

How to make RPM not write to RPM database if RPM fails to deploy? IE I create an rpm spec file that contains the following if then exit 1 fi My rpm will fail at deployment, but if I do rpm -qa , I can see the rpm in the rpm db (3 Replies)
Discussion started by: 3junior
3 Replies

4. Shell Programming and Scripting

Extract RPM name from path to .rpm file

It's the end of the day and I just can't get my head around this. I'm trying to extract just the name of the RPM from the path to a .rpm file. So from: /home/me/rpm/RPMS/i386/nagios-our-plugins-1.2-6.i386.rpmI need to extract 'nagios-our-plugins'. I can't get the awk syntax right: awk '{... (5 Replies)
Discussion started by: aussieos
5 Replies

5. SuSE

How to install .src.rpm ? ( source rpm )

Hi, I have got few RPM's from rpmfind.net ( mainly gcc ). But it seems to be src files instead of the image. so I think we have to build the src files according to target machine using rpmbuild. Can any one help me with 1) Various options of rpm build that have to be taken care 2)... (3 Replies)
Discussion started by: Sivaswami
3 Replies

6. Linux

Converting source rpm to binary rpm

Hi, I need to install an rpm file,but I only have '.src.rpm' version of it. I want to convert it to a binary rpm. so I tried this rpm --rebuild somethin.src.rpm But I am gettin '--rebuild:Invalid option' as output Is there another way to rebuild source rpm.? Thanks in... (2 Replies)
Discussion started by: eamani_sun
2 Replies

7. Solaris

solaris boot problem boot error loading interpreter(misc/krtld)

When I installed the SOLARIS 10 OS first time, the desktop would not start up, this was because of network setup. Reinstalled worked. After a week due to some problem I had to reinstall OS, installation went fine and but when i reboot I get this error. cannot find mis/krtld boot error loading... (0 Replies)
Discussion started by: johncy_j
0 Replies

8. UNIX for Advanced & Expert Users

Cannot boot - Boot : Panic : File size out of range (EWS-UX/V unix)

Hey ! I am running EWS-UX/V (Rel 4.2) on NEC EWS/4800/330 station and I am having problems rebooting my station : I am getting the following message on display : BOOT : PANIC : File size out of range. According to user guide, this error is occuring when a file exceeding the limit and/or... (2 Replies)
Discussion started by: fredo
2 Replies

9. Linux

Failed dependencies of rpm / how does RPM check for ?

Hey, I've a problem installing a package. rpm -ivh brings the errors: libjvm.so is needed by libverify.so is needed by But I have installed the Java SDK like requested and the files are on the disk. But I have no idea how to find out, why rpm cann't find them. I have also... (2 Replies)
Discussion started by: mod
2 Replies

10. Shell Programming and Scripting

how to use RPM to biuld *.rpm

I have an application for linux writen by me. and before use it, I have to install it. I 've writen a shell program to install it. Now how can I turn it to the *.rpm ? Is this can done by shell command rpm? How to do it? thanks (1 Reply)
Discussion started by: murataht
1 Replies
Login or Register to Ask a Question