facing problem in starting a process in background using shell script.

hey all,

i am working on sun solaris machine and i want to start a process in background using shell script (actually i wanna start tomcat server using shell script). please dont tell me that append a & at last because this is not working in the shell script. i have also used nohup and redirected the output to /dev/null but all tries going into waste because every time the process is starting in forground and script hangs at that line:(.

help awaited.

Server daemons have their own conventions for how you start them. Read the tomcat documentation.

but i had tried to start other processes also in background and result was same. have anyone ever tried to start a process in background using shell script. can i start a new shell by shell script?

Yes, somebody has ever tried to start a process in the background. The problem is, it will foreground if it requires tty input. An interactive shell will thus foreground when it wants to read input. If you disable that, you can run it in the background just fine.

sh </dev/null &

(It finishes pretty quickly because it has nothing to do, though. :^)

Servers usually have their own start-up scripts which often require root privileges. Depending on your architecture, look for something like /etc/init.d/tomcat (for a Debian-type system) or /etc/rc/tomcat or some such.

but the processes which i m trying to run by shell script doesnt require any input and still that processes are not going into background.

one more thing i want to say is that, these all processes are going into background successfully when i ran that processes at command prompt directly. the problem in coming when i m trying to run that processes through script.

If it helps you, here I paste one of my scripts where I launch a query script and send CR while it's running for my connection not to timeout. Hope it helps:

#!/bin/ksh
#set -x
# lanzamiento del script de consulta
/opt/bind/usr/sbin/loader.sh $1 $2 &

#   Envio de un CR cada 3 segundos  durante 1,5 minutos
#   para evitar el corte por temporizacion
#
#
PID=$!
#echo $PID
cont=0
while [ `ps -p $PID|grep $PID|wc -l` -eq 1 -a  $cont -lt 30 ]
  do
    echo
    sleep 3
    cont=`expr $cont + 1`
  done

#   Verificacion de si ha temporizado o no el proceso
#
#
if [ `ps -p $PID|grep $PID|wc -l` -eq 1 ]
   then
   	kill -9 $PID
        sleep 2
        echo "ERROR: La consulta no se ha completado correctamente"
        exit 1
   else
        echo "Consulta realizada con exito"
	exit 0
fi

thanx for the script!!!

as long as i understand, it seems that this is calling another script in background but i wanna ask one thing here is that this called script is an infinite script or it return after some time. and are you sure that this called script in going into background?

one more thing, i think there can be difference in running these script on unix machine and solaris machine? is this true?

reply awaited.

Hi there,
The script I'm calling is not infinite but it can hang so it won't finish. That's why I force the timeout.
About the background, you made me doubt, as far as I know it's running in background but maybe someone more experienced could help us.
About the machine, mine is a Sun Ultra-60 sparc with Solaris 8 so I guess there's no problem
Regards!

As a side remark on stylistic issues, this

if [ `ps -p $PID|grep $PID|wc -l` -eq 1 ]

is better expressed simply as

if ps -p $PID | grep $PID >/dev/null

because grep will return true if there was a match, and false otherwise.

The redirection to /dev/null is because grep prints the matching line, which you don't want in this case. Many grep implementations also have a -q option which does the same thing.