You need to login to send post. No account yet? Create one:
Create account

BASH Script to restart mailserver on VPS with Plesk panel

No replies
admin

Keep VPS's services alive is crucial for any web designer. The problem is that usual shortage of RAM causing crashes almost on regular basis. On another place of this forum is published script which will restart Apache when necessary.

The second key part is a mailserver.

On Plesk is used Qmail mailserver with Courier service for POP3/IMAP. Therefore we can use simple BASH script to check it, and, if necessary, restart all Plesk services (which is actually easier than restart all services one by one). Save thish BASH script into /root/scripts folder, name it mail_restart.sh and give it an exec permission:

  1. #!/bin/bash
  2.  
  3. exists=`ps ax | grep -v grep | grep courier`
  4.  
  5. if [ "$exists" = "" ]
  6. then
  7. /etc/init.d/psa stop
  8. /etc/init.d/psa start
  9. echo "Qmail stopped work, PSA have been restarted" | mail -s "Server Alert: PSA on VPS have been restarted" sentry@yourdomain.com
  10. exit
  11. fi

Next step is start this script regularly (once in 5 minuts should be OK) using CRON. As you have Plesk panel, you should use it under root account (or alternatively use command crontab -e) to add:

*/5 * * * * /root/scripts/mail_restart.sh >/dev/null 2>&1

 

OR... Combine this scripts into Webdesigner's combo :)

Script below is combination of Apache & mailserver restart script into one, which will watch all crucial services for you. Use it exactly in the same way as the single ones...

  1. #!/bin/bash
  2.  
  3. # Make sure you have these paths correct
  4. result=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php`
  5. service='courier'
  6.  
  7. if ps ax | grep -v grep | grep $service > /dev/null
  8. then
  9. if [ $result != 1 ]
  10. then
  11. /etc/init.d/apache2 restart
  12. restartcheck=`/usr/bin/php /var/www/vhosts/yourserver.com/httpdocs/vpservercheck.php`
  13. if [ $restartcheck == 1 ]
  14. then
  15. echo "Apache on VPS have been successfully restarted!" | mail -s "Server Alert: VPS have been restarted" sentry@yoursite.com
  16. exit
  17. else
  18. echo "Restart of Apache on VPS has been unsuccessful!!!" | mail -s "Server Alert: VPS is in error state" sentry@yoursite.com
  19. exit
  20. fi
  21. exit
  22. fi
  23. exit
  24. else
  25. /etc/init.d/psa stop
  26. /etc/init.d/psa start
  27. echo "Qmail stopped work, PSA have been restarted" | mail -s "Server Alert: PSA on VPS have been restarted" sentry@yoursite.com
  28. exit
  29. fi