10

I'm running Ubuntu 14.04 LTS and nginx on a Digital Ocean VPS and occasionally receive these emails about a failed cron job:

Subject

Cron test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

The body of the email is:

/etc/cron.daily/logrotate: error: error running shared postrotate script for '/var/log/nginx/*.log ' run-parts: /etc/cron.daily/logrotate exited with return code 1

Any thoughts on how I can resolve this?

Update:

/var/log/nginx/*.log {
  weekly
  missingok 
  rotate 52 
  compress 
  delaycompress
  notifempty 
  create 0640 www-data adm
  sharedscripts
  prerotate
      if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
          run-parts /etc/logrotate.d/httpd-prerotate; \
      fi
  endscript 
  postrotate 
      invoke-rc.d nginx rotate >/dev/null 2>&1
  endscript 
}

Update:

$ sudo invoke-rc.d nginx rotate
initctl: invalid command: rotate
Try `initctl --help' for more information.
  • it looks like it can't run what is specified as the postrotate action, show us your /etc/logrotate.d/nginix script – X Tian Jun 14 '15 at 16:19
  • /var/log/nginx/*.log { weekly missingok rotate 52 compress delaycompress notifempty create 0640 www-data adm sharedscripts prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi \ endscript postrotate invoke-rc.d nginx rotate >/dev/null 2>&1 endscript } – Chris Jun 14 '15 at 20:39
  • 1
    It's normal to update your question with further information when requested. 4 spaces at start of each line makes it a code block. – X Tian Jun 15 '15 at 16:26
  • So, invoke-rc.d nginx rotate is failing, try running that as the appropriate user, and show us that script too, paste output into your original question. tks. – X Tian Jun 15 '15 at 16:28
  • Tried running it, received an invalid command error. – Chris Jun 15 '15 at 21:54
9

The post rotate action appears to be incorrect

Try

invoke-rc.d nginx reload >/dev/null 2>&1

If you look at the nginx command you will see the actions it will accept. Also the message you got says check initctl --help

xtian@fujiu1404:~/tmp$ initctl help
Job commands:
  start                       Start job.
  stop                        Stop job.
  restart                     Restart job.
  reload                      Send HUP signal to job.
  status                      Query status of job.
  list                        List known jobs.

so reload should work and send HUP signal to nginx to force reopen of logfiles.

  • Thank you, that command seems to have run without an error. I'll update the cron job and we'll go from there. – Chris Jun 16 '15 at 10:57
  • There's a typo (nginix), but I cant edit because it's a single char change. – Koen. Sep 23 '15 at 10:22
4

As mentioned in another answer, the problem is that invoke-rc.d nginx rotate returns an error stating that the rotate action is not supported. The interesting thing is that service nginx rotate works without issues.

My guess is that the invoke-rc.d wrapper doesn't support all the actions the actual nginx init script supports.

Changing invoke-rc.d nginx rotate to service nginx rotate should solve the issue.

3

I'm not sure if it because initctl dose not support the rotate option, and when it was removed, but you are not the only one effected by this, and there are open bug report for this on launchpad.

As mention by other answers above&below, you can edit the nignx logrotate file and replace the problmatic line

invoke-rc.d nginx reload >/dev/null 2>&1

with other alternatives which works,

start-stop-daemon --stop --signal USR1 --quiet --pidfile /run/nginx.pid --name nginx
# or 
service nginx rotate >/dev/null 2>&1
# or
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`

What ever method you chose, please not that you are changing a file which is manage by a package, and after you change it, It won't be updated any more and you'll have to manually resolve the diff or overwrite it with a new one(which all ready include the fix).

  • I'm not sure the "service commands don't work" bug applies here, as there's different issues addressed in each bug. (Fun fact: I have a fix in the works for 1450770) – Thomas Ward Oct 28 '16 at 1:04
1

Worked for me:
Replaced
postrotate invoke-rc.d nginx rotate >/dev/null 2>&1
With
postrotate service nginx rotate >/dev/null 2>&1

0

Replace:

invoke-rc.d nginx reload >/dev/null 2>&1

With:

[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`

seems on newer versions of Nginx this works. I am running 1.9 versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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