ValidationException: Parameter ScheduleExpression is not valid - aws-lambda

My cron syntax is
cron(25 8 10 2 4 2022)
And I wish to run the job exactly once on Thursday February 10 at 8:25 am. But getting the above error when trying to run.

You can schedule lambda event using following syntax:
# NOTE: This will execute on UTC mode only
cron(Minutes Hours Day-of-month Month Day-of-week Year)
So your cron expression should look like:
cron(25 08 10 02 ? 2022)
For more information, here is the official AWS Guide

Related

How to perform date operations in bash

Basically, I want to take a time, and day of the week, in UTC+8, and adjust the datetime object to a given UTC offset, within bash, I don't have any code to show because I'm not sure how to start attempting this in the first place honestly
(I'm writing a custom script for a friend who lives in UTC+8 and want to make the input as easy as possible for them, basically they just give it a time in their timezone, and a day of the week, and it'll tell them what date and time that'll be in a different timezone, for an overarching purpose)
For a reference, look at the section 1 of the manual page for "date":
In your shell, just type: man 1 date
or see the online man page:
https://man7.org/linux/man-pages/man1/date.1.html
One way is to parse the date into the number of seconds since epoch (since 1970), and then convert that number of seconds into the format you want:
For example:
$ date +%s --date='2022-12-27 11:30:17 +008'
1672140137
$ date +%c --date='#1672140137'
Tue 27 Dec 2022 06:22:17 AM EST
or you could also convert to ISO format then back to local time
$ date -Iseconds --date='TZ="GMT" 2022-12-22 11:33:44 +08'
2022-12-21T22:33:44-05:00
$ date --date='2022-12-21T22:33:44-05:00'
Wed 21 Dec 2022 10:33:44 PM EST
I hope this helps you get started with some ideas for converting to/from different timezones.
Also, to help with user input, you can show the current month calendar using cal
$ cal
December 2022
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
$ date --date='Fri 08:30'
Fri 30 Dec 2022 08:30:00 AM EST
In the above example, I specified "Fri 08:30" which gets set to the next Friday at 08:30 in the morning for my local timezone.

12 hrs to 24 hrs converter shell script

I want the date format Thu Nov 3 17:21:08 2016 to be converted to Thursday, 3 November 2016 7:48:24 PM AEDT .
Actually, i want to achieve in converting the 12hrs time to 24hrs format using a shell script .
Thanks
I believe below command would help you to go ahead:
date +"%T"

Strange behavior for the date() function linux [duplicate]

This question already has an answer here:
bash "date" returns "invalid date" error for a specific date string
(1 answer)
Closed 7 years ago.
I have found a strange behaviour for the date() function when using bash.
When I use date -d "2008-10-12 +1 days", the date function returned
date: invalid date ‘2008-10-12 +1days’
Even it returned error when trying:
date -d "2008-10-12"
--> date: invalid date ‘2008-10-12’
However, it works for:
date -d "2008-10-13 +1days"
--> Tue Oct 14 00:00:00 CLST 2008
date -d "2008-10-11 +1days"
--> Sun Oct 12 01:00:00 CLST 2008
date -d "2007-10-12 +1days"
--> Sat Oct 13 00:00:00 CLT 2007
... and so on
It seems the date() does not recognize "just" 2008-10-12. Is it a bug? Am I doing something wrong?
I am using CENTOS 7
All the best
In RHEL 7 there were many changes happened to somany commands. To change the time and date earlier we used to use date command and now we need to use timedatectl command (preferred) instead.
http://www.freedesktop.org/software/systemd/man/timedatectl.html
So as there is a new latest command with more features included they might have limited it (Not sure on that). But from this version I have been using timedatectl command which is really great one.

Passing alias as argument in bash

You can use built in command cal for example in this way:
cal 10 2013
and you will get a calendar of Octber 2013. My teacher made and assignment to change first argument in a way, that we could call this function in our native language. For example:
cal spalis 2013
cal gruodis 1999
and so on..
(spalis means october, and gruodis -december in my country)
And we would see same result as cal 10 2013 or cal gruodis 1999
Same goes with others months. I am newbie in shell, and thought that i could make an alias:
alias spalis=10
and then i tried to use cal spalis 2013, but i got error:
cal: spalis is neither a month number (1..12) nor a name
So, after googling for a while, i guess there is no way to pass alias as an argument? Or i'am doing something wrong? Maybe there is some other way? I'll be very thankful for an advice and sorry for bad english :)
Aliases only work for the command name, not for arguments. What you'll want to do is write a function cal which replaces the first argument with the appropriate number and then calls the regular cal binary.
cal() {
local month=$1
local year=$2
# replace named $month with a numeric $month here
...
# Call the real cal. `command' ignores this function and calls the underlying binary.
command cal "$month" "$year"
}
cal accepts month names in the language specified by the current locale. To accept Lithuanian month names, use
$ LANG=lt_LT cal spalis 2013
Spalio 2013
Sk Pr An Tr Kt Pn Št
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
This depends on the version of cal you are using. The above works on my MacOS X 10.9 machine, but not on one of my Linux boxes (where cal doesn't accept month names at all, only numbers).
This may not be exactly what your teacher is looking for, as it only requires using the program as intended. :)

How to schedule to run first Sunday of every month

