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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script in KSH to check if a process its up or down.
# 1  
Old 01-23-2012
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:
Code:
#!/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!
# 2  
Old 01-23-2012
Notice the space between operators and operands!
Code:
if [ "$psup" -gt 0 ]
then
...
fi

You can try this also...
Code:
pgrep sftp >/dev/null 2>&1
test $? -ne 0 && echo "Process not running..."

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 01-23-2012
Its working perfectly now thanks a lot, ill put more attention to this kind of details.
# 4  
Old 01-23-2012
The if statement:
Quote:
Originally Posted by ARSport
Code:
if ($psup > 0);

is incorrect. It should have been:
Code:
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 pgrep, as in:
Code:
if pgrep sftp
then
  echo "Process SFTP running"
else
  echo  "Process SFTP not running"
fi

# 5  
Old 01-23-2012
I will install pgrep in my server and apply that solution.
# 6  
Old 01-23-2012
Save a process by eliminating the "grep -v grep" and replace that line with this:
Code:
psup=$(ps -ef | grep "[s]ftp" | wc -l)

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

Gary
# 7  
Old 01-23-2012
Thanks Gary!

Thanks Gary, ill apply that change.

It will be a cleaner code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for process in a ksh script

Hi I'm trying to catch a particular process (XYZ) running using a ksh script. Like So.. VarPS=`ps -ef |grep XYZ |grep -v grep` However this seems to find the process of the script itself - not the process 'XYZ' Asked in Error - I found my own typo... thanks anyway Skrynesaver (1 Reply)
Discussion started by: Mudshark
1 Replies

2. Shell Programming and Scripting

Script to check process status

Hi Team, I am using redhat 6.4 version server.We have a script which is used to check the process and sends email if the process is not running.If it is running it will continue and do some other operation. I didnot understand below option -z in the if condition.I have tried to... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

3. Shell Programming and Scripting

ksh script to check if certain process is running

I have to kill the process "test" for a maintenance I do but want the script to check when it comes back up. I can get what I want when I run this while loop: while true;do ps -ef | grep test | grep -v grep | sed -e 's/^*//';sleep 60;done but I want the script to do it for me and as soon as... (6 Replies)
Discussion started by: seekryts15
6 Replies

4. Shell Programming and Scripting

binaries PATH check in a ksh script

The situation is a data center with around 800 servers. Each server has installed one of these unix-like OS: rhel, sunos, aix, hp-ux. And we have to make scripts general enough to being able to be executed over all these servers. Furthermore, sometimes the scripts will be executed as a... (1 Reply)
Discussion started by: asanchez
1 Replies

5. Shell Programming and Scripting

Script to check running of process

Hi, Can anyone please tell me how to write a shell script to check whether a process if running or not.... if its still running then wait for sometime and if not then run the next query. Also, Under my one main script main.sh I have to run 2 scripts simutaneously which take some time to... (2 Replies)
Discussion started by: lovepujain
2 Replies

6. Shell Programming and Scripting

ksh script to process grep output

Hi, I would like to know how can i pipe the following output of grep into a predefined output format This is the output of the grep command grep record *.txt | sort -r 2010-04-28-11-12-21.txt:C The user has created a record 2010-04-29-10-18-41.txt:U The user has updated a record... (8 Replies)
Discussion started by: alienated
8 Replies

7. Shell Programming and Scripting

Ksh Script to get the start minute of n number of process

How can i write a script.? which lists all X process and gets the start minute of each of them. thanks (1 Reply)
Discussion started by: Anteus
1 Replies

8. Shell Programming and Scripting

How do I write a ksh script that will check if all ftp files are received?

I am trying to code a ksh script that will check to see if all 26 incoming ftp files have been received before proceeding to the next function, which is to rename each file. Here is the pseudo-code of what I am trying to do: <<STEP_1>> IF all ALS files have been transmitted then... (2 Replies)
Discussion started by: doug145
2 Replies

9. UNIX for Dummies Questions & Answers

Script to check process completion

Hi, OS - Unix Iam doing the following: after login to the unix box 1. change directory 2. run a shell script "preinstall.sh" 3. This takes apprx 5 mins 4. after which i use to change permission of a file "installhub.sh" (this file is generated from the previous step). Is there anyway... (2 Replies)
Discussion started by: kenkanya
2 Replies

10. Shell Programming and Scripting

script to check if process is running

How do I make a shell script to see if a certain process is running. The process shows up on ps aux as /usr/sbin/daemon eg: if /usr/sbin/daemon else #nothin basically i want to run the process if it isnt running/ has been stopped. Thanks. (2 Replies)
Discussion started by: daydreamer
2 Replies
Login or Register to Ask a Question