So, What's the big deal anyway?
When I encountered this situation in a real-life scenario I could not imagine that I would invest so much time in comparing dates. after all comparing dates in bash has been documented a thousand times over the web.
The solution is quite obvious if you have two date strings and would like to compare them. If you have two unix epoch on your hand the solution is evident as well.
But, what happens when you have on one hand an epoch and on the other, an arbitrary date time and you would like to fully compare them. well, that's less documented. so after hours of trying out various solutions and failing this is what worked for me.
Code sample:
# Assume we have an epoch named "first_epoch"
# get the process uptime as a date formatted string
process_uptime_tmp=$(systemctl status <your_process> | grep ago | awk '{print $6" "$7}')
# convert the date into a comparable epoch
process_start_epoch=$(echo $(date -d"$process_uptime_tmp" +%s))
# compare epochs
if [ "$first_epoch" -gt "$process_start_epoch" ]
then
echo "do something"
else
echo "do something else"
fi
Hope this helps anyone out there looking for a solution.
Cheers,
No comments:
Post a Comment