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
Related
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'
I want to validate, the date given as input is 7 day old or more from current day in bash script linux
This command provides the date 7 days previous to now, in seconds (since 1/1/1970):
minus7=$(date -d "7 days ago" "+%s")
This command provides the date in seconds of the user's input date:
tstDate=$(date -d "$yourInputDate" "+%s")
then
let delta=$tstDate-$minus7
I am trying to setup a cron job on a Ubuntu server. I want the cron job to run the script on weekly basis. Problem is - It should be a working day, If im mentioning it with time interval, it fails during weekoffs - Need an Schedular Exp which has to work weekly only on working days at office hours.(9am to 9pm)max.
Want to Execute the script every week #6 pm during the weekdays. It Can be Mon to Fri.
Step1
sudo apt-get install cron
systemctl status cron
Step2
Configure the cron job:
crontab -e
Select an editor of your choice
0 0 * * 0 /path/to/command
Syntax: minute hour day-of-month month day-of-week command.
Day-of-week goes from 0-6 where 0 is Sunday.
Save it.
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 ...
I have created scripts and now I want them to run automatically at specified time, how can I do that?
I am using Ubuntu and JAVA to write my scripts.
You can use crontab to do this in a specific time continuously.
To edit a crontab entries, Login as root user (su – root) and do crontab -e as shown below. By default this will edit the current logged-in users crontab.
root#dev-db# crontab -e
Scheduling a Job For a Specific Time
The basic usage of cron is to execute a job in a specific time as
shown below. This will execute the Full backup shell script
(full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.
30 08 10 06 * /home/ramesh/full-backup
More example here, and the crontab utils can be found here