LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2003, 09:17 AM   #1
pilot1
Member
 
Registered: Jun 2002
Location: USA
Distribution: Gentoo, Fedora Core
Posts: 408

Rep: Reputation: 30
Killing a pid in C++


How is it possible to read a pid from a file (/var/run/httpd.pid in this case) and then kill it, in C++?
I found a thread telling how to kill a process in C, but I didn't know if it was the same for C++ and I didn't have a way to read /var/run/httpd.pid anyways.
Hopefully this isn't a stupid question, I'm very new to C++
Thanks!
 
Old 09-20-2003, 09:43 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
something like
Code:
#include <istream>
#include <sys/types.h>
#include <signal.h>

int main()
{
    pid_t pid=0;
    std::ifstream ifs("/var/run/httpd.pid", ios::in);

    ifs >> pid;
    if(pid)
        kill(pid, SIGTERM);
    
    return 0;
}
not tested though, so there might be an error with it.
 
Old 09-20-2003, 09:52 AM   #3
pilot1
Member
 
Registered: Jun 2002
Location: USA
Distribution: Gentoo, Fedora Core
Posts: 408

Original Poster
Rep: Reputation: 30
Code:
test.cpp:8: variable `std::ifstream ifs' has initializer but incomplete type
That's an error I've never seen, and I can't see anything obviously wrong with the code.
Do you know what might be wrong?
 
Old 09-20-2003, 09:59 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
it could be related to the fact i meant #include <fstream> instead of #include <istream> and i meant std::ios::in, try this
Code:
#include <fstream>
#include <sys/types.h>
#include <signal.h>

int main()
{
    pid_t pid=0;
    std::ifstream ifs("/var/run/httpd.pid", std::ios::in);

    ifs >> pid;
    if(pid)
        kill(pid, SIGTERM);
    
    return 0;
}
 
Old 09-20-2003, 10:07 AM   #5
pilot1
Member
 
Registered: Jun 2002
Location: USA
Distribution: Gentoo, Fedora Core
Posts: 408

Original Poster
Rep: Reputation: 30
It works, thanks!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM
killing tail --pid=??? DavidPhillips Linux - Software 1 08-13-2003 10:48 PM
finding a PID and killing it tangle Linux - General 3 01-25-2003 10:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration