Where do processes place runtime data

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

Type 1: Privileged Processes (i.e. run with sudo)

Unix systems use the /run folder to store sockets, pid files, etc. of privileged processes and daemons. This means any process you run with sudo

By having them all in the one suggested place, things are easier to debug.

ls -l /run/

srw-rw-rw-  1 root root    0 Jun 26  2019 acpid.socket
-rw-------  1 root root    0 Jun 26  2019 agetty.reload
-rw-r--r--  1 root root    4 Jun 26  2019 atd.pid
drwxr-xr-x  2 root root   60 Jun 26  2019 blkid
drwxr-xr-x  2 root root   80 Jun 26  2019 console-setup
-rw-r--r--  1 root root    4 Jun 26  2019 crond.pid
----------  1 root root    0 Jun 26  2019 crond.reboot
drwx------  2 root root   40 Jun 26  2019 cryptsetup
drwxr-xr-x  2 root root   60 Jun 26  2019 dbus
prw-------  1 root root    0 Jun 26  2019 dmeventd-client
prw-------  1 root root    0 Jun 26  2019 dmeventd-server
lrwxrwxrwx  1 root root   25 Jun 26  2019 initctl -> /run/systemd/initctl/fifo
drwxr-xr-x  2 root root  100 Jun 26  2019 initramfs
drwxrwxrwt  4 root root   80 Jun 26  2019 lock
drwxr-xr-x  2 root root   40 Jun 26  2019 log
drwx------  2 root root   80 Jun 26  2019 lvm
-rw-r--r--  1 root root    4 Jun 26  2019 lvmetad.pid
drwx------  4 root root   80 Jun 26  2019 lxcfs
-rw-------  1 root root    4 Jun 26  2019 lxcfs.pid
-rw-r--r--  1 root root    6 Jul 16 12:15 monit.pid
-rw-r--r--  1 root root  858 Sep 24 17:39 motd.dynamic
drwxr-xr-x  2 root root   80 Sep 15 10:53 mount
drwxr-xr-x  2 root root   60 Jun 26  2019 network
drwxr-xr-x  3 root root   60 Jun 26  2019 NetworkManager
-rw-r--r--  1 root root    6 Nov 15  2019 nginx.pid
-rw-r--r--  1 root root    3 Jun 26  2019 rsyslogd.pid
drwxrwxrwt  2 root utmp   40 Jun 26  2019 screen
drwxr-xr-x  2 root root   40 Jun 26  2019 sendsigs.omit.d
lrwxrwxrwx  1 root root    8 Jun 26  2019 shm -> /dev/shm
srw-rw-rw-  1 root root    0 Jun 26  2019 snapd-snap.socket
srw-rw-rw-  1 root root    0 Jun 26  2019 snapd.socket
drwxr-xr-x  2 root root   40 Jun 26  2019 sshd
-rw-r--r--  1 root root    4 Jun 26  2019 sshd.pid
drwx--x--x  3 root root   60 Jun 26  2019 sudo
drwxr-xr-x 22 root root  520 Sep 15 10:53 systemd
drwxr-xr-x  2 root root   60 Jun 26  2019 tmpfiles.d
drwxr-xr-x  7 root root  160 Sep 24 10:24 udev
drwxr-xr-x  3 root root   60 Sep 24 17:39 user
-rw-rw-r--  1 root utmp 2304 Sep 24 17:39 utmp
drwxr-xr-x  2 root root   60 Jun 26  2019 uuidd

Type 2: Unprivileged processes created by users

These go in $XDG_RUNTIME_DIR

In my ubunutu server, this works out as

echo $XDG_RUNTIME_DIR
# a subdirectory of `/run` corresponding to the current user
/run/user/0

# Some example files
tree $XDG_RUNTIME_DIR
.
├── gnupg
│   ├── S.dirmngr
│   ├── S.gpg-agent
│   ├── S.gpg-agent.browser
│   ├── S.gpg-agent.extra
│   └── S.gpg-agent.ssh
├── snapd-session-agent.socket
└── systemd
    ├── notify
    └── private

Lessons

Given that there is a convention for where to put runtime data, the method I employed in the past, of creating working directories where my code was installed, was a bad practice. I should have used $XDG_RUNTIME_DIR. Or maybe /dev/shm for speed (RAM storage) in heavy IO workloads involving temporary files.