105

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

0

2 Answers 2

124

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.

3
  • 14
    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. Commented Apr 14, 2015 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. Commented Mar 13, 2018 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. Commented Mar 13, 2018 at 20:38
55

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> 
10
  • 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. Commented Apr 13, 2015 at 19:07
  • 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). Commented Apr 15, 2015 at 1:01
  • 7
    "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. Commented Jun 30, 2016 at 23:10
  • 3
    As the original asker, I think I should perhasp weigh in on this: The accepted answer was chosen on the basis of my question in its literal sense; the location of the file. My intentions for that file is irrelevant. I don't think editing in the additional info changes the answer, but it might be considered superfluous. Commented Nov 5, 2017 at 16:39
  • 1
    Please be a bit more open-minded, and accept that the use case you are thinking of probably isn't the only one. In my case, I have a reinstalled system, and I am trying to recover the old crontab of the previous system. The old filesystem was backed up on an external drive, which I have accessible. So I really do need the location of the old crontab, and I really don't need more people telling me to do crontab -l, so while I agree that this could be incorporated into the answer, it certainly isn't the canonically correct answer. Commented Sep 21, 2023 at 10:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.