Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to execute a command as root on an Ubuntu 11.04 machine every 24 hours. This machine is not on all of the time, but when it is, it can be left on for days at a time. How should I correctly configure my crontab to execute this command at the desired interval, and is there anything special about executing a cron job as root that I should know about? I am the only person who uses this machine.

share|improve this question
1  
Note that your title seems to ask how you can execute a command 24 hours after each startup, while your question asks about doing it every 24 hours, with no regards to when the system was started. –  Daniel Andersson Feb 12 '13 at 11:46

2 Answers 2

up vote 3 down vote accepted

You can simply add your script to be executed to directory /etc/cron.daily - it will be executed once a day by anacron.

Excerpt from man 8 anacron:

Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron.

share|improve this answer
    
Sorry for taking so long to get back to this. It's been working great. –  Matthew G Feb 17 '13 at 23:05

To edit root's crontab, run:

sudo crontab -e

Fill in a line with e.g.:

0 0 * * * /usr/sbin/ntpdate-debian -s

This will execute the specified command ntpdate with switch -s once every stroke of midnight (see info 'crontab(5)' for a documentation of the format, or probably just the commented first lines of the crontab itself).

Some small pointers:

  • Note that the commands in crontab normally are executed via /bin/sh, which on Debian based system is not Bash, but Dash. Be careful to not use Bashisms in the commands. In my experience it is not uncommon to see incorrect redirects, and incorrect assumptions concerning which characters that need to be escaped.
  • It is often a good idea to give complete paths to the binaries since the PATH variable might differ between root and your regular user. I have seen situations where one command has shadowed another due to differing PATH variables, which is an unnecessary complication.

    It can also be viewed as somewhat of a security risk to simply execute the program that is first in the path lookup, since sloppy configuration might open this up for someone to replace a binary (if one has that kind of rights on the system, one can probably do it anyway to some extent, but nevertheless).

Cron will execute the command every 24 hours, as long as the machine is running at the time. If you want to make the machine "catch up" if it was turned off at a planned execution, you should look at anacron that has this exact purpose.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.