27

Simple question, in BASH I'm trying to read in a .pid file to kill a process. How do I read that file into a variable. All the examples I have found are trying to read in many lines. I only want to read the one file that just contains the PID

#!/bin/sh
PIDFile="/var/run/app_to_kill.pid"
CurPID=(<$PIDFile)

kill -9 $CurPID
AndyW
  • 275
  • 1
  • 4
  • 8

2 Answers2

50

You're almost there:

CurPID=$(<"$PIDFile")

In the example you gave, you don't even need the temp variable. Just do:

kill -9 $(<"$PIDFile")
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • Welcome to Stack Overflow. Please remember to accept the answer that best solves your problem, by pressing the [checkmark sign](http://i.imgur.com/uqJeW.png). When you see good Q&A, vote them up by using the [gray triangles](http://i.imgur.com/kygEP.png). Happy New Year!! – jaypal singh Dec 30 '11 at 22:39
  • 1
    @JaypalSingh if you are replying to a comment and that person is not the person who wrote the answer, you need to prefix their name at the beginning like I did with you. Otherwise without it, the person who wrote the answer (me) gets notified =) – SiegeX Dec 31 '11 at 01:25
  • Oops sorry about that @SiegeX. – jaypal singh Dec 31 '11 at 01:31
  • @JaypalSingh no worries, appreciate the comment – SiegeX Dec 31 '11 at 01:33
9

POSIX portable way:

$ read pid <$pidfile

See: pid=`cat $pidfile` or read pid <$pidfile?

Community
  • 1
  • 1
gavenkoa
  • 45,285
  • 19
  • 251
  • 303