Script should check and run only on Sunday


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script should check and run only on Sunday
# 22  
Old 06-23-2014
Quote:
Originally Posted by nanz143
pl correct me for a better solution if this one is not a suggested one.
Generally, there are usually lots of functional and non-functional ways to do something. That non-functional ways are to be avoided is obvious, but less obvious is that some functional ways should be avoided too.

If you know that going from A to B in a direct way means having to pass the traffic jam at C this makes the direct rooute less desirable and maybe the route over D E and F better overall, despite the route over C still being "functional" as it finally will get you where you want to go.

Quote:
Originally Posted by nanz143
2nd point. Thanks for pointing, even i forgot about this, and yes every week it should check for 3 times only then it should, so for every first run number file should be reset.

can you or wise cracker help me updating this script.
No problem. Here is how i would do it:

Code:
#! /bin/bash

pCheckDOW ()
{
typeset chDOW="${1:0:3}"

if [ "${chDOW}" != "Sun" ] ; then
     return 1
fi

return 0
}



pCheckAttempts ()
{
typeset fLog="${1}"
typeset iCurrDay="${2}"
typeset iNrOfAttempts=0
typeset iMaxAttempts=3
                                                     # clear log from old entries
grep "$iCurrDay" "$fLog" > "${fLog}.tmp"
mv "${fLog}.tmp" "${fLog}"
echo "$iCurrDay" >> "$fLog"

iNrOfAttempts=$(wc -l "$fLog")
if [ $iNrOfAttempts -gt $iMaxAttempts] ; then
     echo "Error: $iNrOfAttempts exceeds set maximum of $iMaxAttempts"
     return 1
fi
return 0
}





# ------- main
typeset chDate="$(date +'%a %d')"
typeset fAttemptsLog="/tmp/attempts.log"

if ! pCheckDOW "${chDate:0:3}" ; then
     echo "Error: This is the wrong day of week to run ${0}." >&2
     exit 1
fi

if ! pCheckAttempts "$fAttemptsLog" "${chDate:4:2}" ; then
     echo "Error: Attempts to run are already exhausted."
     exit 2
fi

# .... do lots of stuff here

exit 0

Notice, that illegitimate attempts on non-Sundays will not affect the attempts-log because the script terminates before even checking that.

The script relies on the fact that two consecutive Sundays will not have the same day of month. It could be fooled, though, if sufficient time between two attempts to run it would pass so that Sunday falls on the same day of month than the Sunday of the last attempt.

I hope this helps.

bakunin

Last edited by bakunin; 06-23-2014 at 03:52 PM.. Reason: corrected typo
This User Gave Thanks to bakunin For This Post:
# 23  
Old 06-24-2014
Understood wisecracker once the scriptSmilie ends Counter goes to 3 which will reset to 1 .

---------- Post updated at 10:42 AM ---------- Previous update was at 10:38 AM ----------

Thanks bakunin...
Am understanding this.. will test this also and get back to you. .

---------- Post updated 06-24-14 at 12:33 AM ---------- Previous update was 06-23-14 at 10:42 AM ----------

Hey wise cracker,
I tried the way you gave but when i run the script multiple times it is not giving me any error.........
it is just simply running,

---------- Post updated at 12:40 AM ---------- Previous update was at 12:33 AM ----------

Hey bakunin,
I tried your script, every thing was good, but wc -l was giving me the count and the file name as below and was givng error even for the first time...

4 attempts.log,

so i used grep -c,, which gave the count only,

Thanks for the help...

Last edited by nanz143; 06-24-2014 at 02:50 AM..
# 24  
Old 06-24-2014
Apologies my mistake...
Code:
if [ "${DATE:0:3}" == "Sun" ]
then
	echo "Do lots of tasks here!"
	exit 0
else

Should read:-
Code:
if [ "${DATE:0:3}" == "Sun" ]
then
	echo "Do lots of tasks here!"
else

I don't know why I added it as I was at work at the time and probably distracted.
This User Gave Thanks to wisecracker For This Post:
# 25  
Old 06-24-2014
yes I have removed exit 0 and tried, it worked, but forget to tell you... sorry for that.. but still this script will send an email for the consecutive successful run following weeks.

Like.
1st week, 1strun : No mail
2nd Week, 1st run : No Mail
3rd Week, 1st run: No Mail
4th Week 1st run: You will receive mail.

This is a risk for me,. we need to update the counter every day.
So i havemade few changes.

Code:
if [ "${DATE:0:3}" == "Sun" ]
then
	echo "Do lots of tasks here!"
else
	echo "It is not Sunday, so nothing to do!"
	exit 1
fi
n=$[ ( $n + 1 ) ]
printf "$n" > /tmp/number
if [ $n -gt 3 ]
then
	echo "Counter reset to 1 and email sent..."
	# Do your email send here...
	exit 2
fi
	n=1
	printf "$n" > /tmp/number

exit 0

This is working for me as expected.

Now both the scripts are working fine, I am confused which to use now. Smilie
Thank you once again wisecracker and bakunin....
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when displaying multi-line code segments.

Last edited by Don Cragun; 06-24-2014 at 05:32 AM.. Reason: Fix tags.
# 26  
Old 06-24-2014
Quote:
Originally Posted by nanz143
yes I have removed exit 0 and tried, it worked, but forget to tell you... sorry for that.. but still this script will send an email for the consecutive successful run following weeks.

