daemon - bash script to monitor myself -
i need develop shell script not started if instance of them self running.
if build test.sh monitors need know if running , abort, otherwise (if not running) can run
#!/bin/bash loop() { while [ 1 ]; echo "run"; #-- (... omissis ...) sleep 30 done } daemon="`/bin/basename $0`" pidlist=`/usr/bin/pgrep $daemon | grep -v $$` echo "1:[ $pidlist ]" pidlist=$(/usr/bin/pgrep $daemon | grep -v $$) echo "2:[ $pidlist ]" echo "3:[ `/usr/bin/pgrep $daemon | grep -v $$` ]" echo "4:[" /usr/bin/pgrep $daemon | grep -v $$ echo "]" if [ -z "$pidlist" ]; loop & else echo "process $daemon running pid [ $pidlist ]" fi exit 0;
when run above script first time (no previous instances running) output:
1:[ 20341 ] 2:[ 20344 ] 3:[ 20347 ] 4:[ ]
i cannot understand why 4th attempt not return (as expected). what's wrong in script? have redirect output of 4th command on temporary file , query file in order decide if can run (or not) loop function?
thanks me!
sub-shells...the first 3 run in sub-shells , hence $$
has changed pid of sub-shell.
try using:
pid=$$ pidlist=`/usr/bin/pgrep $daemon | grep -v $pid` echo "1:[ $pidlist ]"
etc. since value of $pid
established before sub-shell run, should same of commands.
is process going popular enough other people want run same daemon on machine? maybe never have multiple users on machine, remember else might wanting run command too.
Comments
Post a Comment