11

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.
5
  • 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, 2015 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, 2015 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, 2015 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, 2015 at 16:28
  • Tried running it, received an invalid command error.
    – Chris
    Jun 15, 2015 at 21:54

5 Answers 5

11

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.

3
  • 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, 2015 at 10:57
  • 1
    Answer is ambiguous as to which nginx is being referred to. invoke-rc.d nginx does not invoke /usr/sbin/nginx but rather /etc/init.d/nginx. In Ubuntu 18 at least this works fine.
    – gerardw
    May 12, 2020 at 16:06
  • My answer was specific to OP's problem, he was running 14.04 and my answer was written in 2015, I believe it solved his problem at the time. By the time 18.04 came along, the bug had probably been fixed. It's great to know it's been fixed, tks for the update.
    – X Tian
    May 12, 2020 at 16:19
7

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.

4

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

1
  • 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) Oct 28, 2016 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .