VMware Cloud Community
BWinchell
Enthusiast
Enthusiast
Jump to solution

ESXi 5.5 Check if custom script is running

Hello,

I am working on a custom remote office shutdown solution for ESXi environments.  The one area I have run into a problem is I need to check if my shutdown script (running locally on the ESXi host) is actually running or still running (to avoid running duplicate processes).

I know in regular Linux systems I can do stuff like (ps aux | grep "test.sh" | grep -v "grep") or (stat -c %X /proc/pgdump).  This does not seem to work on ESXi.

Recommendations:

  • This has to be run locally from the remote site
    • no access to vCenter or Orchestrator
  • No Windows
    • The main system that will be controlling the overall process is Debian
  • Do not want to install any 3rd party software onto the ESXi hosts
  • Do not recommend N.U.T.S or APCUPSD
  • Have to be able to see if the script is running not the commands within the script
    • The real script is running multiple commands at different times

Test setup:

  1. Create a loop file in /tmp/test.sh

#!/bin/sh

i=1

while [[ $i -le 1000000]]

do

   echo "Still running.  Step $i"

   ((i = i + 1))

done


  1. Make the file executable
    1. chmod +x /tmp/test.sh
  2. run the test script
  3. What can you run to verify the script is still running?

Any assistance is appreciated.

Thanks

B

0 Kudos
1 Solution

Accepted Solutions
BWinchell
Enthusiast
Enthusiast
Jump to solution

Ok.  After spending many hours looking for a way to see which "sh" process is running my script, I gave up and continued down the path of what Alistar suggested.

Solution: (linux - Quick-and-dirty way to ensure only one instance of a shell script is running at a time - Sta...)

Basically we create a lockfile with the PID inside of it.  If simultaneous processes try to kick of, they are stopped.  You can also look into the lockfile to find the PID if you want to force exit, log, etc....

E.g.

#!/bin/sh

LOCKFILE=/tmp/lock.txt

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then

     echo "already running"

     exit

fi

# Remove the lockfile when process is exited

trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT

     echo $$ > ${LOCKFILE}

#do stuff

i=1

while [[ $i -le 1000000 ]]

     do

          echo "Still running.  Step $i"

          let i=i+1

done

rm -f ${LOCKFILE}

View solution in original post

0 Kudos
3 Replies
Alistar
Expert
Expert
Jump to solution

Hi,

what about creating some sort of a log file - let's say script.log where you would update it with "running" while still in the cycle and "done" when it has completed? Via simple echo script_status > /tmp/sh.log, then a simple cat /tmp/script.log should do the trick.

This the easiest solution I can think of.

Stop by my blog if you'd like 🙂 I dabble in vSphere troubleshooting, PowerCLI scripting and NetApp storage - and I share my journeys at http://vmxp.wordpress.com/
BWinchell
Enthusiast
Enthusiast
Jump to solution

Hi Alistar,

The script does log everything to a log file for troubleshooting and audit purposes.  That is one way of doing it. 

If I cannot find another way (something where I can ensure the script is running via a running process) this might be my only solution.

Thanks

B

0 Kudos
BWinchell
Enthusiast
Enthusiast
Jump to solution

Ok.  After spending many hours looking for a way to see which "sh" process is running my script, I gave up and continued down the path of what Alistar suggested.

Solution: (linux - Quick-and-dirty way to ensure only one instance of a shell script is running at a time - Sta...)

Basically we create a lockfile with the PID inside of it.  If simultaneous processes try to kick of, they are stopped.  You can also look into the lockfile to find the PID if you want to force exit, log, etc....

E.g.

#!/bin/sh

LOCKFILE=/tmp/lock.txt

if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then

     echo "already running"

     exit

fi

# Remove the lockfile when process is exited

trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT

     echo $$ > ${LOCKFILE}

#do stuff

i=1

while [[ $i -le 1000000 ]]

     do

          echo "Still running.  Step $i"

          let i=i+1

done

rm -f ${LOCKFILE}

0 Kudos