Can I delete [php] <defunct> processes - linux

I made a PHP script which reads some data and starts a new process with pctnl_fork to do some work with the data. When the child processes are finished they stay in as process. ps aux shows me a list of processes like
demo 32229 0.0 0.0 0 0 pts/0 Z 12:23 0:00 [php] <defunct>
demo 32251 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32284 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32298 0.0 0.0 0 0 pts/0 Z 12:56 0:00 [php] <defunct>
demo 32303 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32316 0.0 0.0 0 0 pts/0 Z 12:23 0:00 [php] <defunct>
Can I safely kill this processes as the parent process is still creating new processes to do some work? I know I should probably start the command piping to stderr (according to this topic Insane crond behavior. keeps making defunct bash processes now it is too late.
The parent process is running in the background.

After doing some research, it appears you'll have to kill the parent process for these defunct child processes to go away.
The most pertinent quote from the link says "You cannot kill a defunct process (a.k.a zombie) as it is already dead."

So as U said,
a command in your PHP, like
$a;
pcntl_waitpid($child_pid, $a, WNOHANG|WUNTRACED);
will do the job.
to me works, just fine !

Related

How do I find the name of a process in linux?

I'm really struggling with how to find processes by name in linux. I'm sure it's probably something simple that I'm missing.
Are you looking for the command ps ?
Here an example
nabil#LAPTOP:~$ ps xua | grep python
rootwsl 327 0.0 0.1 29568 17880 ? Ss Jan30 0:02 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
rootwsl 411 0.0 0.1 108116 20740 ? Ssl Jan30 0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
nabil 106387 0.0 0.0 3444 736 pts/1 S+ 23:26 0:00 grep --color=auto python

How does 'kill -STOP and kill -CONT' work?

I'm facing an issue.
We have a clean script using to clean old files, and sometimes we need stop it for and will start it again later. Like the below processes. We use kill -STOP $pid and kill -CONT $pid in check.sh to control the clean.sh, $pid is all the pids of clean.sh (at there, they are 23939, 25804):
root 4321 0.0 0.0 74876 1184 ? Ss 2015 0:25 crond
root 23547 0.0 0.0 102084 1604 ? S 2015 0:00 \_ crond
root 23571 0.0 0.0 8728 972 ? Ss 2015 0:00 \_ /bin/bash -c bash /home/test/sbin/check.sh >>/home/test/log/check.log 2>&1
root 23577 0.0 0.0 8732 1092 ? S 2015 0:00 \_ bash /home/test/sbin/check.sh
root 23939 0.0 0.0 8860 1192 ? S 2015 0:45 \_ bash /home/test/bin/clean.sh 30
root 25804 0.0 0.0 8860 620 ? S 2015 0:00 \_ bash /home/test/bin/clean.sh 30
root 25805 0.0 0.0 14432 284 ? T 2015 0:00 \_ ls -d ./455bb4cba6142427156d2b959b8b0986/120x60/ ./455bb4cba6142427156d2b959b8b0986/80x
root 25808 0.0 0.0 3816 432 ? S 2015 0:00 \_ wc -l
Once the check.sh stopped clean.sh, hours later, check.sh started clean.sh, but there is a strange thing, after a stop and continue, there is a child process 'ls -d ....', it's still stopping.
Could you tell me if it's caused by wrong use of the signal? And how can I modify it?
ok, same like my description is not clear, my bad English...
Not sure what's the reason, but there is a way to sovle it:
kill -CONT $pid
pkill -CONT -P $pid
This will continue the child process.

Memory usage with high precision

How can I get high precision memory usage per proccess with "ps aux"?
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 3672 1984 ? Ss Dec11 0:07 /sbin/init
root 2 0.0 0.0 0 0 ? S Dec11 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Dec11 0:23 [ksoftirqd/0]
root 6 0.0 0.0 0 0 ? S Dec11 0:00 [migration/0]
...
I need more than 1 digit after point.
Maybe I can format column with %MEM?
Look into the proc filesystem /proc/[pid]/status, /proc/[pid]/statm, /proc/[pid]/smaps.
To get fully detailed memory map /proc/[pid]/maps
Read the proc(5) manual page for all the details.
the ps command have that. you can type man ps for detail.
when the termianl show info after you typed it, you can type /memory , then heighlight contain 'memory''s string. you can type n show next palace with memory.

How to detect system power events such as suspend, hibernate, etc

I want to make a Linux application that send a message to a remote host when a local system (where the application runs) is going to suspend, hibernate or shutdown. I googled how to do this and found acpi_listen but it doesn't work.
Here is the result of ps aux | grep acpi on the system I tested (Ubuntu 10.04 LTS):
root 35 0.0 0.0 0 0 ? S Dec03 0:00 [kacpid]
root 36 0.0 0.0 0 0 ? S Dec03 0:00 [kacpi_notify]
root 37 0.0 0.0 0 0 ? S Dec03 0:00 [kacpi_hotplug]
root 934 0.0 0.0 2048 872 ? Ss Dec03 0:00 acpid -c /etc/acpi/events -s /var/run/acpid.socket
Communicating with acpid directly via a UNIX domain socket wouldn't work since that's the way acpi_listen works, although I didn't try it. Is there any other way?
Listen for them over D-Bus.

How to find the real owner of a process after su

We have a convention whereby developers get into a server with their own username, and then sudo su - django where django is the user our apps run under.
I need to find out which developer is running a script as django. With ps faux :
root 26438 0.0 0.0 90152 3320 ? Ss 10:38 0:00 \_ sshd: fred [priv]
fred 26444 0.0 0.0 90152 1852 ? S 10:38 0:00 | \_ sshd: fred#pts/0
fred 26445 0.0 0.0 66052 1560 pts/0 Ss 10:38 0:00 | \_ -bash
root 27923 0.0 0.0 101052 1336 pts/0 S 10:46 0:00 | \_ su - django
django 27924 0.0 0.0 66188 1752 pts/0 S 10:46 0:00 | \_ - bash
django 31760 0.0 0.5 227028 42320 pts/0 S+ 11:10 0:01 | \_ python target_script.py
I can easily see what fred is up to. However I need to write a script to act on this info, and I can find no way to pull out "fred" and "target_script.py" in one line with ps ... euser,ruser,suser,fuser all say "django." Will I need to fumble through this ps faux output to get the info I need?
I found this old post when trying to find the same basic information. The easiest way I found was to use the "loginuid" file under /proc/[pid]. For example:
cat /proc/${processid}/loginuid
Sorry for resurrecting such and old post, but maybe someone will find it useful.
You used su - django. The " - " will make the new shell a login shell (see manpage of su), which let the child process forget its parent uids. That's why euser,ruser,suser,fuser all say "django".
So yes, you may have to fumble through the parent process id, or through "ps faux".

Resources