0

I have written the following shell script:

#!/bin/ksh
main()
{
    echo 'Checking if process is running...'
    if [ [ps -ef|grep "Djava.security.auth.login.config"|grep -v grep|wc -l] != 0 ]; then
            echo "startEventProcessorScript is not running"
            nohup /apps/fasigw/test/bin/startEventProcessor &
    fi

    echo '******************************************************************'
    echo ' Script started successfully '
    echo '******************************************************************'
}
main $*

When I am executing the above script, I get the following error message:

Checking if process is running...
main[3]: test: ] missing
wc: illegal option -- ]
usage: wc [-c|-m] [-lw] [name ...]

I am unable to fix this issue. startEventProcessor is a script that is in path /apps/fasigw/test/bin. Please help

1 Answer 1

3

To get the output of a command within your if statement, you should use command substitution, i.e. use $(...) instead of [..].

if [ $(ps -ef|grep "Djava.security.auth.login.config"|grep -v grep|wc -l) != 0 ]

Note that this can be simplified to:

if [ $(pgrep -cf "Djava.security.auth.login.config") != 0 ]

or :

if ps -ef | grep -q "[D]java.security.auth.login.config"; then

(the brackets in the "[D]java..." is a trick often used to ensure that the grep command does not match its own process).

Sign up to request clarification or add additional context in comments.

Comments

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.