My requirement is that I want to push ssh command to remote server, So that will check current and installed kernel version on the remote server and have to reboot.
I have tried “su --session-command” combination also.
ssh anand@remote_host "su -lc 'if [ "`rpm -q kernel | tail -1`" == "kernel-$(uname -r)" ]; then echo "System is already running with latest kernel version"; else echo "reboot"; fi'"
Its taking variables where I pushing the command but I should take variables from remote server.
First you create a script called checkversion.sh on your workstation:
#!/bin/bash
rv="$(rpm -q kernel)"
cv="kernel-$(uname -r)"
[ "$rv" == "$cv" ] && echo "The latest version of the Linux kernel running on this box." || echo "Reboot the box"
To actually reboot the box modify script as follows:
#!/bin/bash
rv="$(rpm -q kernel)"
cv="kernel-$(uname -r)"
[ "$rv" == "$cv" ] && echo "The latest version of the Linux kernel running on this box." || /sbin/reboot
A better solution is to use tool like Ansible that allows to reboot the box when the Linux kernel updated on fly.