crontab : yesterday's date not showing up - date

I need a cron job to work on a file named like this:
20160307_20160308_xxx_yyy.csv
(yesterday_today_xxx_yyy.csv)
And my cron job looks like this:
53 11 * * * /path/to/python /path/to/python/script /path/to/file/$(date -d "yesterday" +"\%Y\%m\%d")_$(date +"\%Y\%m\%d")_xxx_yyy.csv >> /path/to/logfile/cron.log 2>&1
Today's date is getting calculated properly but I am unable to get yesterday's date working. The error is:
IOError: [Errno 2] No such file or directory: 'tmp/_20160308_xxx_yyy.csv'
Please help!

I found the answer to my own question.
I needed to use this to get yesterday's date:
53 11 * * * /path/to/python /path/to/python/script /path/to/file/$(date -v-1d +"\%Y\%m\%d")_$(date +"\%Y\%m\%d")_xxx_yyy.csv >> /path/to/logfile/cron.log 2>&1
Hope it helps somebody!

This version worked for me. Maybe it can be helpful for someone:
53 11 * * * /path/to/python /path/to/python/script /path/to/file/$(date --date '-1 day' +"\%Y\%m\%d")_$(date +"\%Y\%m\%d")_xxx_yyy.csv >> /path/to/logfile/cron.log 2>&1

Related

Issue with aix sed command

I am trying to uncomment "/usr/lib/sa/sa1" or "/usr/lib/sa/sa2" entries from one of the configuration file.
Below is the regex:
"^[0-9].*/usr/lib/sa/sa(1|2)"
Eg:
#0 * * * * /usr/lib/sa/sa1 1200 3 &
#5 23 * * * /usr/lib/sa/sa2 -s 0:00 -e 23:01 -i 3600 -ubcwyaqvm &
Output should be:
0 * * * * /usr/lib/sa/sa1 1200 3 &
5 23 * * * /usr/lib/sa/sa2 -s 0:00 -e 23:01 -i 3600 -ubcwyaqvm &
Tried the below "sed" command but no luck:
sed -e '/^#.*\/usr\/lib\/sa\/sa\(1\|2\)/s/^#//g' adm
Can you please let me know where am I going wrong here.
Thanks in advance!
You can use
sed '/^#.*\/usr\/lib\/sa\/sa[12]/s/^#//'
Here, no grouping construct is used, no alternation operator is used either. It is possible as the 1 and 2 are single char alternatives, and bracket expressions are supported in all versions of sed.
Also, see this online demo.

replacing first pattern using sed

I have a cron job with below details
The timing section is unknown, it might be hourly or daily.
cat zyx.txt
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 * * * * root /usr/local/bin/log-cleaner
I have to update it on the fly using sed, so that it should run every minute .
Here is what I have tried
sed -in 's/\(.*\) \(.*\) \(.*\)/\* \* \* \* \*/1' zyx.txt
But it is removing the user and path section
cat zyx.txt
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * *
Any help / suggestion using sed is much appreciated

Batch - Get the date from 8 days from now [duplicate]

This question already has answers here:
Subtract days in batch file
(3 answers)
How to get 3 days past date from current date Using Batch file
(2 answers)
Date arithmetic in cmd scripting
(6 answers)
Windows console %DATE% Math
(6 answers)
Closed 2 years ago.
I'm trying to get the date from 8 days from now to navigate in a tree structure.
I've use this but I got a problem at the start of the new month (I got a negative number and thats logical) :
set annee=%date:~6,4%
set mois=%date:~3,2%
set jour=%date:~0,2%
set /a j8=%jour%-8
cd %annee%\%mois%\%j8%
Got you a solution to resolve my problem ?
Thanks in advance.
The easiest way will be with a polyglot script that embeds jscript.
Here's the dayAdder.bat that accepts only one argument - the days you want to add to the current date and prints the result:
#if (#X) == (#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var days=parseInt(WScript.Arguments.Item(0));
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
WScript.Echo(date.addDays(5));
WScript.Echo("Year: " + date.getFullYear());
WScript.Echo("Month: " + date.getMonth());
WScript.Echo("DayOfTeWEek: " + date.getDay());
examaple:
E:\scripts>dayAdder.bat 7
Sun Nov 8 16:27:48 UTC+0200 2020
Year: 2020
Month: 10
DayOfTeWEek: 2
DayOfTheMonth: 3
You can modify it in way that will be suitable for you.

