50

I'm trying to add a line to the crontab on Ubuntu.

Right now, I'm doing crontab -e and editing the crontab there.

However, I can't seem to find the real crontab file, since crontab -e seems to give you a temporary working copy.

/etc/crontab looks like the system crontab.

What is the path of the crontab that crontab -e saves to?

Thanks!

Filo Stacks
  • 1,811
  • 2
  • 19
  • 20

4 Answers4

145

You can also do it without a temporary file:

(crontab -l ; echo "0 4 * * * myscript")| crontab -
jeroent
  • 1,798
  • 2
  • 12
  • 8
  • 1
    wish i had looked at this one first. nice. – mdandr Feb 03 '15 at 19:14
  • 5
    What does `crontab -` mean? – Noumenon Oct 15 '16 at 23:48
  • 5
    `crontab -` means replace crontab with standard input. – Duvrai Mar 06 '17 at 17:44
  • 4
    Should be `(crontab -l 2>/dev/null; echo "0 4 * * * myscript")| crontab -` as otherwise `no crontab for $user` might be written to the output if no crontab was already present – LuGo Sep 21 '17 at 11:57
  • 3
    @LuGo that's not true, the no crontab message is stderr and crontab - writes stdin from the pipe. However, `(crontab -l || true; echo "0 4 * * * myscript")| crontab -` is necessary if `set -e` is in place since the subshell will error on a nonexistent crontab and silently not write the echo part. – dmikalova May 20 '18 at 06:07
34

Use crontab -l > file to list current user's crontab to the file, and crontab file, to install new crontab.

alexander
  • 2,583
  • 14
  • 13
  • 4
    For example, this one liner will add a job that runs script.sh on every reboot: `crontab -l > file; echo "@reboot /home/user/script.sh" >> file; crontab file; rm file` – Gumby The Green Jul 16 '19 at 10:20
16

If your crontab is empty you should use 2>/dev/null:

(crontab -l 2>/dev/null; echo "0 4 * * * myscript")| crontab -
PatJ
  • 5,452
  • 1
  • 26
  • 35
tal4444228
  • 209
  • 2
  • 4
  • Take it with caution because doing it this way `2>/dev/null` may not let you know of any other errors, I would personally rather to see an error once than to skip all the errors forever – Ordiel Feb 22 '16 at 18:13
  • @Ordiel Are there really any other errors you could get from `crontab -l`? – Noumenon Oct 16 '16 at 02:13
  • 2
    In typical shell behavior, the pipe already omits stderr: so without touching stderr, the error msg should still not end up in the crontab. E.g, if you *wanted* stderr in the pipe, in Bash you would add an '&' after the pipe: | – Rondo Feb 06 '18 at 04:32
  • This is unnecessary. Crontab will not append the stderr empty message to the crontab if it's empty. – rai Oct 02 '20 at 07:53
1

The user crontab file is in '/var/spool/cron/crontabs' for ubuntu.

adyliu@adyliu-pc:~$ sudo ls -lh /var/spool/cron/crontabs/adyliu
-rw------- 1 adyliu crontab 1.2K 2012-03-01 09:33 /var/spool/cron/crontabs/adyliu

'adyliu' is your login user.

You need root privilege to see this file.

Using "crontab -e" maybe is the best way to modify cron script.

In the manual:

Users are not allowed to edit the files under that directory directly to ensure that only users allowed by the system to run periodic tasks can add them, and only syntactically correct crontabs will be written there.

imxylz
  • 7,537
  • 4
  • 25
  • 25