LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What does kill -0 PID do? (http://www.linuxquestions.org/questions/linux-newbie-8/what-does-kill-0-pid-do-651484/)

asihsh.nakul 06-25-2008 02:50 AM

What does kill -0 PID do?
 
dear all

im struck with question " What kill -0 PID do"

i have check many sites but the answer is same if the PID found it comes back to KONSOLE.... i want detailed answer

please help me

regards
nakul.ashish

antegallya 06-25-2008 04:17 AM

Hi,

according to the man pages :
Quote:

The signals listed below may be available for use with kill. When known constant, numbers and default behavior are shown.
Name Num Action Description
0 0 n/a exit code indicates if a signal may be sent
So, it does nothing, but the exit code of your "kill -0 PID" command just returns 0 if you can send a signal to PID, and returns 1 if you can't (don't have access or invalid PID)

regards,
Antegallya

Tinkster 06-25-2008 04:52 AM

Hi,

welcome to LQ!
Quote:

i have check many sites but the answer is same if the PID found it comes back to KONSOLE.... i want detailed answer
But reading the man-page for kill could have saved you hours
of searching (or posting, for that matter) ;) ...

Quote:

Code:

man kill
SIGNALS
      The  signals  listed  below  may  be available for use with kill.  When
      known constant, numbers and default behavior are shown.

      Name    Num  Action    Description
      0          0  n/a      exit code indicates if a signal may be sent




Cheers,
Tink

asihsh.nakul 06-26-2008 02:34 AM

Thanks but i need more
 
Hi

dude thanks for the replying,

but as my training going on (system admin) so this I've already submitted to my TL can find some more....

Regards
Ashish Shankar


Quote:

Originally Posted by Tinkster (Post 3194470)
Hi,

welcome to LQ!

But reading the man-page for kill could have saved you hours
of searching (or posting, for that matter) ;) ...





Cheers,
Tink


asihsh.nakul 06-26-2008 02:35 AM

Hi

dude thanks for the replying,

but as my training going on (system admin) so this I've already submitted to my TL can find some more....

Regards
Ashish Shankar

acid_kewpie 06-26-2008 06:04 AM

please note we aren't here to do your homework for you...

dsouza 07-17-2012 12:15 PM

Quote:

Originally Posted by Tinkster (Post 3194470)
But reading the man-page for kill could have saved you hours
of searching (or posting, for that matter) ;) ...

It's needless snarkiness like this that turns off many noobs and non-native speakers of English. If you really wanted to help, you could show him how to use a return code from "kill -0" in a script to determine the existence or non-existence of a process.

There is just a handful of man pages written with the end user in mind. The huge majority are shorthand for fellow coders.

honeybadger 07-17-2012 01:22 PM

man pages for common commands (man 1 i.e man section 1) are understandable to anyone (unless there is a language problems) and I think it should work.
some man pages (eg man 2 or man 3) definately need some technical know-how and for that you can post here.

sharadchhetri 07-17-2012 04:24 PM

Quote:

Originally Posted by honeybadger (Post 4730841)
man pages for common commands (man 1 i.e man section 1) are understandable to anyone (unless there is a language problems) and I think it should work.
some man pages (eg man 2 or man 3) definately need some technical know-how and for that you can post here.

As you are new to linux.keep these things always in practice.

(1) man pages (to know about what commands can do)
(2) check the logs (in troubleshooting)

cheers

acid_kewpie 07-18-2012 02:23 AM

He is NOT new to Linux. This question is FOUR YEARS OLD.

Wim Sturkenboom 07-18-2012 10:19 AM

I don't think that I got any wiser from reading the man page. Probably just me that thinks that I'm always allowed to (may) send a signal to a process. I mean, I can type the command kill -9 pid and press <enter>; the OS might not allow me to kill a process (or whatever), but it will tell me that when I issue the command; so why kill -0 pid first?

Below how I see that it can be used and it's a total waste of cpu cycles.
Code:

kill -0 pid && kill -9 pid
I agree that this thread should not have been woken up, but now it's alive again I like to know why kill -0 pid exists?

acid_kewpie 07-18-2012 05:18 PM

For the reasons above from 4 years ago... :confused:

Wim Sturkenboom 07-19-2012 02:16 AM

I think I finally figured out an example for myself that might be useful (but it took a while)

Code:

kill -0 $1
if [ $? -eq 1 ]; then
  echo "no permission to signal pid $1, script terminated"
  exit
fi
...
...
...
kill appropriate_signal_depending_on_process $1

The dots can be a modification of a config file for a background process; the 'kill' will inform the process that it needs to read the config again.

Any better examples?

chrism01 07-19-2012 07:06 PM

I think its there to differentiate between processes you can't signal because you don't have access/auth eg they are not yours (doesn't apply to root ;) ) and processes you get a failure on because it either doesn't have a handler for that signal, or its incorrectly written.

mfirth 11-14-2012 12:35 PM

Sorry to resurrect this again, but another possible use for "kill -0" (or more likely the system API equivalent) is to check the existence of a process before communicating with it in another way - e.g.

If service X writes its process ID into /var/run/X.pid, then you can use 'kill -0 `cat /var/run/X.pid`' to determine if the process for X is still running.

There are obviously other ways of doing it (e.g. checking for the PID in /proc, or using grep on the output of PS), but they are probably less elegant

Hope this is useful

Michael


All times are GMT -5. The time now is 11:13 AM.