- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
- 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
- Make the file executable
- chmod +x /tmp/test.sh
- run the test script
- What can you run to verify the script is still running?
Any assistance is appreciated.
Thanks
B
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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}