title: How to get NGINX to restart/reload from a cron job?
body: <p>I am trying to restart the NGINX service from a script which is called via a cron job (it works when run in the command line by the root user) - I am not sure if this is relevant but the cron job in question is also created via a script. I have tried multiple commands but the current one that I am using is the below:</p>
  
  <p>sudo /etc/init.d/nginx restart</p>
  
  <p>Any help on this would be great! </p>
  
  <p>UPDATE </p>
  
  <p>Below is the script that the cron runs and tries to reload the nginx service</p>
  
  <pre><code>#!/bin/bash
  
  NGINX_CONFIG='/etc/nginx/sites-available'
  NGINX_SITES_ENABLED='/etc/nginx/sites-enabled'
  WEB_DIR='/var/www/vhosts/demos'
  
  EMAIL=$1
  DOMAIN=$2
  SITE_DIR=$3
  
  echo $DOMAIN
  echo $SITE_DIR
  
  # Remove the nginx configurtations
  rm -Rf $NGINX_CONFIG/$DOMAIN.conf
  rm -Rf $NGINX_SITES_ENABLED/$DOMAIN.conf
  
  # Remove the access log
  rm -Rf /var/log/nginx/$SITE_DIR.access.log
  
  # Remove unicorn socket log
  rm -Rf /tmp/unicorn.$SITE_DIR.sock
  
  # Remove the DB
  sudo psql -U postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity     WHERE pg_stat_activity.datname = '_$SITE_DIR' AND pid &lt;&gt; pg_backend_pid();"
  dropdb -U postgres $SITE_DIR
  
  # Remove the sites application from the server
  rm -Rf $WEB_DIR/$SITE_DIR/
  
  if [[ ! "$(/sbin/service nginx status)" =~ "start/running" ]]
  then
     /etc/init.d/nginx reload
  fi
  </code></pre>