LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2011, 09:11 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
bash: ps, psgrep and read /proc/$pid make script exit


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.

Last edited by catkin; 03-22-2011 at 09:18 AM.
 
Old 03-22-2011, 09:28 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
SOLVED and taking off the zero-reply list: had set -e in the script
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read call is hanging and ls -l /proc/PID/fd shows broken pipe on file descriptor exceed1 Linux - Server 0 01-22-2011 12:24 PM
pmap or /proc/<pid>smap or /proc/<pid>/status iQoder Linux - Newbie 1 07-16-2009 06:32 PM
Cannot read beyond the first 2^63 bytes in /proc/<pid>/mem yeye_olive Programming 5 05-17-2009 04:06 AM
how to read /proc/pid/status in sched.c linuxdoniv Programming 3 07-21-2008 09:49 PM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration