-1

I'm running a script called RecordProcessor.sh and I'm writing another script to check whether RecordProcessor.sh is running or not.Even though that process is running, if i do grep I'm not able to find the process running .

My script is as follows:

ps -U $LOGNAME | grep "RecordProcessor.sh"

I did everything even like

ps -U $LOGNAME | grep -v grep | grep "RecordProcessor.sh"

I can only find in which bash it is running not the actual file name.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Anirudh S
  • 39
  • 7
  • 1
    Run your script. Then type `mypid=$$` (as the very next command). That will save the process ID for your script in `mypid`. Then check if it is running with `ps $mypic`. – David C. Rankin Aug 26 '19 at 06:57
  • What about using a supervisor, you could use for example immortal and run only once your script and restart if required: https://immortal.run/post/retries/ – nbari Aug 26 '19 at 07:31
  • 2
    Possible duplicate of [Linux command to check if a shell script is running or not](https://stackoverflow.com/q/16828035/608639), [How to check if another instance of my shell script is running](https://stackoverflow.com/q/16807876/608639), [Linux/Unix command to determine if process is running?](https://stackoverflow.com/q/9117507/608639), [Check if program is running with bash shell script?](https://stackoverflow.com/q/7708715/608639), [Check if a process is running using Bash](https://stackoverflow.com/q/29260576/608639), etc. – jww Aug 26 '19 at 12:55

1 Answers1

1

You can use the following:

if pidof -s RecordProcessor.sh &>/dev/null;then 
   echo "process exists"
else
   echo "no such process"
fi

Or using grep

if ps -aux -U $LOGNAME | grep RecordProcessor.sh |  grep --quiet -v "grep";then 
   echo "process exists"
else
   echo "no such process"
fi
mik1904
  • 1,335
  • 9
  • 18