Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

It seem's pretty simple but i need

Status
Not open for further replies.

w1nn3r

ISP
Sep 12, 2001
179
US
It seem's pretty simple but i need help writing an if else script that does ps x | grep process, then send's the resulting text to: read proccheck, then if [ $proccheck -eq $null ] then /path/to/process. I have some processes that keep dying and i want a script to keep checking every 10 minutes or so(With the help of cron) and start it if its not running while i debug the process. Any help would be greatly appreciated.
 
Hi:

The options to ps will be unix flavor specific, so you might need to change it:

The following searches the process table for isql and the grep -v eliminates 'grep isql'. This should get you started:

if ps -ef|grep isql|grep -v grep > /dev/null
then
echo "Found the command"
else
echo "No command"
fi
 
olded: you are using SysV'ps please try once:

WHATYOUWANT=qqq
PID='ps -efocomm,pid|sed -e "s,.*$WHATYOUWANT.* ,,"'
printf $WHATYOUWANT
[ x$PID = x ] && printf " not"
printf " found\n"

note the space after $WHATYOUWANT.* and the " in sed
the , in sed in case $WHATYOUWANT contains / -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
# Script
WHATYOUWANT=ircd
PID='ps -efocomm,pid|sed -e "s,.*$WHATYOUWANT.* ,,"'
printf $WHATYOUWANT
[ x$PID = x ] && printf " not"
printf " found\n"

And what i get is,

[redrum@19 redrum]$ ./checkit
ircd./checkit: [: too many arguments
found
[redrum@19 redrum]$

thanks btw :))
 
Hi:

I can't say I like the following example, but it's been done many times before. Save your process id in a file before when executing the script. If you execute the script again before the original script terminates, it'll check ps with the -p option.

This probably won't work for you if you're executing a binary.

# Check for redundent copies of script.ss and abort if they exist.
# use something other than tmp for production.
PROCESS_FILE=/tmp/scriptss.running

#
# If a process file exists, see if the process is still running.
if test -r $PROCESS_FILE
then
PROCESS=`cat $PROCESS_FILE`
else
PROCESS=" "
fi
# if [ ps -p $PROCESS ] > /dev/null 2>&1
if ps -p $PROCESS > /dev/null 2>&1
then
echo "script.ss already running" >> $LOGFILE
exit 1
else
# process is not running so create a new process file containing my pid.
rm -f $PROCESS_FILE
echo $$ > $PROCESS_FILE
fi

echo "starting script.ss process.."
# do some other stuff
read x
echo "script.ss ending"
rm -f $PROCESS_FILE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top