2

I would like to automate some services using cron jobs.

When I try to edit the crontab file at /etc/crontab, I am not able to save it as it says that it is read only.

What can I do?

Yaron
  • 13,173

1 Answers1

5

You should note that the reason you got an error specifying that the file is read-only is because you are trying to edit a root owned file without being root.

If you want to edit /etc/crontab you should execute your editor as root - (e.g. sudo vi /etc/crontab). Note that it isn't the recommended way for regular users

The answer in here suggest using crontab -e in order to edit the crontab file.

Note that /usr/bin/crontab is executed as root (set user-id root), and will allow you edit crontab for your own user - the file will be created in this root owned folder /var/spool/cron/crontabs.

If you want to execute crontab services which will run as root You should use sudo in order to became root.

You can either update /etc/crontab directly by using sudo vi /etc/crontab (or use any other text editor) and add a line for your job specifying the user as root...

Or you can run:

sudo crontab -e

Which will edit root's crontab file in /var/spool/cron/crontabs.

Zanna
  • 70,465
Yaron
  • 13,173
  • How do I save the crontab I created with sudo crontab -e cause when I create like that and close the terminal, open and write sudo crontab -l it says there are no crontabs for root – The Only Smart Boy Oct 18 '18 at 12:48
  • @TheOnlySmartBoy - running crontab -e will open your default editor, when you complete writing your crontab rules, you should save the file and exit the editor. You should NOT close the terminal at that point. you should first save the file... – Yaron Oct 18 '18 at 13:49
  • @Zanna - thanks for your comment! I've updated my answer to clarify it. – Yaron Oct 21 '18 at 06:09