All the solutions which are parsing /proc/$PID/stat manually are broken!
A process may contain a space character, and then changes the index of splitting it by whitespace:
python -c "import setproctitle, os, glob; setproctitle.setproctitle('foo bar baz'); [open(fn).read().split()[3] for fn in glob.glob('/proc/*/stat') if not os.path.isdir(fn)]"
import setproctitle, os, glob
setproctitle.setproctitle('foo bar baz')
[
open(fn).read().split()[3]
for fn in glob.glob('/proc/*/stat')
if not os.path.isdir(fn)
]