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...

#!/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:

#!/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

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...

Show us the output from the command:

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 ?

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:

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:

install_time=$(date -d "$(rpm -q --queryformat %{INSTALLTIME:date} tmux)" "+%s")
$ 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:

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.

MAX_Time=600 #10 minutes

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