Ubuntu: What is the correct way to edit a crontab file?



Question:

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 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?


Solution:1

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 easy 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).


Solution:2

Every user has it's own crontab.

To see it just type

crontab -l  

and, yes you've seen it right, when you want to add a crontab then simply do

crontab -e  

for the first time you'll be asked about the editor to use with crontab. As you are a newbie as you say, I'd recommend to use nano, it's the simplest editor to use.

The crontab itself works like this

MIN =  Minute 0-60  HOUR = Hour [24-hour clock] 0-23  MDAY = Day of Month 1-31  MON = Month 1-12 OR jan,feb,mar,apr ...  DOW =  Day of Week 0-6 OR sun,mon,tue,wed,thu,fri,sat  COMMAND = Command to be run Any valid command-line  

So e.g. to trigger a job every 15 minutes only on monday, you do it like so

*/15 * * * mon /home/me/yourscript.sh  

and YES it's very important that you put your cronjob into the crontab of the user which has enough rights to execute it.

So if your script needs to be root or a special user, make sure to su to that user and add the cronjob there.

Also very important!

cronjob is dumb! Yes I said it. Always make sure to enter the full PATH to the application/command/script you are using, because crontab won't work with .bashrc or similar. You always have to make sure that you tell cron where it can find the files and directories.

To answer the rest of your questions:

Of course it will run on it's own, that's what it's there for. But you need to check that cronjob really does it, or if it's missing something. E.g. log into a logfile to see if it really works.

And NO, a restart is not needed. A restart with linux is normally only needed, if you install a new kernel. For it to use you'd need to reboot. Almost everything else can be done without reboot in Linux. Of course there are exceptions, but in general this statement is correct.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »