41

I have a daemon I have written using Python. When it is running, it has a PID file located at /tmp/filename.pid. If the daemon isn't running then PID file doesn't exist.

On Linux, how can I check to ensure that the PID file exists and if not, execute a command to restart it?

The command would be

python daemon.py restart

which has to be executed from a specific directory.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
davidmytton
  • 38,604
  • 37
  • 87
  • 93
  • 1
    The "which has to be executed from a specific directory" part of your description sounds like a recipe for trouble. Beware - rethink if at all possible. – Jonathan Leffler Mar 21 '09 at 18:09
  • duplicate: http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash/ –  Mar 21 '09 at 19:36
  • 1
    /tmp is a bad location to put PID files, since some distributions have cleaner processes that delete files from /tmp, and users may delete files from there to make space. – Douglas Leeder Jul 28 '15 at 09:29

7 Answers7

86
[ -f /tmp/filename.pid ] || python daemon.py restart

-f checks if the given path exists and is a regular file (just -e checks if the path exists)

the [] perform the test and returns 0 on success, 1 otherwise

the || is a C-like or, so if the command on the left fails, execute the command on the right.

So the final statement says, if /tmp/filename.pid does NOT exist then start the daemon.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
eduffy
  • 39,140
  • 13
  • 95
  • 92
18
test -f filename && daemon.py restart || echo "File doesn't exists"
Messa
  • 24,321
  • 6
  • 68
  • 92
harveyD
  • 579
  • 5
  • 17
  • 1
    if only the file needs to be checked for existence : test -f filename && echo "exists" || echo "does not exist" – lode Jun 28 '18 at 23:10
11

If it is bash scripting you are wondering about, something like this would work:

if [ ! -f "$FILENAME" ]; then
   python daemon.py restart
fi

A better option may be to look into lockfile

Brimstedt
  • 3,020
  • 22
  • 32
6

The other answers are fine for detecting the existence of the file. However for a complete solution you probably should check that the PID in the pidfile is still running, and that it's your program.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
5

Another approach to solving the problem is a script that ensures that your daemon "stays" alive...

Something like this (note: signal handling should be added for proper startup/shutdown):

$PIDFILE = "/path/to/pidfile"

if [ -f "$PIDFILE" ]; then
    echo "Pid file exists!"
    exit 1
fi

while true; do
    # Write it's own pid file
    python your-server.py ;

    # force removal of pid in case of unexpected death.
    rm -f $PIDFILE;

    # sleep for 2 seconds
    sleep 2;

done

In this way, the server will stay alive even if it dies unexpectedly.

gahooa
  • 131,293
  • 12
  • 98
  • 101
  • `if [ ! -f "$PIDFILE" ]; then` should actually be `if [ -f "$PIDFILE" ]; then`. I tried to edit it, but the edit is too small for stackoverflow. :/ – logidelic Feb 13 '19 at 21:50
1
ls /tmp/filename.pid

It returns true if file exists. Returns false if file does not exist.

KBIIX
  • 873
  • 9
  • 14
1

You can also use a ready solution like Monit.

Anonymous
  • 3,011
  • 4
  • 24
  • 23