Like.
1st week, 1strun : No mail
2nd Week, 1st run : No Mail
3rd Week, 1st run: No Mail
4th Week 1st run: You will receive mail.

This is a risk for me,. we need to update the counter every day.
So i havemade few changes.

Code:
if [ "${DATE:0:3}" == "Sun" ]
then
	echo "Do lots of tasks here!"
else
	echo "It is not Sunday, so nothing to do!"
	exit 1
fi
n=$[ ( $n + 1 ) ]
printf "$n" > /tmp/number
if [ $n -gt 3 ]
then
	echo "Counter reset to 1 and email sent..."
	# Do your email send here...
	exit 2
fi
	n=1
	printf "$n" > /tmp/number

exit 0

This is working for me as expected.

Now both the scripts are working fine, I am confused which to use now. Smilie
Thank you once again wisecracker and bakunin....
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when displaying multi-line code segments.
I am VERY surprised that this is working for you as expected.

The code you have above never reads n from /tmp/number, so the command shown in red above always sets n to 1. And, both lines in green write a partial line containing 1 with no terminating <newline> character to /tmp/number. And, the test shown in blue always compares 1 to 3 so you will never send mail (even if the comment "Do your email send here..." is changed to code that actually sends email).

Furthermore, n is incremented whether or not the code that replaces "Do lots of tasks here!" succeeds or fails. So, if you were reading x (instead of always setting it to 1), you would always send mail on the 3rd week, and despite the echo saying "Counter reset to 1 and email sent...", there is no code here that will reset n to 1 before the script exits with exit code 2.

If this is working as expected, I guess I don't understand what your script is supposed to do.
This User Gave Thanks to Don Cragun For This Post:
# 27  
Old 06-24-2014
Hi don...
This is only part of the code....my actual requirement is
If my codes in do lot of tasks runs successfully during its first 3 times ( using nretrys 3 ) counter should automatically set to 1 and redirect to /tmp/number. And n will take input from /tmp/number at starting of this script. . Which is not mentioned here...
I have tested with dummy code in got the result. . Have to check with my original code during weekends.
Please suggest for modifications. ..

Thanks in advance. .

If script fails for the 3rd time also it should send an email and reset the counter to 1 and exit from the script.
# 28  
Old 06-24-2014
Hi nanz143...

I am with Don here...

I have no idea how you changed my code and got it to work, plus, you now have a totally different requirement to your OP.

So you are saying that Sunday is no longer a requirement?

So EXACTLY what is the requirement now?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run script on every last sunday of the month

Hi ALL, I have been testing this script to run for every last Sunday of the month,looks like month which have 5 sunday (july 2016 )is not passing this and failing every time. Here is what I am using, current_date=31 echo " CURRENT DAY -> $current_date" if ... (2 Replies)
Discussion started by: netdbaind
2 Replies

2. Shell Programming and Scripting

How to check if script is run via cron or manual=command line?

Hi all, I have a script that can be run via cron or via the command line. Is there any way that I can place something on the script to be able to distinguish/differentiate whether the script was run via a user in the command line or whether it was run from the cron? (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

Script to run php and check value in txt

Hello, I'm looking for a bash script, that I will run from cron, that executes a php script. After 5 minutes that the php script is executed, the bash script, must check a value in a text file /public_html/check.txt if check.txt = 0 the script will stop, if check.txt is not = 0 it must... (0 Replies)
Discussion started by: andymc1
0 Replies

4. UNIX for Advanced & Expert Users

Autosys job run on Sunday

Hi, I need to create a autosys job which will run on every sunday at 7:30 AM NY time for each 10 min interval of time. days_of_week: su start_mins: 0,10,20,30,40,50 run_window:"07:30" Is it fine? Please help Thanks, Anup (2 Replies)
Discussion started by: anupdas
2 Replies

5. UNIX for Dummies Questions & Answers

Run script on every second sunday

I was to schedule a script in a crontab after every 15 days specically on every 2nd Sunday. I know that i can schedule on basis of weekdays, but can it be done by skipping in between???:wall: (5 Replies)
Discussion started by: masquerer
5 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

Crontab job to run every sunday

Hello, I wanted to run one of my shell script for every sunday at 5PM. Here is the crontab entry i am using.. 00 17 * * 0 /inventory/update.sh > /inventory/update.log 2>&1 The job is not kicking on sunday at the specified time.. Am i missing anthing? Any help is appreciated... (2 Replies)
Discussion started by: govindts
2 Replies

8. Shell Programming and Scripting

Check if script run by a user directly or via other scripts

Hi, i have a script 'a.sh' that should be called only by certain scripts like b.sh, c.sh Inside a.sh, how can i determine 1) if this script was run directly from command prompt (or scheduler) 2) if called via other scripts? Is there an easy way to get parent process name (not just pid),... (2 Replies)
Discussion started by: ysrinu
2 Replies

9. Solaris

How to check for Saturday or Sunday

Hi , I have a date parameter passed in YYYYMMDD format , how can I check whether it is Sat or Sun on Solaris box , as we can do the same easily on linux box by using date -d YYYYMMDD '+a' . Any pointres will be really helpful . (5 Replies)
Discussion started by: harpreetanand
5 Replies

10. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies
Login or Register to Ask a Question