1

how can i check if a process is running, if it is running then echo "process is running" and keep them from using that process til its finished. i have this piece of code but i cant get it to not allow them to use that process after it echos:

#!/bin/bash
SERVICE=EXAMPLE
if ps ax | grep -v grep | grep -v grep | grep $SERVICE > /dev/null
then
echo -e ""
echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
fi
1
  • 1
    i think you need pgrep Commented Nov 5, 2017 at 4:49

2 Answers 2

1

It seems you want to write a loop, not a single if statement. And you probably want to sleep a bit between checking the condition.

#!/bin/bash

SERVICE=EXAMPLE

while ps ax | grep -v grep | grep "$SERVICE" > /dev/null
do
    echo
    echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
    sleep 60
fi

The condition can be simpler if you have pgrep:

while pgrep "$SERVICE" >/dev/null

(Or the simpler while pgrep -q "$SERVICE" if your implementation of pgrep supports it.)

When there is no matching process (already finished or not started yet), then the script will not produce any output. If you want to get some output in that case, then you can rework like this:

while true
do
    if pgrep "$SERVICE" > /dev/null; then
        echo
        echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
        sleep 60
    else
        echo "Process '$SERVICE' not running"
        break
    fi
fi

To print the message only once and just wait until the process is no longer running:

is_running() {
    pgrep "$SERVICE" > /dev/null
}

if is_running; then
    echo -e "${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
    while true; do
        sleep 60
        is_running || break
    done
else
    echo "Process '$SERVICE' not running"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

@EndzoW it will not output anything when the process is not running. Have you changed the SERVICE=EXAMPLE line to the process name you want to check?
yes, not to be difficult but after it echos "${LIGHTRED}[!] ${WHITE}Please wait till process is finished." how can i just keep them from using that process, kind of like block it until the process is done being used. im trying to achieve having the process being used 1 at a time
@EndzoW I added one more variation, I hope this matches what you were looking for. Let me know if not.
still not exactly what im looking for, sorry for the trouble, i tried to change your code around but still didnt work. it is a little hard to explain what im looking for, do you have somewhere i can contact you?
0

Another solution

#!/bin/bash
tput civis # hide cursor
until ! ps -ef | grep "$SERVICE" | grep -v "grep" > /dev/null; do
    while ps -ef | grep "$SERVICE" | grep -v "grep" > /dev/null; do
        echo -en "\r${LIGHTRED}[!] ${WHITE}Please wait till process is finished."
    done
    printf "\r%80s" "" # clear line
    echo -en "\rProcess '$SERVICE' is completed!\n"
done
tput cnorm # show cursor again

This solution is useful also if you have multiple instance of the service together.

1 Comment

issue with this piece of code is it echos: ${LIGHTRED}[!] ${WHITE}Please wait till process is finished." when the process if running and when its not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.