-2

I am trying to create one script which check for a running process and start it if it is not running.

Here is test.sh

#!/bin/bash
if pgrep infiloop > /dev/null ;
  then
  echo "Process is running."
else
   exec /u/team/infiloop.sh > /u/team/infiloopOutput.txt
  echo "Process was not running."
fi

And infiloop.sh

#!/bin/sh

while true
do
 echo "helllo"
 sleep 2
done

Now when i run the 1st script , it starts the script but after it start it doesn't allow me to run another command.

Output:

[user@host ~]$ ./checkforRunningJob.sh

^C

I have to press Ctrl+C, Ctrl+Z, and once i do that my infinite script also stop.

Could you please check.

Thanks.

Thinker
  • 6,820
  • 9
  • 27
  • 54
  • Does this answer your question? [executing shell command in background from script](https://stackoverflow.com/questions/3683910/executing-shell-command-in-background-from-script) – Aserre Jul 09 '20 at 09:16

1 Answers1

0

You can put the process in the background with &:

#!/bin/bash

if pgrep infiloop > /dev/null ;
then
    echo "Process is running."
else
    exec /u/team/infiloop.sh > /u/team/infiloopOutput.txt &
    echo "Process was not running, started process $!"
fi

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108