kill - Get return value of process by PID - Unix & Linux Stack Exchange


本站和网页 https://unix.stackexchange.com/questions/87753/get-return-value-of-process-by-pid 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

kill - Get return value of process by PID - Unix & Linux Stack Exchange
Stack Exchange Network
Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
s-popover#show"
data-s-popover-placement="bottom-start" />
Loading…
Tour
Start here for a quick overview of the site
Help Center
Detailed answers to any questions you might have
Meta
Discuss the workings and policies of this site
About Us
Learn more about Stack Overflow the company
Business
Learn more about our products
current community
Unix & Linux
help
chat
Unix & Linux Meta
your communities
Sign up or log in to customize your list.
more stack exchange communities
company blog
Log in
Sign up
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Home
Public
Questions
Tags
Users
Companies
Unanswered
Teams
Stack Overflow for Teams
– Start collaborating and sharing organizational knowledge.
Create a free Team
Why Teams?
Teams
Create free Team
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Get return value of process by PID
Ask Question
Asked
9 years, 4 months ago
Modified
9 years, 4 months ago
Viewed
6k times
If I have the PID of a process that was killed, how might I get the return status of the process?
I am particularly interested to know if the return status was 143 (kill) or 137 (kill -9) as I don't know what is killing the process. It is a Python script being run from a PHP script on a server that I don't have root access for. The Python script runs for a few minutes, but PHP is limited to running for 30 seconds. PHP has facility to get the return status with exec() however that won't work if the PHP script has timed out!
killpid
Share
Improve this question
Follow
asked Aug 22, 2013 at 8:28
dotancohendotancohen
14.8k2424 gold badges7878 silver badges114114 bronze badges
Add a comment
5 Answers
Sorted by:
Reset to default
Highest score (default)
Date modified (newest first)
Date created (oldest first)
This is almost certainly determined by (the default) php.ini or equivalent setting:
max_execution_time = 30
TLDR: if you are certain that PHP is really exiting first, and something is killing your script, then you can either use daemon to wrap and monitor the Python process (in non-restarting mode, i.e. without --respawn), or add signal handers to the Python script.
In general, if you are able to run a shell as the userid of the script, then you should be able to strace or truss the process, e.g. on linux you can do this reasonably effectively:
$ sleep 60 &
[1] 10873
$ strace -e trace=signal,process -p $!
Process 10873 attached - interrupt to quit
+++ killed by SIGKILL +++
Process 10873 detached
The sleep process was terminated with a kill -9 from another terminal. kill -9 would not be common though, since a process would not be able to trap this and cleanly exit.
To monitor with daemon use a variation on:
daemon -i --errlog=/tmp/mypy.log -- /usr/bin/python [...]
This will log any signal related termination. Add --dbglog and --debug=2 if you want to log every exit regardless.
As noted elsewhere if the process has already terminated, it's gone. Only the parent (or init) would have been able to obtain its exit code, and unless it was logged (possibly using process accounting or auditing), the exit code is lost.
Internally for timeout handling on *nix platforms, PHP sets up a SIGALARM or SIGPROF with the specified timeout. That signal handler simply calls the internal zend_error() function. It does however also call any registered callbacks and error handlers, which may be of use to you.
If the default error handler kicks in, the PHP exit code will be, I believe, 255, since a timeout is an E_ERROR.
Also note, the convention of an exit code as 128+N, where N is the signal number, is due to the behaviour of certain shells (including bash) and many APIs. The actual process exit code after a signal is system dependent. It is probably just the signal number, this is generally the case on Linux. The wait() group of system calls provide better details of the process exit. PHP follows the shell convention, e.g. if you are using popen() and pclose() you will see 128+N as the exit code when a signal terminated the process.
Another consideration here is the behaviour of PHP time limits, if you check the set_time_limit() documentation you will see
The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
You can prove this with a short script:
<?php
# for (;;);
$exe="sleep 20";
print "exit code: " . pclose(popen($exe, 'r'));
?>
Invoke with time php -d max_execution_time=5 -f sleep3.php.
The script will run for 20 seconds, there will be no error. If you kill the sleep process, e.g. kill -USR1 $(pgrep sleep) in another terminal, you will see an exit code of 138 (128+SIGUSR1).
If you uncomment the infinite for(;;) loop, the script will terminate after 5 seconds.
Share
Improve this answer
Follow
edited Aug 23, 2013 at 11:31
answered Aug 22, 2013 at 11:10
mr.spuraticmr.spuratic
9,4912424 silver badges4141 bronze badges
The status reported to the parent process (via the wait system call family) encodes the 8-bit value passed to _exit if the process exited normally, the signal number if the process was killed by a signal, and a few more flags. I don't know how PHP reports it, but typically it wouldn't be “just the signal number”, it would be the signal number bit-shifted and with other bits set.
– Gilles 'SO- stop being evil'
Aug 23, 2013 at 7:26
Add a comment
A process that has been killed no longer exists on the system. In fact, on a busy host, there's a pretty good chance that a long-running and since killed process has had its PID reused by some other process, so you'd end up looking at something totally unrelated!
When a process gets killed, it does not return anything. It simply ceases to exist. This in contrast to a catchable signal such as HUP or USR1, which the process can (but does not necessarily) catch and handle in any way it sees fit (including exiting with a defined exit code).
If you have sufficient access, you might be able to install some sort of library or kernel module which intercepts and records the syscalls which ultimately result in the process in question receiving a signal, but it does seem easier to as pjc50 suggested just add a wrapper script and record the exit status somewhere.
An alternative would be to not make the user wait but rather move to an asynchronous architecture for whatever you are doing that takes so long to complete.
Share
Improve this answer
Follow
edited Apr 13, 2017 at 12:36
CommunityBot
answered Aug 22, 2013 at 9:57
useruser
27.2k1212 gold badges7373 silver badges138138 bronze badges
“When a process gets killed, it does not return anything.” True but irrelevant. When a process gets killed, the signal number is reported over the same channel that reports the exit status for a process that exits normally.
– Gilles 'SO- stop being evil'
Aug 22, 2013 at 21:53
@Gilles I'm not sure whether that's irrelevant, actually. (It might be, but not necessarily for the reason you are implying.) Note the phrasing of the question: "If I have the PID of a process that was killed, how might I get the return status of the process?" So the process no longer exists by the time the OP wants its return status, meaning there's nothing left to hook on to or query. The past tense form "was" makes this a very different question than, say, "how can I get the return value of a process that runs longer than the program invoking it?".
– user
Aug 23, 2013 at 7:18
The “return status” of a process can mean three different things: the 8-bit value passed to _exit (only if the process exited normally), the status reported by wait and family (which encodes the value passed to exit if the process exited normally, the signal number if the process was killed by a signal, and some more flags), or the condensed 8-bit status reported by shells in $? where signals are converted to values above 128. By the numbers in the question, the asker was talking about $?, but needs to be educated about how it's made.
– Gilles 'SO- stop being evil'
Aug 23, 2013 at 7:23
Add a comment
You want waitpid(), but I don't know if that's available in PHP. Don't forget to add WNOHANG if you don't want it to block. However, that only works if the PID is of a child of the calling process.
It might be easiest to put a wrapper script around the python script that records its termination status in a log file somewhere.
Share
Improve this answer
Follow
answered Aug 22, 2013 at 8:57
pjc50pjc50
3,0061717 silver badges1212 bronze badges
Add a comment
Getting the exit status if PHP is no longer the parent process is simple. Wrap the Python script in a BASH script to record the exit status in a file. For example:
#!/bin/bash
false # false is a program that always has an exit status of 1.
echo $? > exit_status.log
Will put '1' into exit_status.log. Just replace false with your actual Python script.
Triggering the script to run as disowned non-blocking process in the background from PHP is more difficult. Consider triggering the process with a Cron schedule and avoid PHP all-together!
Otherwise, a possible solution is to call the script using at. Here is a complete solution:
From PHP call:
<?php
exec("at -f commands now");
In the file, commands:
sleep 10 # Do nothing for 10 seconds
false # Return status 1
echo $? > exit_status.log
If you do not have root access and at is not installed, then compile it locally into your home directory, by using a something like ./configure --prefix=$HOME/local. Otherwise, ask the administrator to install the package.
Further reading: man at, man cron
Share
Improve this answer
Follow
edited Aug 22, 2013 at 10:06
answered Aug 22, 2013 at 10:00
Damien BezborodowDamien Bezborodow
58411 gold badge55 silver badges1111 bronze badges
Wouldn't atd still need to be running outside of the web server's process and security context, though?
– user
Aug 22, 2013 at 10:48
Thank you. It was difficult to pick an 'accepted' answer here!
– dotancohen
Aug 25, 2013 at 12:39
Add a comment
Another alternative:
From PHP:
exec("./wrapper.sh");
In wrapper.sh:
#!/bin/bash
./wrapperlogger.sh >/dev/null 2>&1 &
In wrapperlogger.sh:
./yourPythonScript
echo $? > exit_status.log
The first wrapper backgrounds the process. The second wrapper monitors for the exit code and logs it to a file. Be careful that this script will overwrite the same log file each time it is executed.
Share
Improve this answer
Follow
answered Aug 22, 2013 at 10:25
Damien BezborodowDamien Bezborodow
58411 gold badge55 silver badges1111 bronze badges
Thank you. It was difficult to pick an 'accepted' answer here!
– dotancohen
Aug 25, 2013 at 12:42
Add a comment
Your Answer
Thanks for contributing an answer to Unix & Linux Stack Exchange!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.
Draft saved
Draft discarded
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit
Post as a guest
Name
Email
Required, but never shown
Post as a guest
Name
Email
Required, but never shown
Post Your Answer
Discard
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged killpid or ask your own question.
The Overflow Blog
I spent two years trying to do what Backstage does for free
The complete guide to protecting your APIs with OAuth2 (part 1)
Featured on Meta
Navigation and UI research starting soon
How do we want to handle AI-generated answers?
Related
How to kill both process and subprocess?
14
Graceful way to kill process
Cannot kill a java process
12
Obtaining PID of command earlier in Pipeline
How can I rejoin a process running
Kill a running shell script, and kill all process running within it
PID of a bash process not captured by $!
Kill job in subshell with set -m
KILL process with input as process name and time
Dynamic changing PID - nohup process not killable - Linux
Hot Network Questions
What does it mean by "maximum likelihood estimation (MLE) problem is unbounded"?
Subordinate clause with "que" and subject change, still no Subjuntivo?
Why are wire transfers not reversible by the bank?
CGAC2022 Day 23: North Pole Railroads
8086 Segment Address to Linear
Can ICE tables give two answers?
The World Cup final conspiracy
Property of Antiderivatives
Would a living being moving at 99% the speed of light see time stop, or would it see time move faster?
Why is it "need" in "it need not be thus" instead of "needs"?
Why does stripping metadata decrease the duration of my MP3 file by 0.03 seconds?
Adding linear penalties for multiple assignments
What was the second platform supported by SAP?
Where does the name 'D-Day' come from?
Why can't I do two greps after a tail?
Why would a molten alloy need to be jettisoned into low-to-medium orbit to cool as part of the manufacturing process?
Where would the first nuke have been dropped in Germany?
Table row to fill entire page
sed to search and replace string from another file
How does the kernel know it's resuming from hibernation, not booting?
Mulberry bleeding for a month
Keep elements in sequence that have a letter repeated at least 3 times
RISC-V Zero Instruction Question
How do you add LGBT characters in your story in an organic fashion without coming off as "pandering"?
more hot questions
Question feed
Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Unix & Linux
Tour
Help
Chat
Contact
Feedback
Company
Stack Overflow
Teams
Advertising
Collectives
Talent
About
Press
Legal
Privacy Policy
Terms of Service
Cookie Settings
Cookie Policy
Stack Exchange Network
Technology
Culture & recreation
Life & arts
Science
Professional
Business
API
Data
Blog
Facebook
Twitter
LinkedIn
Instagram
Site design / logo 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2022.12.21.43127
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group. This site is not affiliated with Linus Torvalds or The Open Group in any way.
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Accept all cookies
Customize settings