text processing to select date range

I have below input and I want to select lines with dates from now to 2 weeks or 3 weeks and so on.
0029L5 08/19/2017 00:57:33
0182L5 08/19/2017 05:53:57
0183L5 02/17/2018 00:00:16
0091L5 10/19/2022 00:00:04
0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
0100L5 08/20/2017 01:25:39
0111L5 08/21/2017 00:46:15
0128L5 08/21/2017 12:38:51
D00054 07/21/2017 09:01:19
So the desired output if let say I want for 2 weeks from now
0045L5 07/27/2017 09:03:56
D00054 07/21/2017 09:01:19
But if i want for let say 4 weeks then the output should be
0045L5 07/27/2017 09:03:56
0059L5 08/14/2017 00:51:50
D00054 07/21/2017 09:01:19
One way:
awk '{split($2,a,"/");split($3,b,":"); x=mktime(a[3]" "a[1]" "a[2]" "b[1]" "b[2]" "b[3]);y=systime();}x>y && x<(y+(n*7*24*60*60))' n=2 file
where n indicates the number of weeks
split($2,a,"/") => Split the 2nd column on the basis of / and store in array a
split($3,b,":") => Split the 3rd column on the basis of : and store in array b
mktime => gives the time in seconds
x contains the time in file in seconds
y contains the current time in seconds
Here's one solution using bash where file is the name of your file:
while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s))); echo $(($dd/(3600*24))); done < file
This will compute the date difference in seconds between the date in ${r:6} (substring of the current row) and today's date $(date +%s) and convert it to days.
To output only lines where the date difference is less than 2 weeks (1209600 seconds)
while read r; do dd=$(($(date -d "${r:6}" +%s) - $(date +%s))); if [ "$dd" -lt 1209600 ]; then echo $r; fi; done < file
This works fine, Please let me know in case anybody has any other simpler solution for AIX.
awk '{split($2,a,"/");split($3,b,":"); print $1,b[3],b[2],b[1],a[2],a[1],a[3]}' /tmp/TLD_1 | head -10 | while read media sec min hour day mon year; do month=$((10#$mon-1)); expiry=$(perl -e 'use Time::Local; print timegm(#ARGV[0,1,2,3], $ARGV[4], $ARGV[5]), "\n";' $sec $min $hour $day $month $year); current=$(date +%s); twoweeks=$(($current + (2*7*24*60*60))); if [ "$expiry" -gt "$current" -a "$expiry" -lt "$twoweeks" ]; then echo "$media $mon/$day/$year $hour:$min:$sec"; fi; done

Better method to append to output crontabs

So i have simple shell commands to ping websites to retrieve data about said websites.
For example one of my pinging.sh looks like this:
ping -R -c 120 blar.org.cn >> pingdata.txt
ping -R -c 120 another.net >> pingdata.txt
But then my crontabs look like this:
7 * * * ./pinging.sh >> pingdata.log
The pingdata.log doesn't output. Is it best to do it through the crontab or through the script? I assumed the crontab would be better because it would cover the entire script rather than having to write it out multiple times.
You need to indicate the full path of your script in the cronjob, together with the binary running it.
For example:
7 * * * * /bin/sh /home/you/pinging.sh >> /home/you/pingdata.log
Note also you are just adding 4 parameters to the cronjob, whereas you need at least 5:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
You can test your cron syntax with Crontab guru (---> http://crontab.guru/).
First, the executable must be provided as full path in cron.
Example:
7 * * * * /bin/bash /path/to/pinging.sh
Second, create a wrapper script for pinging.sh >> pingdata.log and add that to crontab.
Third, your crontab entry is wrong. There must be 5 fields whereas your's have 4 (maybe that's a typo ?)