2

I have this script that I found online to read from a fifo and import it into mysql:

#
# Created by Tadghe Patrick Danu 
#
#!/bin/bash


if [ -e /tmp/mysql.pipe ]; then
while [ -e /tmp/mysql.pipe ]
do
mysql -u syslog --password=mypassword syslogdb < /tmp/mysql.pipe
done
else
mkfifo /tmp/mysql.pipe
fi

I have scheduled this script to run as a cron job and it works, however, every time it runs, it spawns a new instance, so when I look at the processes I see a bunch of processed running the script. How can I make it run only if it is not running already? I would like to have only one instance of this running at a time.

Thanks,

jangeador
  • 594
  • 1
  • 6
  • 17
  • 4
    `cron` is for things that need to be run periodically, not for things that need to run once and stay running. I think you want to start this as a service instead. – chepner Oct 03 '14 at 16:58
  • 1
    The right way to do mutexes in shell is to use `fuser`, but the right way to keep services running has nothing at all to do with `cron`. – Charles Duffy Oct 03 '14 at 23:31

1 Answers1

1

At least two options to do it:

  1. Check if script is already running at the beginning and if yes then exit and not execute.
  2. Use monit utility

https://mmonit.com/monit/documentation

which checks if script is working, if not then start process. But if it should work all time, then just run it as a daemon.

RaFD
  • 688
  • 6
  • 14