cronjob if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cronjob if condition
# 1  
Old 09-11-2012
cronjob if condition

I have two master servers (i.e) myserver1 and myserver2 but one of them is passive and another is primary one which is live in production

I have a script that will tell the status of the machine.
Quote:
unix@myserver1 > cat /tmp/server-status.pl
** Use -h to specify servicename (e.g. myserver.com))
** Use -f to specify status file (e.g. /home/unix/file.txt)
Usage: /tmp/server-status.pl -h servicename -f status_file
E.g. /tmp/server-status.pl -h myserver.com (dns-name) -f /home/unix/file.txt
Returns status (live|passive)

I have a cronjob which pulls the files from the active server in production and the main requirement is to have same cron file on both machines so there is no need to comment if we switch the machines.

Quote:
unix@myserver1 >crontab -l

cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver1.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1

cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver2.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1


unix@myserver2 >crontab -l

cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver1.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1

cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver2.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1

So for eg : if the myserver1 is passive then the two jobs will run on that machine as it meets the criteria, but i want only the job that will run against myserver2 which can pull the files off that machine.

Please assist in having the same crontab on both machines.

Thanks

---------- Post updated 09-11-12 at 11:24 AM ---------- Previous update was 09-10-12 at 04:53 PM ----------

So my question is if myserver2 is passive and not live in production it should only pull the files off myserver1 through rsync but the below jobs in cron pulls the files of myserver1 which is correct and even pull files off myserver2 (pulls files itself on the same server)

Quote:
unix@myserver2 >crontab -l
cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver1.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1

cd /tmp;if [ `./server-status.pl -h myserver.com -f /home/unix/file.txt` = 'passive' ];then (date;/usr/local/bin/rsync -auzv rsync://myserver2.oracle.com/WWW-primary-master-to-secondary /home/unix ); fi >> /var/log/cron/rsync-log 2>&1
# 2  
Old 09-15-2012
Ideally when the script gets complex like that I put it in a script and then have cron call the script. Much easier to maintain this way.
Code:
# something like this
# put all of that logic above into script.sh in a readable and maintainable way.
0 12 * * *  /path/to/my/script.sh >/path/to/mylogs/script.log 2>&1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cronjob

How to set cronjob for 48 hours. I can set for 2 days as shown below. * * */2 * * It is creating confusion for 30 days & 31 days per month. (3 Replies)
Discussion started by: Nishit
3 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. UNIX for Dummies Questions & Answers

Cronjob help

Hi I am very new to linux. I want to run a cronjob every 15 minutes that checks a directory for files. If the directory contains more than ten files I want it to send an email to me. All I have is this... */15 * * * * ls -l | wc -l | | mail -s "This is just a test" I would... (2 Replies)
Discussion started by: LinuxNewb
2 Replies

6. UNIX for Dummies Questions & Answers

how to cancel a cronjob if the cronjob still running

hi everyone I'm newbie in this forum hope I can get some help here :) I have a command in crontab that executed every 1 minute sometime this command need more than 1 minute to finish the problem is, the crontab execute this command although it's not finish processing yet and causing the system... (7 Replies)
Discussion started by: 2j4h
7 Replies

7. AIX

cronjob

Hi, I want to schedule a script on daily basis in morning 8:00 A.M and 5:00 P.M , how can i achieve this, i want the parameter 0 ? * * * what will be the second parameter in my case. Regards (1 Reply)
Discussion started by: maooah
1 Replies

8. Shell Programming and Scripting

Check a condition in cronjob to execute a sh file

Hi, I need to execute a crontab based on a condition where one SH file should be executed only based on the output of a file in a folder. I have written the following cron job which is not working. 00 01 * * * read a < /px/batch/reslut.txt && && sh /px/batch/check.sh where my... (2 Replies)
Discussion started by: shanth_chandra
2 Replies

9. Shell Programming and Scripting

Cronjob

Hi, I'm totally new to shell scripting. I need help in my crontab script. I'm trying to read some values from user (username, log file directory, server) and then use those value to create a crontab for log rotation after some interval of time. creating crontabs manually isn't big deal.... (11 Replies)
Discussion started by: MisterKhan
11 Replies

10. UNIX for Dummies Questions & Answers

cronjob

hi iam new at unix.iam running a cronjob,which i want to run on the first day of every month at 1AM.this is what i have specified: 00 01 1 * * is this corect? (1 Reply)
Discussion started by: wiz83
1 Replies
Login or Register to Ask a Question