3

I want to create a system-wide directory, that contains application specific (read-write) data (like log files, configurations and other app specific metadata).

After reading a bit more about the Linux file system, I thought about using /var/app_name/, but then I found out, that some of the subdirs are temporary (not persistent among restarts, like run, log, tmp).

How significant is this? I mean, should I use another directory (like /home/app_name/) or using /var/app_name/ is OK?

  • In the case of temporary directories it is important to check your OS for tmpfs filesystems. By running the mount command without arguments it should print a list of filesystems on your machine, including the filesystem type, source, and mount point. If you see any filesystems mounted as type tmpfs that means anything written within that mount point will be lost on reboot. – Centimane May 2 '17 at 15:21
5

From the Filesystem Hierarchy Standard:

Applications must generally not add directories to the top level of /var. Such directories should only be added if they have some system-wide implication, and in consultation with the FHS mailing list.

You should use /etc/app_name/ to store config files and other stuff for your program, and /var/log/app_name/ to store its logfiles.

For the data used by the application, you can store:

  • in /var/lib/app_name/ the persistent data and metadata
  • in /var/cache/app_name/ any app cache that can safely be deleted
  • in /var/spool/app_name/ the data that awaits processing

Definitely do not use /home/app_name/ which is reserved to the homedir of user app_name. If your program needs to create a specific user to run as, that'll be its place.

About your question in the comment: Linux does not delete neither rotate logs automatically for anything you put into /var/log/. In fact, often sysadmins have the opposite problem of logs filling up all the space... So it's up to you to delete or rotate logfiles; this is done via logrotate or a custom cron job.

  • Hmm, ok about the logs and configs, but what about the rest of the metadata, that I want to write? That's what's bugging me, the configs and logs are kinda clear. For example, what if I want to store some DB files, where to write them? (thanks for the /var/log explanation, btw) – Kiril Kirov May 2 '17 at 14:43
  • @KirilKirov Take a look in /var/lib/ many services/applications store data there. Big one that stands out is ldap, an ldap-server stores the ldap database in /var/lib/ldap/ by default. yum also keeps a yumdb in /var/lib/yum/ – Centimane May 2 '17 at 15:20
  • You could use /var/lib/app_name/. I'm adding that to my answer. – dr01 May 2 '17 at 15:20
  • @Centimane Your comment beat mine of a few seconds :) – dr01 May 2 '17 at 15:26
  • 1
    I would suggest not to use /home/app_name even if the program requires a specific user. If the program requires a specific user, that users shell should be /sbin/nologin, and their home directory should either be / (to indicate the user does nothing special in their home directory) or /var/lib/app_name (in case it is used to direct to the application data). You can look at some of the accounts in /etc/passwd for an idea. – Centimane May 2 '17 at 16:12
1

It ought to be safe to use /var/app_name to store persistent data for an application called app_name.

On the system I'm using at the moment, the default database directories for PostgreSQL and MySQL (MariaDB) are /var/postgresql and /var/mysql, for example.

/var/tmp should only be used for temporary files, but a system is unlikely clear this directory out (on reboots, for example, unlike /tmp). /var/run is most often used to store PID files and named sockets.

I would avoid /home/app_name as /home is most often associated with personal account directories.

See also: https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

An application may choose to store its configuration file in /etc, its log files under /var/log, and data in /var/app_name. In the case of PostgreSQL and MariaDB that I used as examples above, they store everything except system startup hooks in /var.

  • Does Linux purge logs periodically? My concerns are - if I use /var/log/, will the OS delete some of my logs at some point? – Kiril Kirov May 2 '17 at 13:53
  • @KirilKirov Specific logs are rotated. It may depend on the distribution, but I don't think there's any Linux that blindly just rotates everything in /var/log. – Kusalananda May 2 '17 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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