views:

267

answers:

2

Hi there!

I'm trying to update the crontab from a GNU Make file. The idea is like this: I look through the existing cron table and filter out all entries marked as mine (via the comment) and save that to a temporary file. Then I add my jobs to that temporary file and make it a new cron table. That way the make file can be run several times without harming other people's jobs.

This is the relevant part of the make file:

crontab.tmp: $(CRON_FILES)
    @echo -n "Generating new cron table combining existing one with a new one ..."
    if $$(crontab -l); then crontab -l | grep -v "MAX-CRON-JOB"; fi > $@
    @cat $(CRON_FILES) | awk '{print $$0, " ## MAX-CRON-JOB"}' >> $@
    @echo "OK"

.PHONY: cronjobs
cronjobs: crontab.tmp
    @echo -n "Installing cron commands... "
    @crontab $<
    @echo "OK"

The troubling part is this line:

if $$(crontab -l); then crontab -l | grep -v "MAX-CRON-JOB"; fi > $@

When the cron table is empty it somehow breaks the make, while the respective generated bash command:

if $(crontab -l); then crontab -l | grep -v "MAX-CRON-JOB"; fi > crontab.tmp

Works OK from the command line.

Here is an error from the make (nothing particularly informative, if you ask me...):

Generating new cron table combining existing one with a new one ...if $(crontab -l); then crontab -l | grep -v "MAX-CRON-JOB"; fi > crontab.tmp
make: *** [crontab.tmp] Error 1

What am I missing here?

+1  A: 

Try changing this line to include a test (square brackets):

if [ $$(crontab -l) ]; then crontab -l | grep -v "MAX-CRON-JOB"; fi > $@

because, at least for me, this doesn't work at a bash prompt without it:

if $(crontab -l); then crontab -l | grep -v "MAX-CRON-JOB"; fi > $@

yields:

-bash: 0: command not found
Dennis Williamson
+1 for the observation about 'command not found'
Jonathan Leffler
+1  A: 
Beta
And if there is nothing in the crontab file, you may get an error status from grep, so you probably need to add '|| true' to the end of the 'crontab -l' line.
Jonathan Leffler
Well yes, the condition is mostly for making it a successful operation anyway, so make does not report an error, but I failed at it :(
maksymko