Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In recent ubuntus (specifically, what interests me the most is 12.04) /var/run is tmpfs, and thus I assume it starts empty with each reboot.

Yet, I see there some folders, like /var/run/mysqld and numerous others. How those folders created on each reboot? Is there some template folder which copied to /var/run/ (and if so, which script does that), or each folder mkdir'ed separately, or what ?

EDIT:
Please, do not answer that /var/run is persistent, or that /run is persistent. Because it is NOT.

Even if it is so on your system, it is not so on a regular 12.04.

share|improve this question
    
Hmm, so I'm going to have 50 rep. wasted on one of the two answers which are both obviously plainly wrong.... –  Sandman4 Jun 10 '13 at 13:22
    
Someone, please answer ANYTHING sensible and you get +50 rep :) –  Sandman4 Jun 10 '13 at 13:24

6 Answers 6

up vote 16 down vote accepted
+50

Looks like they're created dynamically by individual services as they start:

$ sudo egrep -r 'mkdir.*/var/run' /etc

/etc/init.d/ssh:        mkdir /var/run/sshd
/etc/init.d/bind9:      mkdir -p /var/run/named
/etc/init.d/timidity:    mkdir -p /var/run/timidity
/etc/init.d/bzflag:                mkdir -p /var/run/bzflag
/etc/init.d/dns-clean:mkdir /var/run/pppconfig >/dev/null 2>&1 || true
/etc/init/winbind.conf: mkdir -p /var/run/samba/winbindd_privileged
/etc/init/dbus.conf:    mkdir -p /var/run/dbus
/etc/init/ssh.conf:    mkdir -p -m0755 /var/run/sshd
/etc/init/libvirt-bin.conf:     mkdir -p /var/run/libvirt
/etc/init/cups.conf:    mkdir -p /var/run/cups/certs

I believe this is the one that handles mysqld:

[ -d /var/run/mysqld ] || install -m 755 -o mysql -g root -d /var/run/mysqld
/lib/init/apparmor-profile-load usr.sbin.mysqld

man install says that the -d form will "create all components of the specified directories".

share|improve this answer

The new tmpfs-mounted /run folder allows programs like udev, lvm and mdadm to keep runtime data from initrd until shutdown.

/var is a standard directory of any Linux/UNIX system - it stands for "variable" and is a place where a lot of logs, cahces, BUT also program variable settings files and even some system configuration databases reside.

Most things in /var should be properly purged and regulated by the system. Your swap files for virtual memory also live in /var so don't mess with that. /var/run also holds a lot status and parameter information of actively running process daemans.

This directory contains system information data describing the system since it was booted. Files under this directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process. Programs may have a sub-directory of /var/run; this is encouraged for programs that use more than one run-time file.

Well since /var/run is mounted as tmpfs. That means it's totally empty when your machine boots and it's meant to be like this to prevent stuff like daemons not starting because of a left-over PID-file.

Startup scripts usually create the directories they need before using them. If you want to store a PID-file either put it in /var/run directly or create a directory before creating the PID-file. This is no place to store data that needs to remain there across reboots.

Sources:Pathname & Linux System Administrator's Guide

share|improve this answer

Yet, I see there some folders, like /var/run/mysqld and numerous others. How those folders created on each reboot ? Is there some template folder which copied to /var/run/ (and if so, which script does that), or each folder mkdired separately, or what ?

As defined in the File Hierarchy Standard the /var/run or /run is used to store volatile runtime data.

All the folders and files created in there are managed by the respective program that created the files. There is no such a thing as a template folder that is copied, every program can use that folder to store volatile information. The stored data gets lost when the system reboots.

A common thing for using the /run folder is to store the pid of running daemons, marker files that contain the process number of a process. They are mainly used for the start/stop scripts that you can find for example in /etc/init.d/

I hope that made things clear to you!

br

share|improve this answer

For anyone that comes across this thread because you're looking for a solution to how you can configure an application so that it creates the directory in /var/run so it can store it's sock or pid file or whatever… here's an example. I came across this thread because I wanted to store the MySQL sock file in /var/run/mysqld. So, after I came across this thread, I started looking in the /etc/init files for examples. dbus was a good one. And, I came up with this mysql startup configuration:

start on runlevel [2345] 
stop on runlevel [!2345] 
expect daemon 

pre-start script 
    mkdir -p -m0755 /var/run/mysqld 
    chown mysql:mysql /var/run/mysqld 
end script 

exec /etc/init.d/mysql start 
pre-stop exec /etc/init.d/mysql stop 

The pre-start script part did the trick.

share|improve this answer

Your assumption is not absolutely correct. The location of the /var folder is negotiable - that is, you can use an alternate partition or volume on which to locate the /var folder. Regardless of where the /var folder is located, the /var/run folder is a symlink to the /run folder and its contents remain after reboots, though many of the files in /run are generated or modified at boot by services starting during boot. So it is the services - such as mysqld - that call for loading files into the /var/run directory and are set up to create subdirectories if they do not exist currently.

share|improve this answer
1  
I created some folders in /var/run and they disappeared after reboot. (specifically on 12.04) –  Sandman4 Jun 3 '13 at 12:58
    
Also see here that /var/run is indeed tmpfs askubuntu.com/questions/57297/… –  Sandman4 Jun 3 '13 at 12:58

douggro is absolutely right, /var/run is mounted as tmpfs, and /var/run is a symlink to /run which is persistent across reboots, so anything that goes in /run will also appear in (and stay in) /var/run.

So any boot services like mysqld, which is a daemon which is started at boot time, that create files in /run, will also have files visible in /var/run (symlink to /run remember). If you want to create a file that will persist through a reboot in /var/run, create it in /run and then reboot.

Hope this answers your question.

share|improve this answer
3  
You're both mistaken, or using non-standard setups. Check for yourself. mount | grep /run .... tmpfs on /run type tmpfs –  Steven Kath Jun 8 '13 at 19:11
1  
"If you want to create a file that will persist through a reboot in /var/run, create it in /run and then reboot." That is NOT correct. tmpfs is volatile and changes are lost. –  ortang Jun 11 '13 at 12:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.