0

I'm trying to run a shell script via a service. This seems to be working fine and the service is able to call the shell script. The issue is when the wiremock_process.sh is called via the service, it believes that the process is already running. However if I run "ps -ef | grep wiremock | grep -v grep" this is not true. I'm also able to run my shell script and start it just fine without the service

Any help would be appreciated!

journal


-- Logs begin at Thu 2020-10-08 14:43:49 EDT, end at Thu 2020-10-08 14:44:59 EDT. --
Oct 08 14:44:57 vc2coma1313056n systemd[1]: Started Service to start wiremock.
Oct 08 14:44:57 vc2coma1313056n bash[2836]: Wiremock is already running. Please stop the process and try
Oct 08 14:44:57 vc2coma1313056n systemd[1]: wiremock.service: main process exited, code=exited, status=1
Oct 08 14:44:57 vc2coma1313056n systemd[1]: Unit wiremock.service entered failed state.
Oct 08 14:44:57 vc2coma1313056n systemd[1]: wiremock.service failed.

wiremock.service

[Unit]
Description=Service to start wiremock

[Service]
Type=simple
ExecStart=/bin/bash -c "source /apps/wiremock/wiremock_process.sh && wiremock_start"

[Install]
WantedBy=multi-user.target

wiremock_process.sh

#!/bin/sh
#Script to start/stop wiremock process
#Date 08-OCT-2020
wiremock_start()
{
        ## Don't start if wiremock is already running
        if [[ `ps -ef | grep wiremock | grep -v grep | wc -l` -ne 0 ]]; then
                echo "Wiremock is already running. Please stop the process and try again"
                exit 1;
        fi

    echo 'Starting...' 
        ##Start the process
        
        java -jar /apps/wiremock/wiremock-standalone-2.27.0.jar --port 9999 --global-response-templating &

}

wiremock_stop()
{
        ## Check if the  wiremock is stopped
        if [[ `ps -ef | grep wiremock | grep -v grep | wc -l` -eq 0 ]]; then
                echo "wiremock is not running. Please start the process and try again"
                exit 1;
        fi

        PID=$(ps -ef | grep wiremock | grep java | awk '{print $2}')

        kill $PID

        echo "Done"

}


1

1 Answer 1

0

Try changing if [[ ps -ef | grep wiremock | grep -v grep | wc -l -ne 0 ]]; then

to if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 )) then

2
  • this leads to echo "starting..." but i dont see my jar running Commented Oct 9, 2020 at 14:18
  • Oct 09 10:15:56 abc systemd[1]: Started Service to start wiremock. Oct 09 10:15:56 abc bash[13020]: Usage: grep [OPTION]... PATTERN [FILE]... Oct 09 10:15:56 abc bash[13020]: Try 'grep --help' for more information. Oct 09 10:15:56 abc bash[13020]: Starting... Commented Oct 9, 2020 at 14:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.