Solved

Find EOM in Unix crontab

Posted on 2014-10-31
15
365 Views
Last Modified: 2014-11-07
I have a command to run a script in crontab but it doesn't work.  This is the command   test `echo \`cal\`|awk '{print $NF}'` -eq `date +\%d`.  What is wrong with it?
0
Comment
Question by:OCCU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
15 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40416643
Backslashes (\) must be escaped (\\) under cron because it eats up the first one:

test `echo \\`cal\\`|awk '{print $NF}'` -eq `date +\%d` &&  <run the script>

The backslash before % is also consumed by cron, so that the percent sign is passed to "date" instead of being interpreted as start of stdin. It must thus not be escaped!

Better use the "$( ... )" notation instead of backticks. No escaping is required then:

test $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) && <run the script>

You could also replace "test" with "[ ... ]":

[ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] && <run the script>

All of the above worked for me under AIX.


wmp
0
 

Author Comment

by:OCCU
ID: 40416647
If it returns a true it will run script.  This script does work.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40416653
Did you try my suggestions?
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:OCCU
ID: 40416660
I will try it tonight and let you know.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40416665
This part:

echo \`cal\`

is not executing the cal command, it is merely echoing the string cal - this is why your command in your script is not working.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40416666
If you're on Linux (or have GNU date installed on Unix) you could also use its features instead of "cal":

test $(date -d "-1 days +1 month" "+\%d") -eq $(date "+\%d")  && <run the script>
0
 

Author Comment

by:OCCU
ID: 40416669
If I remove the echo would it work?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40416670
Gerwin - please note that it's nested backticks!

So e. g.

echo `echo \`cal\``

works on command line, but not under cron. The inner backticks must be escaped twice there.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40416673
Only change you need to make is getting the cal command to run:

test `echo $(cal) | awk '{print $NF}'` -eq `date +%d`

Open in new window

^^^ this is working and is returning a $? of 0 (so true).
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40416674
@wmp - I know - I never nest backticks :D
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40416675
>> If I remove the echo would it work?   <<

No, because you'll get multi line output then.

This will work:

cal | tail -1 |awk '{print $NF}'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40416677
Gerwin - I don't even use backticks if at all possible!
0
 

Author Comment

by:OCCU
ID: 40422169
I was able to get my script to run by using [ $(echo $(cal) |awk '{print $NF}') -eq $(date +\%d) ] &&.

Thanks for your help
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40422176
You're always welcome. Glad I could help!

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40429080
Please, what are your reasons for giving grade B?
You wrote above that you followed exactly my suggestion!

Please read here about awarding grades.

Quote:
B is the grade given for acceptable solutions, or a link to an acceptable solution. A B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.
Should you want to review your decision click "Request Attention" above and explain your demand.

wmp
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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