bash script

I have problem saving the PID to a file for my script. Any idea ?

 PID=$(ps -aux | grep '3306:localhost:3306' | grep -v grep 2>/dev/null | awk '{print $2}')
  echo $PID
  echo $PID > $pidfile

------------
pidfile="/var/run/MyScript.pid"
Prog="MyScript"
RETVAL=0

start() {
  if [ -f $lockfile ]; then
  echo -n $"$Prog is already running: "
  echo
  else
  echo -n $"Starting $Prog: "
  echo
  daemon /usr/local/bin/MyScript.sh &
  PID=$(ps -aux | grep '3306:localhost:3306' | grep -v grep 2>/dev/null | awk '{print $2}')
  echo $PID
  echo $PID > $pidfile
  RETVAL=$?
  [ $RETVAL = 0 ] && touch $lockfile
  return $RETVAL
  fi
}

stop () {
  echo -n $"Stopping $Prog: "
  PID=$(ps -aux | grep '3306:localhost:3306' | grep -v grep 2>/dev/null | awk '{print $2}')
  echo $" Killing PID :$PID"
  echo
  #killproc /usr/bin/ssh
  if [ -f $lockfile ]; then
        rm -f $lockfile
  fi
  return $RETVAL
  kill -TERM `cat $pidfile`
}
AXISHKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA Engineer IIICommented:
I tested your command which sets PID and it works.
What value do you see for echo $PID?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
Have you tried using $! to get the PID after the daemon call?

There's no guarantee that the subprocess will have made enough progress for its arguments to be on display in ps if you don't wait a bit, but $! is cleaner if the value returned is the right PID.
0
AXISHKAuthor Commented:
The PID can prompt out in the screen but the file doesn't include anything....
---------------
 pidfile="/var/run/forward.pid"

 daemon /usr/local/bin/MyScript.sh
  PID=$(ps -aux | grep '3306:localhost:3306' | grep -v grep 2>/dev/null | awk '{print $2}')
  echo $PID
  echo $"Starting PID :$PID"
  echo $! > $pidfile

Result
---------
 Killing PID :7468

Starting forwardssh:
Restricted! Authorized access only. All activities would be recorded and monitored, and be used as possible evidence of criminal activity when needed.
bind: Address already in use
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
7536
Starting PID :7536
0
Mark BullockQA Engineer IIICommented:
Do you have permission to write in the file?
Try this:
echo hello > /var/run/forward.pid
cat /var/run/forward.pid

It should only contain hello.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.