Forum Moderators: bakedjake

Message Too Old, No Replies

How do you kill a process with a script?

         

mr_evans2u

11:04 pm on May 22, 2003 (gmt 0)

10+ Year Member



I need to write a script that I can call that will kill a certain process and restart it. Below is how I am doing it via command line. As you all know the process number changes everytime it is started up, which is where I'm stuck. I'm not sure how to write a script that will go out and kill the right process and then restart it. Does anyone have a script they would be willing to share?

Thanks

cssmtp51> ps -ef ¦ grep spamd
admin 15065 1 0 16:44:09? 0:02 /usr/local/bin/perl /usr/local/bin/spamd -d -D -L -m 20 -u admin
root 15595 4129 0 17:17:19 pts/2 0:00 grep spamd
cssmtp51> kill 15065
cssmtp51> ps -ef ¦ grep spamd
root 15597 4129 0 17:17:31 pts/2 0:00 grep spamd
cssmtp51> /usr/local/bin/perl /usr/local/bin/spamd -d -D -L -m 20 -u admin
debug: Score set 0 chosen.
debug: running in taint mode? no
unix passed to setlogsock, but path not available at /usr/local/bin/spamd line 194
cssmtp51> ps -ef ¦ grep spamd
admin 15599 1 5 17:17:39? 0:02 /usr/local/bin/perl /usr/local/bin/spamd -d -D -L -m 20 -u admin
root 15601 4129 0 17:17:41 pts/2 0:00 grep spamd

dingman

1:29 am on May 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If there's only going to be one 'spamd' running at a time, you could just have the script do a 'killall spamd' - though you'd probably have to re-write spamd to start with a first line of '#!/usr/local/bin/perl' and call it as "/usr/local/bin/spamd -d -D -L -m 20 -u admin " instead of as an agrument to perl. 'killall perl' is not likely to do what you want...

If you aren't lucky enough to have that work, try writing a script to fork() and then exec() the program you want to kill and restart. The return value from fork() is either 0, -1, or the PID of a new child process. 0 means you're in the child process, and it's time to exec() your new program. -1 means something bad happened. Anything else means that's the pid of the child process you spawned, and you can record that someplace. (/var/run/spamd.pid would be typical) Then next time you script is called, it can read /var/run/spamd.pid, and kill the process with that number.

Of course, I've only ever actually *done* that in C, but I'm pretty sure Perl has wrappers around fork(), as do several other languages.