Does it say "no such PID" or is there an error, - as in does this work?
kill -TERM -- -GPID
Also note, as per (emphasize mine)
man 1:
"[…] When an argument of the form '-n' is given, and it is meant to denote a process group […]"
man 2:
"[…] If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid. […]"
man 3:
"[…] If pid is negative, but not -1, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the absolute value of pid, […]"
As in, not PID but process group ID.
Else perhaps you can have so fun with /proc/[pid]/stat
ppid: awk '{gsub(/\([^)]+\)/,"_"); print $4}' /proc/3955/stat
pgrp: awk '{gsub(/\([^)]+\)/,"_"); print $5}' /proc/3955/stat
pkill -TERM -g PGRP
One approach could be to use PID namespaces:
Boot your system with a init=/some/cmd
as kernel parameter, where /some/cmd
forks a process in a new namespace (CLONE_NEWPID
) and runs /sbin/init
in it (it will have PID 1 in that new namespace and pid 2 in the root namespace), then in the parent, execute your "program".
You'll probably want a way to control your program in one way or another (TCP or ABSTRACT Unix socket for instance).
You'll probably want to mlock your program in memory and close most references to the filesystem so that it doesn't rely on anything.
That process won't be seen from the rest of the system. The rest of the system will in effect run like in a container.
If that process dies, the kernel will panic which gives you an extra guarantee.
An inconvenient side-effect though is that we won't see the kernel threads in the output of ps
.
As a proof of concept (using this trick to boot a copy of your system in a qemu virtual machine):
Create a /tmp/init
like:
#! /bin/sh -
echo Starting
/usr/local/bin/unshare -fmp -- sh -c '
umount /proc
mount -nt proc p /proc
exec bash <&2' &
ifconfig lo 127.1/8
exec socat tcp-listen:1234,fork,reuseaddr system:"ps -efH; echo still running"
(you need unshare
from a recent version of util-linux (2.14)). Above we're using socat
as the "program" which just answers on TCP connections on port 1234 with the output of ps -efH
.
Then boot your VM as:
kvm -kernel /boot/vmlinuz-$(uname -r) -initrd /boot/initrd.img-$(uname -r) \
-m 1024 -fsdev local,id=r,path=/,security_model=none \
-device virtio-9p-pci,fsdev=r,mount_tag=r -nographic -append \
'root=r rootfstype=9p rootflags=trans=virtio console=ttyS0 init=/tmp/init rw'
Then, we see:
Begin: Running /scripts/init-bottom ... done.
Starting
[...]
root@(none):/# ps -efH
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 14:24 ? 00:00:00 bash
root 4 1 0 14:24 ? 00:00:00 ps -efH
root@(none):/# telnet localhost 1234
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
UID PID PPID C STIME TTY TIME CMD
root 2 0 0 14:24 ? 00:00:00 [kthreadd]
root 3 2 0 14:24 ? 00:00:00 [ksoftirqd/0]
[...]
root 1 0 2 14:24 ? 00:00:00 socat tcp-listen:1234,fork,reuseaddr system:ps -efH; echo still running
root 204 1 0 14:24 ? 00:00:00 /usr/local/bin/unshare -fmp -- sh -c umount /proc mount -nt proc p /proc exec bash <&2
root 206 204 0 14:24 ? 00:00:00 bash
root 212 206 0 14:25 ? 00:00:00 telnet localhost 1234
root 213 1 0 14:25 ? 00:00:00 socat tcp-listen:1234,fork,reuseaddr system:ps -efH; echo still running
root 214 213 0 14:25 ? 00:00:00 socat tcp-listen:1234,fork,reuseaddr system:ps -efH; echo still running
root 215 214 0 14:25 ? 00:00:00 sh -c ps -efH; echo still running
root 216 215 0 14:25 ? 00:00:00 ps -efH
still running
Connection closed by foreign host.
root@(none):/# QEMU: Terminated
Best Answer
I don't think you can reserve or assign PIDs. However, you could start your process in a script like this:
This creates a "pid file", as some other people have referred to it. You can then fetch that in bash with, e.g.,
$(</tmp/myprocess.pid)
or$(cat /tmp/myprocess.pid)
.Just beware when you do this that if the process died and the pid was recycled, you'll be signalling the wrong thing. You can check with:
See comments if
"$(ps -o comm= -p "$pid")"
looks strange to you. You may want to do a more vigorous validation if there is a chance of someone doing something devious with the content of/tmp/myprocess.pid
(which should not be writeable by other users!).