Hello
I have not been able to write bash to use a PID file to ensure no other instance of the same script is running! All three methods I can think of to see if the PID in the PID file is another instance of the script make the script exit with a return code of 1 but the same commands run at the command prompt work as expected.
The first attempt was:
Code:
read pid < $pid_afn
pid_command=$( ps --pid $pid -o command --no-heading )
The first attempted workaround was:
Code:
if [[ -d /proc/$pid ]]; then
# /proc/$pid/cmdline is a sequence of NUL-terminated strings
# but bash's read conveniently gets only the first
read pid_command < /proc/$pid/cmdline
fi
The second attempted workaround (with debug to make the following command prompt copy and paste meaningful) was:
Code:
echo "DEBUG: pgrep -f \"^$my_name\""
pids="$( pgrep -f "^$my_name" )"
echo "DEBUG: pids from pgrep: $pids" >&2
Here's the command prompt session, testing with a stale PID file and then manually running the problem command and it behaving as expected:
Code:
root@CW8:~# /opt/LadA/bin/rsync.sh
[snip]
DEBUG: found PID file containing PID 2951
DEBUG: pgrep -f "^/opt/LadA/bin/rsync.sh"
[no "DEBUG: pids from pgrep" message]
root@CW8:~# echo $?
1
root@CW8:~# pgrep -f "^/opt/LadA/bin/rsync.sh"
[no output, as expected]
root@CW8:~#
Huh?
This on Slackware64 13.1 which has bash 4.1.7.
Best
Charles
EDIT: in desperation I tried rebooting but the behaviour was the same.