Re: Safe Way to Tell if Process is Running
Subject: Safe Way to Tell if Process is Running Date: Tue, 04 Dec 2012 13:39:41 -0600 From: Martin McCormick mar...@dc.cis.okstate.edu About 20 years ago, I saw some code in which you verified whether or not a process was running by giving it a kill -0 command. If the process was running, nothing happened to it but your kill -0 command exited with a 0 status. If there was no process with that PID, the kill command exited non-zero. I use this in a system(command); in a C program I wrote some years ago and I think this is now causing a segmentation fault when the process number being signalled doesn't exist. Is there a better way to determine if process number 12345 is running without bothering it? None of the documentation on kill (1) shows a signal 0 nor does kill -l. Something tells me this is a bad idea these days, but I still need an easy way to see if XYZ process is still alive. 'man 2 kill' tells all. ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Safe Way to Tell if Process is Running
Robert Bonomi writes: 'man 2 kill' tells all. I believe that is the first or second time I have used Section 2. I appreciate the reminder. It looks like ps -p ### /dev/null appears to do what I need without producing output ps -p 54321 /dev/null date ran the date command if there was a process with that number and produced nothing if no process 54321 existed. Martin ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Safe Way to Tell if Process is Running
Robert Bonomi writes: 'man 2 kill' tells all. I believe that is the first or second time I have used Section 2. I appreciate the reminder. It looks like ps -p ### /dev/null appears to do what I need without producing output ps -p 54321 /dev/null date ran the date command if there was a process with that number and produced nothing if no process 54321 existed. Martin ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org
Re: Safe Way to Tell if Process is Running
On Tue, 04 Dec 2012 14:50:38 -0600 Martin McCormick mar...@x.it.okstate.edu wrote: Robert Bonomi writes: 'man 2 kill' tells all. I believe that is the first or second time I have used Section 2. I appreciate the reminder. It looks like ps -p ### /dev/null appears to do what I need without producing output ps -p 54321 /dev/null date ran the date command if there was a process with that number and produced nothing if no process 54321 existed. That's not a certain test, ps can miss processes. Given that you are working in C you would be better off calling kill directly rather than spawning a process with system and risking picking up some odd implementation of a command. if (-1 != kill(pid, 0)) { // Process exists } else if (EPERM == errno) { // No permission to signal process - belongs to someone else } else if (ESRCH == errno) { // Process does not exist } else { // Something weird and undocumented went wrong } -- Steve O'Hara-Smith at...@sohara.org ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org