how to use if/then statements in crontab?

Posted on 2009-05-26
Medium Priority
Last Modified: 2013-12-26

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

Question by:weihk
LVL 62

Expert Comment

ID: 24478287
You should not reboot UNIX automatically.

Specifying full path to commands helps in crontab.
No "if" statements in your command line, just gawk syntax.
LVL 11

Accepted Solution

Todd Mummert earned 800 total points
ID: 24478436
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.....

LVL 62

Assisted Solution

gheist earned 800 total points
ID: 24480029
This could be of more value when scripting in cron and avoiding reboots:

Author Comment

ID: 24485758
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

Assisted Solution

docbert earned 400 total points
ID: 24598980
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.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question