I am using Bash on RedHat. I need to schedule a cron job to run at at 9:00 AM on first Sunday of every month. How can I do this?
You can put something like this in the crontab file:
00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script
The date +%d gives you the number of the current day, and then you can check if the day is less than or equal to 7. If it is, run your command.
If you run this script only on Sundays, it should mean that it runs only on the first Sunday of the month.
Remember that in the crontab file, the formatting options for the date command should be escaped.
It's worth noting that what looks like the most obvious approach to this problem does not work.
You might think that you could just write a crontab entry that specifies the day-of-week as 0 (for Sunday) and the day-of-month as 1-7, like this...
# This does NOT work.
0 9 1-7 * 0 /path/to/your/script
... but, due to an eccentricity of how Cron handles crontab lines with both a day-of-week and day-of-month specified, this won't work, and will in fact run on the 1st, 2nd, 3rd, 4th, 5th, 6th, and 7th of the month (regardless of what day of the week they are) and on every Sunday of the month.
This is why you see the recommendation of using a [ ... ] check with date to set up a rule like this - either specifying the day-of-week in the crontab and using [ and date to check that the day-of-month is <=7 before running the script, as shown in the accepted answer, or specifying the day-of-month range in the crontab and using [ and date to check the day-of-week before running, like this:
# This DOES work.
0 9 1-7 * * [ $(date +\%u) = 7 ] && /path/to/your/script
Some best practices to keep in mind if you'd like to ensure that your crontab line will work regardless of what OS you're using it on:
Use =, not ==, for the comparison. It's more portable, since not all shells use an implementation of [ that supports the == operator.
Use the %u specifier to date to get the day-of-week as a number, not the %a operator, because %a gives different results depending upon the locale date is being run in.
Just use date, not /bin/date or /usr/bin/date, since the date utility has different locations on different systems.
You need to combine two approaches:
a) Use cron to run a job every Sunday at 9:00am.
00 09 * * 7 /usr/local/bin/once_a_week
b) At the beginning of once_a_week, compute the date and extract the day of the month via shell, Python, C/C++, ... and test that is within 1 to 7, inclusive. If so, execute the real script; if not, exit silently.
A hacky solution: have your cron job run every Sunday, but have your script check the date as it starts, and exit immediately if the day of the month is > 7...
This also works with names of the weekdays:
0 0 1-7 * * [ "$(date '+\%a')" == "Sun" ] && /usr/local/bin/urscript.sh
But,
[ "$(date '+\%a')" == "Sun" ] && echo SUNDAY
will FAIL on comandline due to special treatment of "%" in crontab (also valid for https://stackoverflow.com/a/3242169/2919695)
Run a cron task 1st monday, 3rd tuesday, last sunday, anything..
http://xr09.github.io/cron-last-sunday/
Just put the run-if-today script in the path and use it with cron.
30 6 * * 6 root run-if-today 1 Sat && /root/myfirstsaturdaybackup.sh
The run-if-today script will only return 0 (bash value for True) if it's the right date.
EDIT:
Now with simpler interface, just one parameter for week number.
# run every first saturday
30 6 * * 6 root run-if-today 1 && /root/myfirstsaturdaybackup.sh
# run every last sunday
30 6 * * 7 root run-if-today L && /root/lastsunday.sh
There is a hacky way to do this with a classic (Vixie, Debian) cron:
0 9 1-7 * */7
The day-of-week field starts with a star (*), and so cron considers it "unrestricted" and uses the AND logic between the day-of-month and the day-of-week fields.
*/7 means "every 7 days starting from weekday 0 (Sunday)". Effectively, this means "every Sunday".
Here's my article with more details: Schedule Cronjob for the First Monday of Every Month, the Funky Way
Note – it's a hack. If you use this expression, make sure to document it to avoid confusion later.
maybe use cron.hourly to call another script. That script will then check to see if it's the first sunday of the month and 9am, and if so, run your program. Sounds optimal enough to me :-).
If you don't want cron to run your job everyday or every Sunday you could write a wrapper that will run your code, determine the next first Sunday, and schedule itself to run on that date.
Then schedule that wrapper for the next first Sunday of the month. After that it will handle everything itself.
The code would be something like (emphasis on something...no error checking done):
#! /bin/bash
#We run your code first
/path/to/your/code
#now we find the next day we want to run
nskip=28 #the number of days we want to check into the future
curr_month=`date +"%m"`
new_month=`date --date='$nskip days' +"%m"`
if [[ curr_month = new_month ]]
then
((nskip+=7))
fi
date=`date --date='$nskip days' +"09:00AM %D` #you may need to change the format if you use another scheduler
#schedule the job using "at"
at -m $date < /path/to/wrapper/code
The logic is simple to find the next first Sunday. Since we start on the first Sunday of the current month, adding 28 will either put us on the last Sunday of the current month or the first Sunday of the next month. If it is the current month, we increment to the next Sunday (which will be in the first week of the next month).
And I used "at". I don't know if that is cheating. The main idea though is finding the next first Sunday. You can substitute whatever scheduler you want after that, since you know the date and time you want to run the job (a different scheduler may need a different syntax for the date, though).
try the following
0 15 10 ? * 1#1
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
00 09 1-7 * 0 /usr/local/bin/once_a_week
every sunday of first 7 days of the month

Resources