Mastering Linux Daemon Processes: Logging, User IDs, and Resource Limits
This article explains how Linux server programs run as daemons, handle logging with syslog/rsyslog, manage user identities, control process groups and sessions, enforce resource limits, and change working directories, providing practical guidance for robust backend operations.
Beyond network communication, Linux server programs must address many routine but essential details.
Server programs typically run as background daemons without a controlling terminal, usually spawned by the init process (PID 1).
They use a logging system that writes to files under /var/log and may forward logs to a UDP server.
Daemons run under dedicated non‑root accounts (e.g., mysql , apache , syslog ).
Configuration files are stored in /etc and parsed at startup.
A PID file is created in /var/run to record the daemon’s process ID.
Resource limits such as maximum file descriptors and memory are considered to predict load capacity.
01 Logging
Linux provides the syslogd daemon (upgraded to rsyslogd) to handle system logs. rsyslogd can receive logs from user processes via the syslog function and from the kernel via /proc/kmsg. Default log files include /var/log/debug, /var/log/messages, and /var/log/kern.log. Configuration resides in /etc/rsyslog.conf, where you set input sources, UDP/TCP ports, file permissions, and included files.
Applications call syslog() with a priority that combines a facility (e.g., LOG_USER) and a severity level. The ident argument prefixes log messages, logopt controls behavior, and facility changes the default facility. Log masks can filter messages so that only those above a certain severity are recorded.
The function maskpri sets the log mask, returning the previous mask value.
02 User Information
Each process has a real UID/GID and an effective UID/GID. The effective IDs determine the privileges the process can use, allowing programs like su to run with root privileges while the real user remains non‑root. Setting the set‑user‑ID bit on an executable makes its effective UID become root when executed.
03 Process Relationships
Every process belongs to a process group (PGID) and may belong to a session (SID). Functions such as getpgid(), setpgid(), and setsid() manage these identifiers. Only a process can change its own or its children’s PGID, and a session leader creates a new process group and detaches from any controlling terminal.
The ps command can display the relationships between processes, groups, and sessions.
04 System Resource Limits
Programs are subject to limits on CPU time, memory, file sizes, etc. The getrlimit and setrlimit functions read and modify these limits using the rlimit structure, which contains rlim_cur (soft limit) and rlim_max (hard limit). Exceeding a soft limit may generate signals such as SIGXCPU or SIGXFSZ. Only root can raise hard limits.
05 Changing Working and Root Directories
Use getcwd() to obtain the current working directory and chdir() to change it. If the path length exceeds the buffer size, getcwd returns NULL and sets errno to ERANGE. To change the root directory, call chroot(); after chroot you must also call chdir("/") to move to the new root.
06 Daemonizing a Server Program
Linux provides library functions to simplify daemon creation. Passing 0 to nochdir keeps the current directory; passing non‑zero changes it to /. Passing 0 to noclose redirects standard input, output, and error to /dev/null. These steps help detach the process from the terminal and run it in the background.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
