62

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!

2

4 Answers 4

172

You can also do it without a temporary file:

(crontab -l ; echo "0 4 * * * myscript")| crontab -
Sign up to request clarification or add additional context in comments.

6 Comments

wish i had looked at this one first. nice.
What does crontab - mean?
crontab - means replace crontab with standard input.
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 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.
|
36

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

1 Comment

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
20

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

(crontab -l 2>/dev/null; echo "0 4 * * * myscript")| crontab -

5 Comments

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 Are there really any other errors you could get from crontab -l?
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: |&
This is unnecessary. Crontab will not append the stderr empty message to the crontab if it's empty.
Or for sh or C shells: ( ( crontab -l ; echo "0 4 * * * myscript" ) | crontab - ) >& /dev/null
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.