Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Systems > Linux / BSD / OS X
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 12-19-2002, 07:28 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
killing a process by name?

i would like to be able to write a script that kills a process somehow by a name rather than a PID.

for example, i use this command to start my game server:

./hds_run -game cstrike -port 17016

sometimes i would like to restart the game server when i don't have shell access ( like from work behind our firewall ), so i want to make a web control panel to stop the server. the php would just execute a file on the server which kills the hlds_run program.

does this make sense?

i may be going about askin the question wrong, but give me any tips out there.. i'd appreciate it =)
__________________
sde is offline   Reply With Quote
Old 12-19-2002, 07:29 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
if it makes any difference, the server is running rh7.2
__________________
sde is offline   Reply With Quote
Old 12-19-2002, 10:23 AM   #3 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
technobard is on a distinguished road
Well, killing by "name" is easy. What you're really talking about is creating a script (with some "name") that kills your process. The script would do something like:

kill -9 `ps -ef |grep "hds_run game" | grep -v grep | awk -F" " '{print $2}'`

Now if you wanted a generic script that could handle anything, you could substitute "hds_run_game" with a commandline variable that you pass in. The only danger in this approach is that if your grep returns a process that you didn't want to kill or someone got into your web control panel, you'd have a problem.

Hope this helps.
__________________
technobard is offline   Reply With Quote
Old 12-19-2002, 10:53 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
thanks, i will give it a try tonight..
__________________
sde is offline   Reply With Quote
Old 12-19-2002, 04:01 PM   #5 (permalink)
anon
 
Posts: n/a
killall ?
__________________
  Reply With Quote
Old 12-19-2002, 11:01 PM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
To use killall you need the exact name, or starting part of it..

Meaning if you have the program running, but in the process list it is named /some/path/program, then using killall program wont kill it.

And if you have another program running ie, program to kill is running as myprogram1, ansd another one is named myprogram11, then using killall myprogram1 will kill them both.

So in teh long run, you would need to atleast use ps once, so I would use what technobard sugests.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-20-2002, 06:43 AM   #7 (permalink)
anon
 
Posts: n/a
Yeah technoboard's thing is awesome..
__________________
  Reply With Quote
Old 12-20-2002, 07:51 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
well here is the problem .. we run 3 games server programs on the same machine running on different ports.

here's how they start:

./hlds_run -game cstrike -port 27015
./hlds_run -game cstrike -port 27016
./hlds_run -game tfc -port 27018

of course they are all in different directories. So the problem with the suggestion above is what redhead pointed out. all servers will shutdown. could i use the port prameter in the grep?

-----
there used to be a special way of starting the process and assigning a name to it. so i could type "kill hlds_1" or "kill_hlds_2". that was a long time ago and i totally forgot how, but i know it exists.
--------

another possibility is that i can start it in a screen with a name, but i don't know how to make a script open and kill the process in the screen.

for example, to start the game in screen i would do something like this: [SHELL]$ screen -A -m -d -S hlds_1 ./hlds_run -game cstrike[/SHELL]this is cool because it automatically starts the process in a screen and detaches it. when i need to call up the process, i type [SHELL]$ screen -r hlds_1[/SHELL] . so now i wonder if i can kill the process in the screen named hlds_1
__________________
sde is offline   Reply With Quote
Old 12-20-2002, 12:05 PM   #9 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
technobard is on a distinguished road
Hey, sde.

I don't know about the screen thing, but you can put whatever you want in the grep. Another option is to start your jobs in the background:
(./hlds_run -game cstrike -port 27015 &)
...

Immediately after putting a job in the background, the process id is available in the shell variable $!
So you can do:

echo $! > hds_run1.pid

Your kill script would then do something like:

mykill hds_run1 ( or hds_run2, etc.)

mykill would look for the file name.pid, then do something like:
kill -9 `cat name.pid`

This should solve the problem of accidently killing other processes. If you include enough in grep to make it unique, you can accomplish the same thing, but the name.pid solution is probably a more generic solution.
__________________
technobard is offline   Reply With Quote
Old 12-20-2002, 01:39 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
thanks.. i think that gives me enough to go on. i didn't realize i could just use grep with all the parameters of the process too. a grep including the port will do exactly what i want.
__________________
sde is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PERL, mySQL, and fork() ? Talleyrand Perl 1 04-09-2003 09:09 PM
Running Programs From In C++ paul Standard C, C++ 5 03-23-2003 10:22 AM
checking if a process exists sde Linux / BSD / OS X 5 02-28-2003 04:22 AM
Using Includes to streamline the design process sde PHP 0 02-16-2003 11:49 PM


All times are GMT -8. The time now is 09:08 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle