Cron Fails on Solo Apostrophe

The following works as expected:

  date + '% d-% b-% Y-% H-% M'

 28-Sep-2009-14-28 

But none of the following 4 records of crontab work.

  * * * * * date + \ '% d-% b-% Y-% H-% M \' >> /backup/shantanu/testing.txt
 * * * * * date + '% d-% b-% Y-% H-% M' >> /backup/shantanu/testing1.txt
 * * * * * date + "% d-% b-% Y-% H-% M" >> /backup/shantanu/testing2.txt
 * * * * * date + \ "% d-% b-% Y-% H-% M \" >> /backup/shantanu/testing3.txt
  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 save the same code in a shell script and install cron, but I will be interested to know if it is possible to directly install cron for the task.

The actual cron entry I'm trying to install looks something like this:

  16 * * * * mysqldump myDB myTB> / backup / ABCbc $ (date + '% d-% b-% Y-% H-% M'). Sql 2> / backup / ABCbc_errORS $ (date + '% d- % b-% Y-% H-% M '). txt 
+6
unix cron
source share
3 answers

There are four common reasons why cron job commands behave differently than commands entered directly into the interactive shell:

  • Cron provides a limited environment, such as the minimum $PATH and other expected variables.
  • Cron calls /bin/sh by default, while you can use some other shell interactively.
  • Cron processes the % character intentionally (it turns into a new line in the command).
  • A team may behave differently because it does not have a terminal.

You must precede all % c \ characters in the crontab file that tells cron to simply put % in the command, for example.

  16 * * * * mysqldump myDB myTB> "/ backup / ABCbc $ (date + '\% d - \% b - \% Y - \% H - \% M'). Sql" 2> "/ backup / ABCbc_errORS $ (date + '\% d - \% b - \% Y - \% H - \% M'). txt "

(As a separate issue, always put double quotes around "$variable_substitution" or "$(command substitution)" if you don’t know why not do it in a particular case. Otherwise, if the contents of the variable or the output of the command contains spaces or ?*\[ , they will be interpreted by the shell.)

+17
source share

As long as there are no spaces in the format string provided as an argument before the date, you don't need ticks at all.

 date +%d-%b-%Y-%H-%M 

must work.

0
source share

You are using syntax not supported by / bin / sh. Try calling your preferred shell and passing the command as an argument.

0
source share

All Articles