SolutionsPrivate Clouds: Cloudforms PaaS: OpenShift Public Clouds Cloud Portfolio Building Clouds Cloud Partner Program ResourcesMedium & Large Enterprise Enterprise Solutions Business Intelligence Collaboration & Content Management Enterprise Applications Security Web Applications
Tips from RHCEs
Featured Article: Time Calculation MagicRichard Keech, RHCE, Red Hat Instructor
There is often a need to do clever reckoning of dates and times in scripts based on things like:
- display tomorrow's date,
- display the date and time four hours ago,
- determine if today is the last day of the month.
Linux has the standard "date" utility which, despite being very flexible, cannot directly do these types of calculations. Two utilities that help are in the rkutils package at people.redhat.com/rkeech. They are "seconds2date" and "eom".
seconds2date. As the name suggests, this command serves to convert a value in seconds to a date. It serves as the inverse of the command "date +%s" which displays the current time as Unix-style epoch seconds (seconds since 1 Jan 1970). With seconds2date you can directly and reliably calculate the date and time at a specified interval before or after a known time. For example, to display the date yesterday you can do this sort of thing:
SECS_IN_DAY=86400 seconds2date -i $(( $(date +%s) - $SECS_IN_DAY ))
eom. Cron is Linux's standard way of scheduling events. However imagine you need to schedule a task to only run on the last day of the month. That type of scheduling logic is beyond cron's capability, even though scheduling for the first day of the month is trivial. eom is a utility which is useful only in its return code, like the standard Linux "test" command. To schedule a job on the last day of the month put a file like this in /etc/cron.daily/:
#!/bin/bash if eom then <insert command which is to be run at end of month> fi