15

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...

Darren Cook
  • 280
  • 4
  • 15
realshadow
  • 251
  • 1
  • 2
  • 5

2 Answers2

16

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.

Keith Thompson
  • 5,075
  • 2
  • 25
  • 33
  • 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 –  Feb 18 '12 at 00:22
  • 1
    So create a program (or script, or whatever) to automate it. Note that you can invoke the `crontab` command from a crontab entry. – Keith Thompson Feb 18 '12 at 00:42
  • 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 – realshadow Feb 18 '12 at 10:16
  • "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? – reducing activity Apr 07 '16 at 17:12
  • 1
    @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. – Keith Thompson Apr 08 '16 at 03:40
  • @KeithThompson - Is your cron repository published or discussed discussed somewhere? (though given that it is not listed at http://keith-s-thompson.github.io/ I am not expecting this). It would be nice to not reinvent the wheel for some common cron ideas. – reducing activity Apr 08 '16 at 05:01
  • 1
    @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.) – Keith Thompson Apr 08 '16 at 15:53
  • 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. –  Nov 17 '16 at 18:13
  • @barrycarter: Sure, but I find keeping my crontab in a file much easier. – Keith Thompson Nov 17 '16 at 18:15
  • @KeithThompson You mean in a single file? I thought your answer was proposing multiple files (which is a brilliant suggestion, btw!) –  Nov 17 '16 at 18:16
  • @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. – Keith Thompson Nov 17 '16 at 18:19
  • 1
    @KeithThompson Oh, gotcha! I'm creating a document to help people run Fedora Core 24 from "scratch" without evolution or gnome: https://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. –  Nov 17 '16 at 18:23
4

Why would you do that? You could use multiple lines in crontab - I think that would do the job too.

There are also the directories you could probably use:

cron.d/
cron.daily/
cron.hourly/
cron.monthly/
cron.weekly/
Gaff
  • 18,569
  • 15
  • 57
  • 68
  • Thats how it is now :) I just would like to know if there is a way to do this –  Feb 17 '12 at 22:58