Can someone tell me what's going on here? As far as I knew, these ways of executing an init script are identical.
box:~# whoami
root
box:~# /etc/init.d/nginx restart
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
box:~# service nginx restart
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
box:~# head -1 /etc/init.d/nginx
#!/bin/sh
box:~# /bin/sh /etc/init.d/nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
I don't understand why this worked and why the permission issue exists in the first place. The init script is running as root and should have access to the pid file.
Update 1: some background
I've tried your standard "permissions issue" troubleshooting, going so far as to make the pid file world-read/writable. Previously, I was getting the same permissions error on the error log, which was in a custom location: on a mount. I tried an unmount/remount with no success. After that I used the "sh" method which worked. After that, it seemed to make the log permissions error go away, though now I'm seeing the same error on the pid file (as you can see).
Update 2: in response to comments
box3:~# ls -alZ /var/run/nginx.pid
-rw-rw-rw-. root root unconfined_u:object_r:var_run_t:s0 /var/run/nginx.pid
box3:~# ls -alZ /etc/init.d/nginx
-rwxr-xr-x. root root system_u:object_r:httpd_initrc_exec_t:s0 /etc/init.d/nginx
Update 3: this is definitely an SELinux problem
Turning off SELinux solves the issue. I need to read up on SELinux Security Contexts.
box:~# setenforce 0
box:~# getenforce
Permissive
box:~# /etc/init.d/nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
box:~# setenforce 1
box:~# /etc/init.d/nginx restart
nginx: [emerg] open() "/mnt1/logs/nginx-error.log" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed