Filo Stacks
Filo Stacks

Reputation: 2041

Appending to crontab with a shell script on Ubuntu

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!

Upvotes: 62

Views: 55266

Answers (4)

tal4444228
tal4444228

Reputation: 259

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

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

Upvotes: 20

jeroent
jeroent

Reputation: 2138

You can also do it without a temporary file:

(crontab -l ; echo "0 4 * * * myscript")| crontab -

Upvotes: 170

imxylz
imxylz

Reputation: 7937

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.

Upvotes: 1

alexander
alexander

Reputation: 2753

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

Upvotes: 36

Related Questions