Home > linux, Tip > Something to watch out while using ‘%’ in crontab

Something to watch out while using ‘%’ in crontab

% is a special character in crontab and it has to be escaped with another ‘%’ a ‘\’. I just found out through a fail-to-run crontab task:

Non-working version:

1 5 * * *       /bin/bash /path/to/myscript.sh `date +%Y%m%d -d '5 days ago'`

When a crontab fails it will drop an email to user’s mailbox. So here are the relavant lines I found in the mailbox (via command mail):

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

With those error messages I googled a bit and changed the crontab to the following
working version:
1 5 * * * /bin/bash /path/to/myscript.sh `date +%%Y%%m%%d -d ‘5 days ago’`

1 5 * * *       /bin/bash /path/to/myscript.sh `date +\%Y\%m\%d -d '5 days ago'`
Advertisements
Categories: linux, Tip
  1. No comments yet.
  1. October 1, 2011 at 2:27 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: