Script in KSH to check if a process its up or down.

Hi all!

Im working on a simple script in KSH (just started) to check if a process its up or down, but im kind of lost with the following error.

Script:

#!/usr/bin/ksh

psup=$(ps -ef | grep sftp | egrep -v grep | wc -l)

if ($psup > 0);
then

   echo "Process SFTP running"

else

   echo  "Process SFTP not runnung"

fi

Error:

./checaproceso.sh[11]: 1: not found.
Process SFTP not runnung

Not sure why is reading the variable like that.

Im a beginer at KSH scripting.

Thanks!

Notice the space between operators and operands!

if [ "$psup" -gt 0 ]
then
...
fi

You can try this also...

pgrep sftp >/dev/null 2>&1
test $? -ne 0 && echo "Process not running..."

--ahamed

1 Like

Its working perfectly now thanks a lot, ill put more attention to this kind of details.

The if statement:

is incorrect. It should have been:

if [[ $psup -gt 0 ]]

(without the trailing semicolon)

What you did was execute the command "1" (which was the value of the variable psup) in a subshell, sending its output to the file "0".

Have you thought of using man pgrep (linux), as in:

if pgrep sftp
then
  echo "Process SFTP running"
else
  echo  "Process SFTP not running"
fi

I will install pgrep in my server and apply that solution.

Save a process by eliminating the "grep -v grep" and replace that line with this:

psup=$(ps -ef | grep "ftp" | wc -l)

The regular expression matches the string "sftp" but not itself.

Gary

Thanks Gary, ill apply that change.

It will be a cleaner code