Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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! :)
share|improve this answer
    
Kudos for the "what the heck you were thinking a year from now!". –  Tonny Mar 9 '12 at 12:14

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.