A lightweight user-level service manager for managing user-space service processes.
- Service Management: Start, stop, and restart services
- Auto Restart: Multiple restart policies (no, always, unless-stopped, on-failure)
- Log Management: Automatically redirect service output to log files
- Status Query: Query service running status
- Hot Reload: Reload configuration files without restarting the daemon
- Environment Variables: Pass environment variables from client to services
- Working Directory: Customize service working directory
userd/
├── LICENSE # MIT License
├── Makefile # Build script
├── src/
│ ├── client/ # Client program
│ │ └── main.cpp # User control tool (userctl)
│ └── server/ # Server daemon
│ ├── main.cpp # Daemon main program
│ ├── message.hpp # Message structure definition
│ ├── message_queue.hpp # Thread-safe message queue
│ └── service.hpp # Service management class
└── bin/ # Build output directory
- C++17 or later
- POSIX compatible system (Linux, macOS)
# Build client and server
make
# Build debug version
make debug# Install to /usr/local/bin
sudo make installService configuration files are located in ~/.config/userd/ directory with .service extension.
Configuration file format:
name=service_name
cmd=command
restart_policy=restart_policy
env=environment_variables (optional)
workdir=working_directory (optional)no: No auto restartalways: Always auto restartunless-stopped: Restart unless manually stoppedon-failure: Restart only on failure
Maximum restart count can be specified: always:5
${SERVER}: Use server environment variables (default)${CLIENT}: Receive environment variables from clientKEY1=val1,KEY2=val2: Specify environment variables
${SERVER}: Use server current directory (default)${CLIENT}: Receive working directory from client/path/to/dir: Specify working directory
Add service names to be auto-started in ~/.config/userd/enable file, one per line.
# Start service
userctl start <service> [env] [workdir]
# Stop service
userctl stop <service>
# Restart service
userctl restart <service>
# View service status
userctl status [service]
# View service logs
userctl logs <service>
# Reload configuration
userctl reloadCreate a simple service configuration:
# Create config directory
mkdir -p ~/.config/userd
# Create service config file
cat > ~/.config/userd/myapp.service << 'EOF'
name=myapp
cmd=/usr/bin/python3 /path/to/app.py
restart_policy=always:5
workdir=/path/to/app
EOF
# Enable service
echo "myapp" >> ~/.config/userd/enable
# Start daemon (usually started by system service manager)
userd &- Uses Unix Domain Socket for IPC
- Single-threaded message queue for all service operations
- Uses
waitpidto monitor child process status - Signal handling for graceful shutdown
- Communicates with daemon via Unix Domain Socket
- Command-line argument parsing
- Real-time service output display
Custom simple protocol:
oprefix: Command messageeprefix: Error messagecprefix: Connection closevprefix: Verification request (env/workdir)aprefix: Verification response\x04(EOT): Message end marker
Service logs are stored in ~/.cache/userd/ directory with filename format <service>.log.
This project is licensed under the MIT License.
Issues and Pull Requests are welcome!
Carry-Rao