0

I'm trying to get a cron job to execute every 20 minutes that will check to make sure a directory isn't down and will output a timestamp to a text file if it is. The cron job is

*/20 * * * * if [ -d PATH_TO_FOLDER ] && [ -s "$(ls -A PATH_TO_FOLDER)" ]; then :; else timestamp=$( date +%T ) && destdir=PATH_TO_TEXT_FILE && echo "$timestamp" >> "$destdir"; fi

This will work perfectly when typed in the command window, but whenever I try to run it as a cron job I get the error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
/bin/sh: -c: line 1: syntax error: unexpected end of file

I can't figure it out. Thanks for the help.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • What are `PATH_TO_FOLDER` and `PATH_TO_TEXT_FILE`? Are they specific paths/files, or are they implemented as shell variables? – lurker Jul 02 '15 at 16:27
  • They are specific directory and file names. – user2180462 Jul 03 '15 at 15:45
  • Are they absolute starting from root `/` or relative? – lurker Jul 03 '15 at 16:19
  • Absolute, they all start from /. – user2180462 Jul 03 '15 at 16:27
  • What I'm not sure about is if `cron` is happy with using the built-in shell constructs (`if`, `then`, etc) directly in-line. As a test, you could take what you have there, put it into a shell script file, and run the shell script file from `cron` to see if that works. – lurker Jul 03 '15 at 16:48
  • That's what I've been trying to do, I've been running it as a .txt file. – user2180462 Jul 03 '15 at 17:02
  • It's unclear to me what you mean by that. What you are showing is not running it as a text file. If you make a script file, you can call it whatever you like, but need to make it executable and call it with an absolute path. – lurker Jul 03 '15 at 17:14
  • Maybe that was my mistake....I wasn't calling it with the absolute path. It seems to be working now. – user2180462 Jul 03 '15 at 18:02
  • Yes. You have to remember that `cron` jobs don't execute from your home directory, and they don't have your normal environment variables and PATH set. You need to make sure they are set the way you want them to be within the `crontab` file. – lurker Jul 04 '15 at 17:54

1 Answers1

0

% character is interpreted as a newline by cron, you need to escape it -- see Percent sign % not working in crontab

Community
  • 1
  • 1
sendmoreinfo
  • 582
  • 6
  • 22