简体   繁体   中英

Calculating time in seconds using bash

I have cron job which processes data every 15 minutes(12:00, 12:15, etc...) I need a bash function/script which determines how many seconds until the next processing cycle relative to the current time. If current time = "15:09:00 2016" the next processing cycle would be 360 sec. Any ideas? thanks.

Get the current time in seconds since the UNIX epoch

$ now=$(date +%s)

then compute that value mod 900 (900 seconds is 15 minutes) and subtract that from 900.

$ echo $((900 - now % 900))

The date command allows a date to be provided following the -d --date option. date also understands relative dates (eg + 6 min , +3 days , etc..). So if you need to know what 6 minutes in the future is you can simply use date -d "+ 6 min" to find the exact time that will be. eg

$ date
Fri Jun 10 15:22:45 CDT 2016

$ date -d "+ 6 min"
Fri Jun 10 15:28:47 CDT 2016

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM