Ubuntu – the correct way to edit a crontab file

cron

I am trying to automate the refreshing of awstats for my webserver and understand that I need to use cron to set that up. So each user has a crontab file?

I was following the instructions here community/AWStats

And it says to go to the file at: /etc/crontab

That file already has some information in it. Is this the main crontab file or something? I have seen some instructions online to use the file that pops up with crontab -e.

What is the proper file to use and the best way to do this?

If I schedule a task as my non-root user, will it run by itself at the time interval as specified, with no issues?

Will I need to restart the server after saving the cron job in the crontab file before it will start working?

Best Answer

  • What is the proper file to use and best way to do this?

    crontab -e is the best way to do it - this will allow you to easily edit per-user crontabs.

    If I schedule a task as my non-root user, will it run by itself at the time interval as specified, with no issues?

    Yes, and it will run with the privileges of that user account.

    As a principle, it is best to run scheduled tasks with the lowest privileges you can get away with, so if what you are doing will not require root, don't use root. If you don't even need to be able to access your account's files and folders, then you can create a new user and use that user account only for that task. On the other hand if your task does require root, use root's crontab (su / sudo su to root then use crontab -e).

    Will I need to restart the server after saving the cron job in the crontab file before it will start working?

    No it'll start working straight away (well, the next possible scheduled time).

    Why not edit the file directly? Where is it located?

    The user crontab files are located in /var/spool/cron/crontabs, but their permissions are set in such a way that they can't be traversed to without superuser permission (but once open and the cron process drops privileges it can still access the file).

    The system is not designed for end users to edit these files directly, and indeed the top of the file has a stern DO NOT EDIT THIS FILE warning at the top to this effect. The file is instead designed to be edited via crontab -e which sets up a temporary mirror of the file in /tmp for editing - without the stern warning - after which it checks and installs the permanent crontab file itself. This can all be done without superuser permission.

    If you did edit the crontab file directly I don't know what the result would be. It's possible it simply wouldn't take effect until next restart and any errors may be hard to debug.

  • Related Question