0

I am new to the the world of init scripts and still a BASH neophyte. I'm currently in a situation where I could use some guidance.

We run an application that utilizes JBOSS and I need to be able to handle it like a daemon using an init script. This is my first time writing an init script and I understand that often '.pid' files are used to keep track of a process using an init script, however I wonder if they are really needed in my situation.

For example, when I go to use the "stop" function of my init script, I can have it check the .pid file to get the process ID, or I can simply have it get the PID using "checkproc".

Here is an example of what I have written.

JBOSSPID=$(checkproc -v $JBOSS_PROCESS_NAME)

function _stop {
   # Check if JBOSS is running..
   if [ -z "$JBOSSPID" ]; then
      echo "Service is not running.."
      exit
   fi

   # Attempt to shutdown JBOSS gracefully..
   echo "Stopping service.."
   ./stop_jboss.sh

   # Loop until JBOSS PID no longer exists..
   RUNNING=0
   while [ "$RUNNING" == "0" ]
   do
      JBOSSPID=$(checkproc -v $JBOSS_PROCESS_NAME)
      if [ "$JBOSSPID" == "" ]; then
         RUNNING=1
      fi
   done
}

I've wrote my "start" and "status" functions to obtain the PID like this as well. I tested it and it seems to work fine, but I don't know if there is something I'm missing that could go wrong without a .pid file. Are there any reasons for me to scrap this logic and instead utilize a .pid file?

bumped to the homepage by Community 18 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • 1
    You can avoid this hassle if your OS supports upstart jobs (Ubuntu, Centos 6) or Systemd (Debian, Centos 7). – jordanm Nov 9 '15 at 14:28
  • actually, PID files come from sysvinit only starting services and not keeping track of them afterwards. sysvinit is every bit as flexible as upstart or systemd...but its design criteria is much simpler, from a simpler age. PID files exist to help other tools (or the admin on the command line) keep track of and manage daemons after they've been started. – cas Nov 10 '15 at 1:49
0

You can make your init script without a pid file, there will be no problems with it. Unless another user will start a jboss instance manually. I don't think your init script can cope with multiple lines returned by "checkproc -v $JBOSS_PROCESS_NAME"

The pid file is mainly useful when there is a possibility that multiple applications will be started on the same machine. You don't want your init script to stop instances started by other users.

So, instead of killing the first (or all) programs that have a specific string, you save the pid file of the program started from the init script. This way, when you try to kill the program, you will only kill the one started by you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.