LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 03-06-2016, 11:47 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Rep: Reputation: 177Reputation: 177
problem using test in cron


This is probably something simple ...

I have the following crontab entry:

Code:
* * * * mon /bin/bash -c "test $(date +%d) -le 07 && echo yup >>/root/yup.log 2>&1"
It is suppose to test that the current day of the month is <= 7th and do the `echo`, on monday.

The command `test $(date +%d) -le 07 && echo yup` works just fine at a command prompt, but in the crontab I get an error email:

/bin/sh: 1: Syntax error: end of file unexpected (expecting ")")

I've stared at this for a long time and can't for the life of me see what's wrong. I'm also a bit confused about the "/bin/sh" bit because I'm explicitly specifying /bin/bash.

Changing to:

Code:
* * * * mon /bin/bash -c "if [ `date +%d` -le 07 ]; then echo yup; fi >>/root/yup.log 2>&1"
(or /bin/date) give the error: "/bin/sh: 1: Syntax error: EOF in backquote substitution"

Note that I am using this cron on Ubuntu 15.10. The crontab entry as initially shown works fine on Slackware.

Last edited by mfoley; 03-06-2016 at 11:58 PM.
 
Old 03-07-2016, 05:18 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Couple of suggestions:

The use of backtick `` is deprecated, $() is the preferred methodology
Rather than try to write your script as a one-liner in the cron you may find it's better to just have a bash script that does all your tests etc. That way you can expand it much more easily.
 
Old 03-07-2016, 05:20 AM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Personally, if I require conditionals (very common, last day of month, first week of month on wednesday, etc.) I write a small script and CALL it from cron, or call a conditional script and
Quote:
&&
between it and the action.

1. Check, and see if /bin/sh really happens to be bash in disguise. If so stop explicitly calling bash and try just the command. Although it is in posix mode, most built-in and extensions work the same.

2. Try using
Quote:
if [ .. ] ; then ... fi
instead of
Quote:
test
and see if that works better.

Last edited by wpeckham; 03-07-2016 at 05:21 AM.
 
Old 03-07-2016, 08:16 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Ubuntu> and has been moved accordingly to help your thread/question get the exposure it deserves.

Requested by OP
 
Old 03-07-2016, 08:38 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The "%" sign is special in a crontab and needs to be escaped with a backslash to be treated as a regular character.
Code:
* * * * mon /bin/bash -c "test $(date +\%d) -le 07 && echo yup >>/root/yup.log 2>&1"
                                       ^
 
2 members found this post helpful.
Old 03-07-2016, 11:27 PM   #6
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by TenTenths View Post
Couple of suggestions:

The use of backtick `` is deprecated, $() is the preferred methodology
If you'll notice in my original example I did use $(). Given the error 'Syntax error: end of file unexpected (expecting ")")', I thought the parenthesis might be an issue and I changed to `` to test that.

Quote:
Rather than try to write your script as a one-liner in the cron you may find it's better to just have a bash script that does all your tests etc. That way you can expand it much more easily.
In reality, I am using a script. My "echo yup" is a test stand-in. The test is simply determining when the script should run which, I think, is the job of a crontab entry.

Quote:
Originally Posted by wpeckham View Post
Personally, if I require conditionals (very common, last day of month, first week of month on wednesday, etc.) I write a small script and CALL it from cron, or call a conditional script and

Quote:
&&
between it and the action.
Well, that's essentially what I'm doing with the &&. As I said in response to TenTenths, my "echo yup" is a test stand-in for the actual script. I have always commonly done the when-to-run logic in the crontab itself, not the called script -- and I want the same NOW!

Quote:
1. Check, and see if /bin/sh really happens to be bash in disguise. If so stop explicitly calling bash and try just the command. Although it is in posix mode, most built-in and extensions work the same.
Hmmm, on this Ubuntu system /bin/sh is slinked /bin/dash -- whatever that is. However, your point is worth some experimentation.

Quote:
2. Try using
Quote:
if [ .. ] ; then ... fi
instead of
Quote:
test
and see if that works better.
Perhaps you didn't read the WHOLE posting. That's exactly what I show in my 2nd example where I say "Changing to:".

Quote:
Originally Posted by rknichols View Post
The "%" sign is special in a crontab and needs to be escaped with a backslash to be treated as a regular character.

Code:
* * * * mon /bin/bash -c "test $(date +\%d) -le 07 && echo yup >>/root/yup.log 2>&1"
^
rknichols - that did the trick! Works like a charm now. thanks.

Interesting that the Slackware cron does not require % to be escaped.

Last edited by mfoley; 03-07-2016 at 11:30 PM.
 
Old 03-08-2016, 10:29 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by mfoley View Post
Interesting that the Slackware cron does not require % to be escaped.
Slackware uses "Dillon's lightweight cron daemon", which does not support some of the features of other cron daemon implementations, such as the use of the "%" sign to provide data on stdin to the command. Quote from `man 5 crontab`:
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL vari- able of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Also, the reason that the complaint came from "/bin/sh" is that the command line is first passed to the shell specified at the top of the crontab, so what it saw was a command to invoke /bin/bash with some arguments:
Code:
/bin/bash -c "test $(date +
Unsurprisingly, it didn't like that truncated command line.
 
Old 03-11-2016, 12:38 AM   #8
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
I typically I have cron test the day of month, and than the shell test the day of week.

Also it is run in sh. BUT remember this may not be BASH, so using backtic while depreciated is recommended.

Code:
  0 0 1-7 * * [ `date '+\%u'` -eq 1 ] && CMD
NOTE do not just use '*' in the first two columns That means cron will run it EVERY MINUTE on the days selected
The above will only attempt to run 7 times every month.

Last edited by A.Thyssen; 03-11-2016 at 12:39 AM.
 
  


Reply

Tags
crontab, test



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache and cron on Fedora test machine delite Linux - Server 3 11-04-2015 06:01 PM
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 02:43 PM
Test if script is launched from cron or CL 0x29a Programming 4 05-06-2008 12:35 AM
cron 'test' message in my /var/log/messages file visaris Linux - Newbie 1 12-13-2004 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration