开发者

Updating crontab from a makefile

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 开发者_C百科"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?


  • Why are you 'escaping' crontab? As far as I can tell,

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

    should work fine.

  • Why use a conditional at all? If the cron table is empty, so be it:

    crontab.tmp: $(CRON_FILES) 
        @echo -n "Generating new cron table..."
        @crontab -l | grep -v "MAX-CRON-JOB" > $@ 
        @cat $(CRON_FILES) | awk '{print $$0, " ## MAX-CRON-JOB"}' >> $@ 
        @echo "OK"
    


    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
    
  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