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:
Test setup:
#!/bin/sh
i=1
while [[ $i -le 1000000]]
do
echo "Still running. Step $i"
((i = i + 1))
done
Any assistance is appreciated.
Thanks
B
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}
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.
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
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}