Tips
Apr 27, 2013

Having Nginx init.d script issues? It might be your comments.

I just updated to Nginx 1.2.8 on my dedicated server and even updated the init.d startup scripts at the same time (I had some custom-written scripts that needed updated). Well, I noticed a weird issue when I tried to restart Nginx:


➜  ~ /etc/init.d/nginx restart
Restarting nginx: nginxnginx: invalid option: "/var/run/nginx.pid"

So I checked the init.d script for Nginx... at the very top it had something like this:


PID=$(awk -F'[ ;]' '/[^#]pid/ {print $2}' /etc/nginx/nginx.conf)
if [ -z "$PID" ]
then
  PID=/var/run/nginx.pid
fi

That's perfectly valid code... so I ran "awk -F'[ ;]' '/[^#]pid/ {print $2}' /etc/nginx/nginx.conf" in my zsh prompt:


➜  ~ awk -F'[ ;]' '/[^#]pid/ {print $2}' /etc/nginx/nginx.conf
set
/var/run/nginx.pid

Obviously, this isn't what $PID should be returning... it should be returning "/var/run/nginx.pid"... where's the set coming from? So I checked "/etc/nginx/nginx.conf" and I wrote this:


# set the pid of nginx
pid /var/run/nginx.pid;

So that one comment above it threw off awk when it was searching for "pid". As I'm too lazy to re-write the script, I just changed my "pid" in my comment to "process id". Ideally, I'd rewrite the regular expression and have it check for comments correctly. Scratch that... as I'm an open source fanatic, I just spent some time figuring out how to get it to work. There might be a better way to do it, but this is how I finally fixed this:


awk -F'[ ;]' '$1 !~ /^#/ && /pid/ {print $2}' /etc/nginx/nginx.conf

I'll contribute the code to Dotdeb-Nginx and see if they'll put the fix in place so it doesn't crash anyone else's Nginx startup script...

Stalk me on social media! Any other way would just be creepy…