Multiple crontabs for one user

13,624

It's generally a good idea to maintain your crontab in a separate file anyway, and install it with crontab filename. (I keep my crontab file in a source control system.)

You could have multiple crontab files, and install them all with

cat file1 file2 file3 | crontab

The crontab command normally only manages a single crontab per user. But you can add system crontab files to the /etc/cron.d directory. These files have an extra field after the time specification that indicates the user for the job, and you can have multiple files per user. Even if one of them has a syntax error, the others will still execute.

You'll need root access to install files in that directory.

man 5 crontab for details.

I'm not convinced that circumventing the normal crontab mechanism like this is a good idea, but it should work.

(This assumes the "Vixie Cron" implementation, which is probably what your system uses.)

NOTE: You might be tempted to try

crontab file1 file2 file3 # WRONG

but a quick experiment shows that all file names but the first are silently ignored. The man page says that a single file name is accepted, but doesn't say what happens if multiple file names are provided.

Share:
13,624

Related videos on Youtube

realshadow
Author by

realshadow

Updated on September 18, 2022

Comments

  • realshadow
    realshadow over 1 year

    is there a way to use multiple crontab files for one user? Thinking something along the lines of crontab file per project instead of crontab per user...

    Any help is appreciated...

  • Admin
    Admin about 12 years
    I did that today. That would be easy to do, but the goal with this is that each project "manager" would have control over his own crontab. With that approach it would have to be combined like that each time someone changes one of the files, which means they will have to keep track of all the files that need to be compiled
  • Keith Thompson
    Keith Thompson about 12 years
    So create a program (or script, or whatever) to automate it. Note that you can invoke the crontab command from a crontab entry.
  • realshadow
    realshadow about 12 years
    Yes that wouldnt be a problem, but wouldnt that create a small problem? When you combine lets say 10 different files (aka from 10 different people) and one of them has wrong syntax, crontab wont install, but I would have to manually check each file to see who has a syntax error. Unless there is some cron syntax checker... Thats why I was asking if you can have multiple crontabs where cron manages them
  • reducing activity
    reducing activity about 8 years
    "It's generally a good idea to maintain your crontab in a separate file anyway, and install it with crontab filename" - why it is a good idea, except that it adds ability to put it under source control?
  • Keith Thompson
    Keith Thompson about 8 years
    @MateuszKonieczny: That's a good reason, but it's also to easy to erase your crontab accidentally. crontab with no arguments reads a new crontab from stdin.
  • reducing activity
    reducing activity about 8 years
    @KeithThompson - Is your cron repository published or discussed discussed somewhere? (though given that it is not listed at keith-s-thompson.github.io I am not expecting this). It would be nice to not reinvent the wheel for some common cron ideas.
  • Keith Thompson
    Keith Thompson about 8 years
    @MateuszKonieczny: No, I just keep it in a CVS repo and copoy it to $HOME when I update it. (I use CVS because Git didn't exist when I started doing this.)
  • Admin
    Admin over 7 years
    Note that you can add "* * * * * cat file[123] | crontab" to the crontab file itself to automatically update it whenever file[123] is updated. To be even more sophisticated, you can have a program that keeps a copy of the old crontab, tries to install the new one, but reverts if the new one fails, and then crontab THAT program to run every minute.
  • Keith Thompson
    Keith Thompson over 7 years
    @barrycarter: Sure, but I find keeping my crontab in a file much easier.
  • Admin
    Admin over 7 years
    @KeithThompson You mean in a single file? I thought your answer was proposing multiple files (which is a brilliant suggestion, btw!)
  • Keith Thompson
    Keith Thompson over 7 years
    @barrycarter: You certainly could use multiple files, with cat file1 file2 ... | crontab, if it's helpful to maintain different parts of your crontab separately. (I wrote this answer 4+ years ago, and I had forgotten that I had suggested that.) I just use a single file myself because the added complexity isn't necessary for my own usage.
  • Admin
    Admin over 7 years
    @KeithThompson Oh, gotcha! I'm creating a document to help people run Fedora Core 24 from "scratch" without evolution or gnome: github.com/barrycarter/bcapps/blob/master/FEDORA and wanted to share part of my cron file but not all of it. Your solution (creating bc-public-cron in the GIT directory and bc-private-cron outside of it) fits the bill nicely.