Calculating the time until the start of next hour in Bash - linux

Hi I have a bash script called by a systemd service that needs to run until the start of the next hour. Currently I have been using:
currentTime=$(date +"%s")
nextHour=$(date -d "$(date -d 'next hour' '+%H:00:00')" '+%s')
duration=$(((nextHour-currentTime)*1000))
Which works except for trying to calculate the difference between 11pm and midnight were as far as I can tell it gets the current days midnight from 23 hours previous.
Oct 13 23:00:05 host bash[2019]: 1665698405
Oct 13 23:00:05 host bash[2019]: 1665615600
Oct 13 23:00:05 host bash[2019]: -82805000
I figure I could put a conditional check with a different calculation if needed or perhaps look at a systemd timer for triggering the service but as the service needs to always activate on boot/reboot as well as running hour to hour this setup seemed more appropriate.
Would appreciate any advice on why this is happening and advice on most streamlined steps to avoid it.

This isn't really a question about bash, but seems to be more about date. Given that date has multiple different implementations, it seems wiser to choose a different tool to do the calculation. I suspect perl is more standardized and (almost) as available as date, so you might try to get the difference with:
perl -MTime::Seconds -MTime::Piece -E '
my $t = localtime; my $m = ($t + ONE_HOUR)->truncate(to => "hour"); say $m - $t'

Related

How to get the time when I last used the computer

In GNU/Linux on xorg session, what I want to do is to get how many seconds have passed since I stopped working with the computer (i.e. no keys pressed and/or cursor moved).
Running in the background, the script below will display secs in the status bar.
But the question is what THE_COMMAND will be.
While true; do
last_touched="$(THE_COMMAND)"
now="$(date +%s)"
secs=$((now - last_touched))
echo "${secs} seconds ago"
sleep 3
done
I remember asking the same question a while back.
Here is what I found,
last -aiF -n 1 userName
command can give you the current session.
When combined with awk you can get the result as follows
$ last -aiF -n2 username
username :1 Wed Apr 21 13:09:00 2021 still logged in 0.0.0.0
username :1 Wed Apr 21 07:28:47 2021 - down (05:39) 0.0.0.0
$ last -aiF -n 2 ogulcan | awk '{print $10}'
in
(05:39)
the lines here are the session times.
These times are counted as now - first boot login
But I believe these does not work best for you.
So here is the 8 year old question that may be helpful to you.
User Idle time in Linux
Using python you can calculate the idle time passed. Maybe this way, you can simply get what you want with python.

Manipulate the system time for a given command

I want to run command with specific date in history.
Let say today is 12.Nov.2019 and I need to run one command with date of 10th. Is there any possibility of setting date on fly ?
date -d '1 day ago' /path/to/command
doesn't work.
Use can you faketime to run a command with given time:
faketime - manipulate the system time for a given command
Because you didn't tell us what command and time you want to run,
I suppose that you want to run /path/to/command on Oct 10th, 2019.
Run /path/to/command in 2 days ago:
faketime -f -2d /path/to/command
Run /path/to/command on Oct 10th, 2019:
faketime -f '2019-11-10' /path/to/command

Shell infinite loop to execute at specific time [duplicate]

