46

as many (most?) others, I edit my crontab via crontab -e, where I keep all routine operations such as incremental backup, ntpdate, various rsync operations, as well as making my desktop background christmas themed once a year. From what I've understood, on a fresh install or new user, this also automatically creates the file if it doesn't exist. However, I want to copy this file to another user, so where is the actual file that I'm editing?

If this varies between distros, I'm using Centos5 and Mint 17

  • 15
    +1 just for making your desktop background christmas themed once a year – Celada Apr 13 '15 at 18:23
  • 5
    @Celada It may not be frequent, but it's a routine task nevertheless. – Jarmund Apr 13 '15 at 18:25
48

The location of cron files for individual users is /var/spool/cron/crontabs/.
From man crontab:

Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.

  • 6
    The key words there are "they are not intended to be edited directly", so this answer is incomplete without Celada's command below, which provides a safer answer to the "copy to another user" portion of the question. If people get in the habit of directly editing crontabs without submitting them through the crontab command, they forego a lot of sanity-checking the command provides. – Monty Harder Apr 14 '15 at 15:25
  • @MontyHarder I agree "they are not intended to be edited directly" but what if there is a need of it, like there is a need to make an entry in the crontab via a bash script. you have to use the exact file, I don't think any external interface will help in that case, correct me if I am wrong. – Prabhat Kumar Singh Mar 13 '18 at 8:05
  • @PrabhatKumarSingh You still shouldn't directly edit the file. If you read Celada's command below, you'd have seen an example of how a script could manipulate a crontab file without directly editing it. man crontab explains how this works. – Monty Harder Mar 13 '18 at 20:38
33

heemayl is correct about the location of crontab files on Linux, but it might be different on other operating systems and "theoretically" is could also be in a different location on Linux. Essentially, when a special interface is provided to access the files, you should use it. This will ensure that cron gets to check the files before installing them, makes sure the files have the permissions it needs them to have, etc...

Therefore you should copy a crontab from one user to another using that interface, like this, not by accessing the files directly.

crontab -u <user1> -l | crontab -u <user2>
  • 3
    There are many good reasons why crontab files should not be directly manipulated by anything other than the OS itself. This is a far better solution. I really think it needs to be incorporated into the official answer. – Monty Harder Apr 13 '15 at 19:07
  • @MontyHarder There isn't really an "official" answer. There is the answer the person asking chose as personally useful to him/her (the one with the checkmark) and the answer the community chose (with the most upvotes). In short, you have 15 rep, you can suggest this is the right answer by upvoting it. Also, if you want to suggest improvements to the other answer, you need to comment on that answer—otherwise, its author won't be notified of your comment. – derobert Apr 13 '15 at 20:34
  • I say "official", you say "accepted". It's the one with the green check mark on it that everyone sees as THE answer, and it's incomplete, because it gives just enough information for people to get in trouble trying to edit crontabs outside of the crontab command itself. I tried to put in an edit on the other answer, incorporating this command, but "peer review" rejected it. – Monty Harder Apr 14 '15 at 15:14
  • 2
    The corollary of this answer is that one should redirect the output of crontab -l to a file, move the file to the other system, and pipe it to crontab. Or maybe even do it directly (crontab -l | ssh $remote_host crontab). – Blacklight Shining Apr 15 '15 at 1:01
  • 4
    "when a special interface is provided to access the files, you should use it." Imagine every application provided a special interface for editing config files instead of just exposing them through the fs, though. That would be quite the annoyance to keep track of. – Witiko Jun 30 '16 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.