I have a command to get date of creation of virtual server, get information from line
Created: Mon Mar 28 07:19:50 UTC 2022 (3 weeks ago)
:
root@hcloud-admin:/home/scripts# hcloud server describe Miguel-Zabbix-hel1-dc2 | awk 'NR == 4' | awk -F " " '{ print $2, $3, $4 }'
Output:
Mon Mar 28
And also have date from my mine server host with hcloud
CLI:
root@hcloud-admin:/home/scripts# date "%a %b %d"
Fri Apr 22
Could you please help, Is this possible to compare this both of date using bash script
, that to find out if it's been 10 days or not, after server was created?
If 10 days is passed, I want to use command hcloud server poweroof Miguel-Zabbix-hel1-dc2
in bash script.
Full output:
root@hcloud-admin:/home/scripts# hcloud server describe Miguel-Zabbix-hel1-dc2
ID: 191220000
Name: Miguel-Zabbix-hel1-dc2
Status: running
Created: Mon Mar 28 07:19:50 UTC 2022 (3 weeks ago)
Server Type: cpx11 (ID: 22)
ID: 22
Name: cpx11
Description: CPX 11
Cores: 2
Memory: 2 GB
Disk: 40 GB
Storage Type: local
Public Net:
IPv4:
IP: 11.111.111.111
Blocked: no
DNS: static.111.111.111.111.clients.your-server.de
IPv6:
IP: 0a99:4f9:p099:0d0b::/555
Blocked: no
Floating IPs:
No Floating IPs
Private Net:
No Private Networks
Volumes:
No Volumes
Image:
ID: 15512600
Type: system
Status: available
Name: ubuntu-20.04
Description: Ubuntu 20.04
Image size: -
Disk size: 5 GB
Created: Thu Apr 23 17:55:14 UTC 2020 (2 years ago)
OS flavor: ubuntu
OS version: 20.04
Rapid deploy: yes
Datacenter:
ID: 3
Name: hel1-dc2
Description: Helsinki 1 DC 2
Location:
Name: hel1
Description: Helsinki DC Park 1
Country: FI
City: Helsinki
Latitude: 60.169855
Longitude: 24.938379
Traffic:
Outgoing: 208 MB
Ingoing: 280 MB
Included: 22 TB
Backup Window: Backups disabled
Rescue System: disabled
ISO:
No ISO attached
Protection:
Delete: no
Rebuild: no
Labels:
Name: Zabbix
CodePudding user response:
Yes. This is possible. The trick is to convert both date/time strings into "the number of seconds since the epoch" format (date %s) and then compare the result. On modern versions of Linux, the 'date' command can interpret just about any reasonably formatted date string and convert it to various formats:
date_1=$(hcloud server describe Miguel-Zabbix-hel1-dc2 | sed -n 's/^Created:\([^(]*\).*$/\1/p')
seconds_1=$(date -s "${date_1}" %s)
seconds_2=$(date %s)
day_diff=$(((seconds_2 - seconds_1)/86400)) # Number of seconds in a day = 86,400.
Note that the $((...)) construct only does integer arithmetic, and it rounds down, so you may want to adjust the arithmetic to suit your exact needs. Also, this simple calculation does not take account of any of the date calculation nuances, like daylight savings, or leap-seconds, etc. But for your situation, I doubt these are very important.
Also, note that I used sed
instead of awk
. sed
is more concise for this type of job, but can be a bit complex, so I'll break down the -n 's/^Created:\([^(]*\).*$/\1/p'
parameters for you:
-n
: Do not print each line by default.s/pat/rep/p
: Ifpat
is found in the current line, replace withrep
and print.
In our case, pat
is:
^Created:
: Beginning of line (^), followed by the exact string 'Created:',\(
: start remembering what follows,[^(]*
: any character ([...]) that is not (^) in the string '(', repeated 0 or more times (*),\)
: stop remembering,.*$
: any single character (.) repeated 0 or more times, followed by the end of the line ($).
And rep
is
\1
: The first thing remembered (i.e. everything afterCreated:
and before(
).