Escaping characters in cron

The following works as expected from the command prompt. But it does not work from cron.

mysqldumpslow <(tail -1000 `mysqladmin variables \
    | grep slow_query_log_file | awk '{print $4}'`) \
    | mail -s "slow log from `hostname` sorted by time" shantanu.oak`hostname`@gmail.com \
    > /root/slow_succ.txt 2> /root/slow_err.txt

I get the following error:

/bin/sh: -c: line 0: syntax error near unexpected token `('

Do certain characters need to be escaped in cron? Or is it an issue with subshell in cron?


I'll expand on what SvenW said by stating that I would put this command into a script for two reasons:

  1. It avoids any issues with escaping characters in crontab.
  2. It allows you to clearly indicate to your fellow administrators what the job does without wasting any of their cycles decrypting the one-liner magic you've created. Such as calling the script /root/bin/dump_mysql_tables_and_email_failure_report.sh. You may not be in an environment with other administrators, but this will serve to remind you what the heck you were thinking a year from now! :)

The easiest solution for cron escaping problems: Put the command in it's own shell script and just call this script from cron.


The reason it does not work is that cron is not bash. Creating a subshell using brackets is part of the bash syntax. A clue to this is in the first "word" of error message.

Backticks would work to create a subshell except that you are already using backticks inside your subshell so you can't use those again.

As the others have already said, write a script and call that from cron.