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
# 15  
Old 06-22-2014
yes mfj,,

Also i would like to know how many times this script ran,
how do i check that, as i need to restrict this to run a max of 3 times (n retries will do that for me) on its 3rd failure, i need a mail to my group mail box that the job has failed,

I have used mailx function for the mail but i configured it for every failure, meaning i will get 3 mails on each server

i want that mail only on the 3rd failure of this script.

Pl suggest
# 16  
Old 06-22-2014
Something like this longhand:-
OSX 10.7.5, deafult bash terminal.
Code:
#!/bin/bash
# OSX 10.7.5, default bash terminal.
# sunday.sh
# As /tmp/number does not exist then this will create an error on the very first run. This is OK.
read n < /tmp/number
#
if [ "$n" == "" ]
then
	n=1
	printf "$n" > /tmp/number
fi
DATE=`date`
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
	n=1
	printf "$n" > /tmp/number
	echo "Counter reset to 1 and email sent..."
	# Do your email send here...
	exit 2
fi
exit 0

Results:-
Code:
Last login: Sun Jun 22 18:12:33 on ttys000
AMIGA:barrywalker~> rm /tmp/number
AMIGA:barrywalker~> ./sunday.sh
./sunday.sh: line 5: /tmp/number: No such file or directory
Do lots of tasks here!
AMIGA:barrywalker~> ./sunday.sh
Do lots of tasks here!
AMIGA:barrywalker~> ./sunday.sh
Do lots of tasks here!
Counter reset to 1 and email sent...
AMIGA:barrywalker~> _

EDIT:
The line "exit 1" is deliberately added but can be removed if not needed.
This section does not change or reset the number of retries but that is an easy task to cure...

Last edited by wisecracker; 06-22-2014 at 03:18 PM.. Reason: See above...
This User Gave Thanks to wisecracker For This Post:
# 17  
Old 06-23-2014
Awesome Wisecracker,

It worked as expected. Thank you so much, you saved my inbox !!! Smilie

Thanks all.. I have learnt a lot. !!!!
# 18  
Old 06-23-2014
Quote:
Originally Posted by wisecracker
Code:
#!/bin/bash
# OSX 10.7.5, default bash terminal.
# sunday.sh
# As /tmp/number does not exist then this will create an error on the very first run. This is OK.
read n < /tmp/number
#
if [ "$n" == "" ]
then
	n=1
	printf "$n" > /tmp/number
fi
DATE=`date`
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
	n=1
	printf "$n" > /tmp/number
	echo "Counter reset to 1 and email sent..."
	# Do your email send here...
	exit 2
fi
exit 0

There are two problems with this approach: the first one is structural. The main processing of a script shouldn't be buried in some if-branch IMHO because it makes it not obvious enough what is going on. I prefer, if i have to check several conditions, to put it that way:

Code:
if condition1 ; then
     exit 1
fi

if condition2 ; then
     exit 2
fi

# main processing here

exit 0


Now, this is mere esthetics and i have to admit it would run the way you wrote it either. The other problem is fundamental, though: what would happen if you start the script two times on sunday and it fails. Then you start it on next sunday twice....

I am not sure because the problem description of the threads owner doesn't go into that detail, but i feel that failed attempts should be cleared once the respective day is over.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 19  
Old 06-23-2014
Hi bakunin...

Points taken, maybe it is my amateur approach at viewing things that makes me code the way I do but......
Quote:
This section does not change or reset the number of retries but that is an easy task to cure...
......in my defence I did take the counter into account as I was aware of your points you quoted with the quote above.
# 20  
Old 06-23-2014
Agreed bakunin,

1. For the structural way, what you have suggested is also a right way. but I need an else statement to run few commands on the failure of the condition in the if statement, like send a notification email, or execute another command.

pl correct me for a better solution if this one is not a suggested one.

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.

Adding to it.

I see the counter resets every 3 times the script runs, but we need to update the way it considers the first run of the day, instead last run.

Kindly suggest

Last edited by nanz143; 06-23-2014 at 05:00 AM..
# 21  
Old 06-23-2014
Take note and think carefully why I have chosen this value of n:-
Code:
if [ "${DATE:0:3}" == "Sun" ]
then
	echo "Do lots of tasks here!"
	exit 0
else
	echo "It is not Sunday, so nothing to do!"
	n=3
	printf "$n" > /tmp/number
	# exit 1
fi

This User Gave Thanks to wisecracker For This Post:
 
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