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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ps auxw help me!

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
hey,

I have a game server running on my system. I have multiple games, and i crontab different files every couple minutes to start the games if they arent up already. It seems like my if statement isnt working right though, it keeps restarting every server, even if they are up. Can someone help me with my script? :

#!/bin/sh

process=`ps auxw | grep "mohaa_lnxded +exec server.cfg"?`

if [ -z "$process" ]; then

cd /home/user29/mohaa/
./mohaa_lnxded +exec server.cfg

fi



(I have multiple IP address and servers on my machine, so I have different server.cfg for each user (server1.cfg,server2.cfg, ect). Right now Im only testing the one you see above.


THANKS!!!!!
 

Maybe the ps auxw option is incorrect, please check man ps for correct options. [3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
process=`ps auxw | grep "mohaa_lnxded +exec server.cfg"?`

What is the question mark for?

Also, be wary of passing the results of the ps command through a pipe; sometimes parts are truncated.

You might consider using lock files or pid files the existence of which indicates the process that created them is running. That would be more reliable.
 
You're missing a dash for the [tt]ps[/tt] parameters. Change it to...
Code:
process=`ps [red]-[/red]auxww | grep "mohaa_lnxded +exec server.cfg"`

Also, depending on the OS you're using, you might need to give the full path to [tt]ps[/tt]. On solaris it would be...
Code:
process=`[red]/usr/ucb/[/red]ps [red]-[/red]auxww | grep "mohaa_lnxded +exec server.cfg"`
 
I think I'd use:

process=$(ps -ef |grep "mohaa_lnxded +exec server.cfg")
if [ -z "$process" ];
then
echo "not running"
else
echo "running"
fi
 
There is always a big danger in using
Code:
ps -ef|grep <name>
to check whether <name> is running because you may, or may not, pick up the grep command. There are a number of options including
Code:
ps -ef | grep <name> | grep -v grep
or
Code:
ps -ef | awk '/<name>/ && ! /grep/ {print}'
The awk variant is useful if you want to pick out various fields - I've seen
Code:
proc_id=$(ps -ef | grep <name> | grep -v grep | awk '{print $2}')
which would have been much better as
Code:
proc_id=$(ps -ef | awk '/<name>/ && ! /grep/ {print $2}')

Ceci n'est pas une signature
Columb Healy
 

feherke,

About the truncating. I've seen it happen were the output of a command like "ps auxw" look normal in the command window, but when passed through a pipe to a command, parts of the right side of the output of the "ps auxw" are cut off which means that not all of the text seen in a normal command window will be seen through the pipe. Getting bit by this is no fun because it seems to defy logic. That's why I gave my caution about it. My only guess as to why it happens on some *nix machines is that an environmental variable may not set properly, but that is just a guess; when I was struggling with the problem, I could not find a solution and had to opt for a more reliable method of accomplishing the goal such as using lock files with the Bash/sh/ksh shell 'trap' facility to indicate the process is runnning as well as to make sure those lock files go away when the running process goes away. Since I support a variety of *nix flavors, I want the solution that works best with all of them and not just with a select group.

ZaSter


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top