read pid-file and check process

Hey guys ,

i will writa a shell script.

the script to check a process by a pid.

the pid is in a file with the name PID

how i can read the pid from this file ? and then check a process (is run or is not run) ?

can you help me there ?

greetz

Cycrus
 
Hey ,

i have now this here :


Code:
ps -p `cat pid`
if [ $? = 1 ] 
then
echo "Not Running."
else
echo "Running."
fi

in the pid file is the pid 50100 this is the pid from the process.
when i kill the process and run this shellscript with "sh pidtest.sh" i become output "Running" .. but the process is not running when i check with ps ax

what i make wrong ?
 
Should work, in principle, just like:

Code:
ps -p `cat pid` > /dev/null 2>&1 && echo Running || echo "Not Running"
 
It's probably better to use pgrep(1), it has better return codes to check on. It also has an option to read a pid file directly (-F switch). Also check if the pid file actually exist before using it.
 
Hey ,

thanks but is not work with my codes..

[CMD="ps -p `cat /usr/server/pid` > /dev/null 2>&1 && echo "Running" || cd /usr/server/ && ./server &"][/CMD]

ehhm also all is right then ?
output is "running" and he start the server

Mfg
 
Okay now i can test it..

but my crontab is not really running i have type follow commands :

Code:
crontab -l
crontab /etc/crontab
crontab -e

follow line i typed in this file


Code:
*/1	*	*	*	*	root	/usr/sv2/game/ && ./pidtest.sh &

this is for every minute or ?
but it is not start the server -,- when he is not running

then i have made the line so :

Code:
*/1	*	*	*	*	root	/usr/server/game/pidtest.sh > /dev/null

but here the same.. the server not start when he not run.

greetz
 
Yes but now i have this so :

Code:
* * * * * /usr/sv2/game/pidtest.sh

and the script doesn´t start every minute..

the script works.

when i make sh pidtest.sh then comes Running or he starts the server

how i can see my mails ? There is every "you have a new mail"

greetz
 
He not restart the Server when is not running.. -,-

crontab -e
entry :
Code:
* * * * * /usr/pidtest.sh > /dev/null

pidtest.sh :

Code:
cd /usr/server/
ps -p `cat /usr/server/pid` > /dev/null 2>&1 && echo "Running" ||  ./server

is not running the server starts not new

what can i make ?
 
Remove the >/dev/null in the crontab.

The echo will make sure you'll get an email. If you get an email every minute from cron you'll see the output in there. Also look in /var/log/cron for any errors.
 
Back
Top