RHEL / CentOS: Shell Script Wrapper TO Start / Stop / Restart NFSv4 Services In A Particular Order

by on March 19, 2013 · 0 comments

A simple shell script wrapper to start / stop / restart NFSv4 service on CentOS / RHEL v.6.x server in a particular order. This script also demonstrates how to use the ln command to create soft links to the same script and take a different action as per $0.

  1. #!/bin/bash
  2. # A simple shell script wrapper start / stop / restart nfsv4 services on CentOS / RHEL
  3. # based systems.
  4. # Tested on: RHEL / CentOS but can be ported to Debian or other distros.
  5. # ----------------------------------------------------------------------------
  6. # Author: nixCraft <http://www.cyberciti.biz>
  7. # Copyright: 2009 nixCraft under GNU GPL v2.0+
  8. # ----------------------------------------------------------------------------
  9. # Last updated: 20/Mar/2013 - Added support for RHEL 6.x
  10. # ----------------------------------------------------------------------------
  11. # Who am I?
  12. _me=${0##*/}
  13.  
  14. ## RHEL/CentOS init.d script names
  15. _server="/etc/init.d/rpcbind /etc/init.d/rpcidmapd /etc/init.d/nfslock /etc/init.d/nfs"
  16. _client="/etc/init.d/rpcbind /etc/init.d/rpcidmapd /etc/init.d/nfslock"
  17. _action="$1"
  18.  
  19. ## Run either server or client script with the following action:
  20. # stop|start|restart|status
  21. ##
  22. runme(){
  23. local i="$1"
  24. local a="$2"
  25. for t in $i
  26. do
  27. $t $a
  28. done
  29. }
  30.  
  31. usage(){
  32. echo "$_me start|stop|restart|reload|status";
  33. exit 0
  34. }
  35.  
  36. [ $# -eq 0 ] && usage
  37.  
  38. ## Main logic
  39. case $_me in
  40. nfs.server) runme "$_server" "$_action" ;;
  41. nfs.client) runme "$_client" "$_action" ;;
  42. *) usage
  43. esac
  44.  

How do I use this script?

Download and install the script as follows:
wget http://bash.cyberciti.biz/dl/605.sh.zip
unzip 605.sh.zip
mv 605.sh nfs.rc
mkdir ~/bin/
mv nfs.rc ~/bin/
cd ~/bin/
chmod +x nfs.rc
ln -s nfs.rc nfs.server
ln -s nfs.rc nfs.client

To start / stop / restart nfsv4 server services in a single go:
# ~/bin/nfs.server stop
# ~/bin/nfs.server status
# ~/bin/nfs.server start
# ~/bin/nfs.server restart

To start / stop / restart nfsv4 clients in a single go, enter:
# ~/bin/nfs.client stop
# ~/bin/nfs.client start
# ~/bin/nfs.client restart
# ~/bin/nfs.client status

Sample outputs:

rpcbind (pid  2379) is running...
rpc.idmapd (pid 7410) is running...
rpc.statd (pid  2397) is running...


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

Previous Script: