how to use if/then statements in crontab?


I'm new to unix shell programming. I was wondering if there is a way to use if/then statements in crontab? If so, can you please give me an example, or the problem I'm working on is reading a file for a specific line (ex: hello there) and if it is read more than 2 times then execute a reboot of that particular server.

The code below is what I have so far but not sure how to fully utilize the if/then statement. Thanks for your help.
*/5 * * * * awk '{if($15 ~/XFMRDPAD/) shutdown -i6}' StructureService_PR.log

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should not reboot UNIX automatically.

Specifying full path to commands helps in crontab.
No "if" statements in your command line, just gawk syntax.
Todd MummertCommented:
if you really want to use awk, you could do it like this.    Actually, once c == 2 you could go ahead and call shutdown if you wanted....

awk '{if ($15 ~ /XFMRDPAD/) c++ } END { if (c > 1) system("shutdown -i6") }' StructureService_PR.log

I agree w/ the warning about automatic reboots.....


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This could be of more value when scripting in cron and avoiding reboots:
weihkAuthor Commented:
i found out that i'm suppose to stop the IS (integration server) using this command:
call vtadmin stop server /StructureService_PR/initial/StructureService_IS

and reboot the project using this command:
call vtadmin start project /Projects/StructureService_PR/initial

can i just replace the "shutdown -i6" with those commands? or how will i be able to implement it into the code? thank you
Trying to put too much information into cron is a bad idea from the perspective of maintainability.

Configure cron to run a single script, and then put whatever commands/logic you need into the script - either in standard Bourne shell, or Perl/Python/etc - whatever you prefer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.