Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Ubuntu Server 12.04. I use:

sudo crontab -e
* * * * * root /etc/init.d/apache2 restart > /dev/null 2>&1
sudo restart cron

And it doesnt work. How to fix it? So many docs in google,I need one "true way".

share|improve this question
    
What are you trying to achieve with this entry ? Using wildcards for everything seems weird. –  alci Apr 3 '13 at 9:17

5 Answers 5

up vote 1 down vote accepted

I'm assuming that you have exited the cron tab after the 2nd statement. That third line won't work in crontab. Cron should reload itself.

The issue with your cron tab is that you have it set to run every minute of every hour of every day ect. That what the * means, all options.

View this in coumn format

` m - h dom mon dow command

          • root /etc/init.d/apache2 restart > /dev/null 2>&1

You need

0 1 * * * root /etc/init.d/apache2 restart > /dev/null 2>&1

This is the the 0th minute of the 1st hour, every day.

share|improve this answer
    
I use * * * * * root /etc/init.d/apache2 restart > /dev/null 2>&1 and it doesnt work –  tim Apr 4 '13 at 9:22
    
You missed the entire point. You can't have 5 * in a row. –  wlraider70 Apr 4 '13 at 19:22
    
0 1 * * * root /etc/init.d/apache2 restart > /dev/null 2>&1 - don't work. empty in logs –  tim Apr 5 '13 at 11:14
    
You should have a log.. do this 1 * * * * service apache2 restart thus will set the job to hourly. –  wlraider70 Apr 5 '13 at 15:31

Restart apache is a bad idea, maybe you can just reload? Or check apache status and restart if apache is down.

#!/usr/bin/env python
import commands
from subprocess import Popen

ip = '127.0.0.1'
try:
  link = urllib2.urlopen('http://%s' % ip).code
  print link
except urllib2.HTTPError as e:
  print e.code
except urllib2.URLError:
  Popen(['invoke-rc.d','apache2','restart'])

and cron job will be

*/5 * * * * restart_apache.py

And first of all, when you want to restart apache in scripts, you must check configuration file for errors.

#!/usr/bin/env python
import re
from subprocess import check_output, call

def is_config_ok():
    if re.findall('OK',check_output('apache2ctl configtest', shell=True)):
        return 1
    else:
        return 0

if __name__ == "__main__":
    if is_config_ok():
        call('invoke-rc.d apache2 restart', shell=True)
    else:
        print "error in config"
share|improve this answer

You are supposed to specify the time duration in crontab , you can review the syntax here http://content.hccfl.edu/pollock/unix/crontab.htm

For restarting apache using crontab http://www.richardtoh.com/how-to-restart-apache-using-cron-job

share|improve this answer
    
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the links for reference. –  Eliah Kagan Apr 3 '13 at 18:19

To become more familiar with the crontab timing commands you could try the Cron Sandbox.

Time/date parameters go in and a list of future run-times comes out.

share|improve this answer

You can use * * * * * this is every minute, just so no one is left believing incorrectly by raiders response.

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.