This question already has answers here:
Sleep until a specific time/date
(22 answers)
Closed 7 years ago.
I have access to a Linux CentOS box. (I can't use crontab sadly)
When I need to run a task everyday I have just created a infinite loop with a sleep. (it runs, sleeps ~24 hours and then runs again)
#!/bin/sh
while :
do
/home/sas_api_emailer.sh |& tee first_sas_api
sleep 1438m
done
Recently I have a task that I need to run at a specific time everyday 6:00 am (I can't use crontab)
How can I create an infinite loop that will only execute # 6:00 am?
Check the time in the loop, and then sleep for a minute if it's not the time you want.
while :
do
if [ $(date '+%H%M') = '0600' ]
then /home/sas_api_emailer.sh |& tee first_sas_api
fi
sleep 60
done
You have (at least!) three choices:
cron
This is hands-down the best choice. Unfortunately, you say it's not an option for you. Drag :(
at
at and batch read commands from standard input or a specified file
which are to be executed at a later time.
For example: at -f myjob noon
Here is more information about at: http://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples/
Write a "polling" or "while loop" script. For example:
while true
# Compute wait time
sleep wait_time
# do something
done
Here are some good ideas for "compute wait time": Bash: Sleep until a specific time/date

Are there cron-expressions to run a script every ninth minute?

I'd like to run a bash command every 9th minute and every 70th minute.
man -S 5 crontab says:
Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".
I ran the command
echo "Cron-job runs. ($(date))" >> crontest.log
with the cron expression
*/9 * * * *
which gave me
Cron-job runs. (We 25. Mar 13:27:01 CET 2015)
Cron-job runs. (We 25. Mar 13:36:01 CET 2015)
Cron-job runs. (We 25. Mar 13:45:01 CET 2015)
Cron-job runs. (We 25. Mar 13:54:01 CET 2015)
Cron-job runs. (We 25. Mar 14:00:01 CET 2015)
Cron-job runs. (We 25. Mar 14:09:01 CET 2015)
Cron-job runs. (We 25. Mar 14:18:01 CET 2015)
It seems, that */9 means "every minute that can be divided by nine without remainder" (this includes zero!) instead of "every ninth minute".
Are there cron-expressions to define intervals?
No, there is no facility for this specific use case. You could create a complex crontab which lists all the combinations;
0-54/9 0-21/3 * * * crontest
3-57/9 1-22/3 * * * crontest
6-51/9 2-23/3 * * * crontest
(This complex syntax is an extension, but since you were asking about */9 which is a similar extension, you should be able to use the above as well. See an earlier version of this answer for the full-hand syntax, which however I incorrectly identified as extended in my original answer.)
For execution every 70 minutes, a similar table would be a lot more complex, because 70 is not evenly divisible by 1440 (24*60). You end up with a periodicity over multiple days, so the table gets so complex as to beg for alternative solutions.
... One of which would be to use a self-scheduling at job:
#!/bin/sh
echo "$0" "$#" | at now + 70 minutes
:
# the rest of your crontest script here
You need to start this one manually the first time, but it will keep scheduling new jobs after that. (If your server is down for an extended period of time, you may need to restart it; but the job should survive the occasional quick reboot just fine.)
Or you could run your script every minute, and the script can check if it's the right time to run.
untested
#!/bin/bash
minute_of_the_epoch=$(( $(date +%s) / 60 ))
(( minute_of_the_epoch % 9 != 0 )) && exit
# rest of script ...

Is there a variable in Linux that shows me the last time the machine was turned on?

I want to create a script that, after knowing that my machine has been turned on for at least 7h, it does something.
Is this possible? Is there a system variable or something like that that shows me the last time the machine was turned on?
The following command placed in /etc/rc.local:
echo 'touch /tmp/test' | at -t $(date -d "+7 hours" +%m%d%H%M)
will create a job that will run a touch /tmp/test in seven hours.
To protect against frequent reboots and prevent adding multiple jobs you could use one at queue exclusively for this type of jobs (e.g. c queue). Adding -q c to the list of at parameters will place the job in the c queue. Before adding new job you can delete all jobs from c queue:
for job in $(atq -q c | sed 's/[ \t].*//'); do atrm $job; done
You can parse the output of uptime I suppose.
As Pavel and thkala point out below, this is not a robust solution. See their comments!
The uptime command shows you how long the system has been running.
To accomplish your task, you can make a script that first does sleep 25200 (25200 seconds = 7 hours), and then does something useful. Have this script run at startup, for example by adding it to /etc/rc.local. This is a better idea than polling the uptime command to see if the machine has been up for 7 hours (which is comparable to a kid in the backseat of a car asking "are we there yet?" :-))
Just wait for uptime to equal seven hours.
http://linux.die.net/man/1/uptime
I don't know if this is what you are looking for, but uptime command will give you for how many computer was running since last reboot.
$ cut -d ' ' -f 1 </proc/uptime
This will give you the current system uptime in seconds, in floating point format.
The following could be used in a bash script:
if [[ "$(cut -d . -f 1 </proc/uptime)" -gt "$(($HOURS * 3600))" ]]; then
...
fi
Add the following to your crontab:
#reboot sleep 7h; /path/to/job
Either /etc/crontab, /etc/cron.d/, or your users crontab, depending on whether you want to run it as root or the user -- don't forget to put "root" after "#reboot" if you put it in /etc/crontab or cron.d
This has the benefit that if you reboot multiple times, the jobs get cancelled at shut down, so you won't get a bunch of them stacking up if you reboot several times within 7 hours. The "#reboot" time specification triggers the job to be run once when the system is rebooted. "sleep 7h;" waits for 7 hours before running "/path/to/job".

Resources