|
 |
|
 |
 |
12-19-2002, 07:28 AM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
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 =)
__________________
|
|
|
12-19-2002, 07:29 AM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
if it makes any difference, the server is running rh7.2
__________________
|
|
|
12-19-2002, 10:23 AM
|
#3 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
|
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.
__________________
|
|
|
12-19-2002, 10:53 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
thanks, i will give it a try tonight..
__________________
|
|
|
12-19-2002, 04:01 PM
|
#5 (permalink)
|
|
|
killall ?
__________________
|
|
|
|
12-19-2002, 11:01 PM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
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.
|
|
|
12-20-2002, 06:43 AM
|
#7 (permalink)
|
|
|
Yeah technoboard's thing is awesome.. 
__________________
|
|
|
|
12-20-2002, 07:51 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
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
__________________
|
|
|
12-20-2002, 12:05 PM
|
#9 (permalink)
|
|
Centurion Nova Prime
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 284
|
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.
__________________
|
|
|
12-20-2002, 01:39 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,397
|
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.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:08 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|