On reading documentation when programs don't work as expected
No matter how often you use UNIX tools, once in a while you get caught out trying to put everything together. This happened this morning while I was setting up a cronjob and it didn't work as expected.
This happened to me earlier this week when I tried to setup a quick cron script to dump the contents of an SQL table every day. I had set it up as follows:
37 3 * * * /usr/lib/postgresql/8.2/bin/pg_dump db -U backup -t table | bzip2 - > /backups/table-monitoring/`date +%Y%m%d`.table.sql.bz2
I checked the next day that things were working and found the directory empty. Interesting.
After a quick check of my email I found the culprit:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"' /bin/sh: -c: line 1: syntax error: unexpected end of file
This is where things got complicated. Had I checked crontab(5) this story would probably have ended here. I didn't.
After a couple of attempts at escaping this command line to make things work I ended up with the following:
backup:~% ls /backups/table-monitoring | head -n 3 `date \+%Y%m%d`.table.sql.bz2 date +%Y%m%d.table.sql.bz2
Even more interesting...
Turns out the answer is quite simple. The man page has this to say about it:
The ‘‘sixth’’ field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the
shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after
the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell’s trailing "\".
Ooops. A quick change later, I end up with the following snippet:
37 3 * * * /usr/lib/postgresql/8.2/bin/pg_dump db -U backup -t table | bzip2 - > /backups/table-monitoring/`date +\%Y\%m\%d`.table.sql.bz2
I'm still wondering about the usefulness of the % to send data to the standard input as opposed to using standard pipes though.
- Java Web Portal Developer - Front End15/03/2010
Other
I.T. & T
Are you an experieced Java web applications developer with experience in Spring and Web content management systems? - Senior Business Analysts - PCI industry experience15/03/2010
Other
I.T. & T
Excellent opportunity for Senior BA's to work on a new project. 6 Years BA experience - 4 x Business Analyst - IT15/03/2010
Information Technology and Internet
I.T. & T
Experienced IT BA - in various areas, have you worked on large projects? Looking for a stable company and career progression? Apply Now!! - Program Manager15/03/2010
Other
I.T. & T
Heavy hitting Program Manager required for an upcoming need. Good $$$ for the right candidate. Utilities experience advantageous. - Siebel Administrator/Configurator15/03/2010
Other
I.T. & T
Strong Siebel background. Oracle E-business suite expeirence a bonus. Great company name! - Customer Service/Sales Support - IT15/03/2010
Other
I.T. & T
Do you have a passion for IT? Do you take pleasure in advising clients in their product selection? Are you seeking an employer which recognises talent - Information Management Team Lead Oil & Gas15/03/2010
Other
I.T. & T
International Co - Oil & Gas - Great Salary for right candidate. This is not a Records Management role - much more technical and hands on..
Whitepapers
-
Maximising customer capital -
Beyond PCI Checklists: Securing Cardholder Data with enhanced File Integrity Monitoring -
Executive Insight | Achieving Service Excellence: Leveraging Cross-Enterprise Workflow to Organise Around the Customer -
Enterprise Management | A Computerworld Strategy Guide -
Operational Responsiveness | An Executive Guide
TechWorld Blogs
Recent blog posts
- Following social networking privacy
- All aboard the Avatar Economy
- Facebook, PayPal tie up ad payments
- Google goes for more markets: too much too quickly?
- Talk about mobile computing
- iPad arrives: can Apple crack the tablet?
- Linux.conf.au 2010 kicks off in New Zealand
- VMware jumps further into SaaS with Zimbra
- Amarok 2.2.2 released – rock on!
- Happy Nexus Year





Recent comments
21 min 14 sec ago
7 hours 17 min ago
10 hours 58 min ago
1 day 2 hours ago
3 days 1 hour ago
3 days 17 hours ago
4 days 11 hours ago
4 days 20 hours ago
5 days 6 hours ago
5 days 17 hours ago
5 days 23 hours ago
6 days 12 hours ago
1 week 5 hours ago
1 week 1 day ago
1 week 2 days ago
1 week 2 days ago
1 week 3 days ago
1 week 3 days ago
1 week 4 days ago
1 week 4 days ago