14

I ssh to a server, and want to add some daily jobs (specifially to renew Kerberos tickets even when I log out and I still want my programs in screen or tmux continue to run) to cron. So I run crontab -e, and add the following,

00 00 * * * kinit -R
00 12 * * * kinit -R

When I save it, I am asked by the editor:

File Name to Write: /tmp/crontab.HMpG7V 

Isn't it that the files in /tmp can be deleted by the OS? Especially after I log out of the server?

Where shall I store my crontab file? Can I save the crontab file under $HOME or some better space?

Tim
  • 101,790

3 Answers3

22

crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors and prevent you from overwriting your actual crontab with those errors. If there are no errors, then your actual crontab will be updated. If crontab -e just wrote straight to your actual crontab, then you would risk all of your cronjobs failing to run due to a syntax error in your new crontab.

sudoedit, visudo, vipw, etc. operate on the same principle.

Don't worry, your actual crontab lives in a non-volatile location on disk.

jayhendren
  • 8,384
  • 2
  • 33
  • 58
  • where is my crontab file stored eventually? If I log out of the server, what will happen to my crontab file? (I am not a admin, and doesn't not have root access to the server) – Tim Apr 20 '15 at 23:45
  • 1
    your actual crontab lives somewhere in /var/spool (typically), but you should never touch your actual crontab and you should never need to, either - use crontab -e instead. You probably don't have the right access level to edit the raw crontab file anyway. – jayhendren Apr 20 '15 at 23:46
  • if you log out, nothing will happen to your crontab. it doesn't live in /tmp. – jayhendren Apr 20 '15 at 23:47
  • (1) Is crontab file for individual user or for all users? (2) How can I verify if my edit to crontab file works? – Tim Apr 20 '15 at 23:48
  • crontabs are per user. to verify that your cronjob runs, you can check the cron logs. typically in /var/log/cron.log, but it could be other locations as well. – jayhendren Apr 20 '15 at 23:49
  • /var/log/cron.log: No such file or directory. The pathname doesn't seem like for me only? – Tim Apr 20 '15 at 23:53
  • ok, well, poke around. check your man page for your cron implementation or documentation for your distro. i don't have magic answers for you; this kind of thing is not universal. – jayhendren Apr 21 '15 at 16:23
6

/tmp is where the temporary crontab file is written to. After you save and quit, crontab will commit the changes to a different, more appropriate directory on your system (usually at /var/spool/cron, if I remember right). So, don't worry, the operating system may delete your /tmp, but it will never take away your crontabs.

2

Crontab files reside in /var/spool/cron/crontabs/ under one's username or user ID. Since situations can arise where the crontab located here is no longer connected to your login account, it is recommended that you save a copy to your home directory, say /home/userid/.crontab, so that you can easily restore it should you need to. This also permits you to place it under revision control should you like.

If you're home directory is relocated to another system, often the crontab from the spool directory doesn't accompany it unless your system admin takes this into account. Nor is /var/spool always backed up or restored when a system needs to be redone.

So again it is recommended that a copy of your crontab be kept under your home directory.

Use your favorite editor (pico, vi, emacs, etc) to edit ~/.crontab and then use crontab to apply it to /var/spool/crontabs. Like this:

% pico ~/.crontab; crontab ~/.crontab

If you like aliases, you can make one to run instead.

JonathanS
  • 312
  • 1
    Do I have to do something to make my home's crontab file override crontab files in other location? – Tim Apr 21 '15 at 00:14
  • Just run "crontab ~/.crontab" and you'll apply it to the one in /var/spool/cron/crontabs. Use pico, emacs, or vi to edit ~/.crontab before hand to make any changes. – JonathanS Apr 21 '15 at 00:45
  • This does not actually answer the question. – nobody Apr 21 '15 at 02:19
  • @AndrewMedico: The primary question seemed to be "Where shall I store my crontab file? Can I save the crontab file under $HOME or some better space?" Or are you referring to Tim's question in the comment here? – JonathanS Apr 21 '15 at 04:13