Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

The following cron job fails to execute. What's wrong with it? It works fine when I run it directly from the shell prompt.

www-data@etl:~$ crontab -l

0 5 * * *   export YESTERDAY=$(date --date "yesterday" "+%Y-%m-%d"); cd /var/www/project && source .env/bin/activate && ./ get_orders --date="$YESTERDAY" && sleep 5m && time ./ merch --date="$YESTERDAY" && time ./ order_flat_report --date="$YESTERDAY" && time ./ update_hourly_report --date="$YESTERDAY"

I'm getting this error message:

Return-Path: <www-data@etl>
X-Original-To: www-data
Delivered-To: www-data@etl
Received: by localhost (Postfix, from userid 33)
    id E170C43EC7; Fri, 18 Apr 2014 05:00:01 -0400 (EDT)
From: root@etl (Cron Daemon)
To: www-data@etl
Subject: Cron <www-data@etl> export YESTERDAY=$(date --date "yesterday" "+
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <MAITO=>
X-Cron-Env: <HOME=/var/www>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=www-data>
Message-Id: <20140418090001.E170C43EC7@localhost>
Date: Fri, 18 Apr 2014 05:00:01 -0400 (EDT)

/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
  • Ubuntu 13.10 (GNU/Linux 3.11.0-14-generic x86_64)
  • GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
share|improve this question

marked as duplicate by Iain, Dave M, mdpc, Rex, Ward Apr 18 '14 at 15:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I suggest you put it in a bash script file and execute it. – Florin Asăvoaie Apr 18 '14 at 12:26
Work your way through the duplicate above, doing so will solve your problem. – Iain Apr 18 '14 at 14:14
up vote 1 down vote accepted

It looks like cron does not correctly escape some of the quotes in your command while it passes the command to bash. As Florin suggests, you could put the command into a shell script, make it executable, store it e.g. in /usr/local/bin, and run it as a cronjob.

If it is not important that the job is run exactly at 05:00, you can also put the script in /etc/cron.daily/, thus avoiding the need to call it from crontab.

share|improve this answer

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