SRM is a lightweight, Swift-based command-line tool designed to help you manage, monitor, and control various processes, including Swift applications, shell scripts, binaries, and commands. Inspired by PM2, it provides an intuitive interface for starting, stopping, monitoring processes, and viewing real-time logs.
- 🚦 Process Management: Start, stop, restart, and delete processes like commands, binaries, or Swift applications.
- 📊 Monitoring: List all processes with real-time tracking, including CPU and memory usage.
- 📜 Logging: Automatically store and fetch logs for each process, with real-time log tailing by default.
- ❗ Process Statuses: Processes retain their status (
running
,stopped
,error
), even if they fail to start. - ♻️ Auto-Restart: Automatically restart processes if they crash, ensuring continuous uptime.
- 🔄 Log Rotation: Prevent log files from becoming too large with automatic log rotation.
- 🎯 Flexibility: Run shell commands, executables, or scripts seamlessly.
- 🖥 Cross-Platform: Compatible with macOS and Linux systems, supporting both
bash
andzsh
shells. - 🚀 Automatic Monitoring: SRM supports an automatic monitoring service that runs at system startup, ensuring all previously running processes are restored.
To use SRM, ensure you have Swift 5.9 or later installed on your system. Here's how to install Swift:
On macOS, you may already have Swift if you're using Xcode. You can also install Swift via Homebrew:
brew install swift
For Linux-based distributions like Ubuntu, Debian, Fedora, or Raspbian, you can install the Swift toolchain manager Swiftly with a one-liner:
curl -s https://raw.githubusercontent.com/Maxim-Lanskoy/Swiftly/main/install/swiftly-install.sh | bash
For distributions such as Arch or others, please follow the official Swift installation guide for Linux.
-
Clone the Repository:
git clone https://github.com/Maxim-Lanskoy/SRM.git cd SRM
-
Run SRM Setup:
Build and set up SRM to ensure it's globally available:
swift run srm setup
This command builds SRM, adds it to your
$PATH
, and sets up the monitoring service to start with the system.
SRM offers a variety of commands to manage and monitor processes, scripts, and executables.
-
Start any command, executable, or script with a custom name:
srm start "watch -n 5 free -m" --name MemoryMonitor
-
Running a Swift application:
srm start /path/to/swift/app --name SwiftApp
-
Running a Shell Script:
srm start ./myscript.sh --name ScriptRunner
-
Running a Python Script:
srm start "python script.py" --name PythonScript
-
Automatically restart a process if it crashes:
srm start ./myapp --name MyApp --restart
-
Start all stopped processes:
srm start all
-
Stop a running process by its name:
srm stop ProcessName
-
Stop a process by index:
srm stop 1
-
Stop all managed processes:
srm stop all
-
Restart a process by its name:
srm restart ProcessName
-
Restart a process by index:
srm restart 1
-
Restart all processes:
srm restart all
-
Delete a process from SRM by its name:
srm delete ProcessName
-
Delete a process by index:
srm delete 1
-
Delete all processes from SRM:
srm delete all
See a numbered list of all processes and their statuses, including CPU and memory usage:
srm list
You can also use the alias:
srm ls
Example Output:
┌───────┬───────────────┬──────────┬───────┬──────┬──────┬─────────────────────┐
│ Index │ Name │ Status │ PID │ CPU% │ MEM% │ Start Time │
├───────┼───────────────┼──────────┼───────┼──────┼──────┼─────────────────────┤
│ 1 │ MyApp │ running │ 12345 │ 2.3 │ 1.5 │ 2024-10-11 12:34:56 │
├───────┼───────────────┼──────────┼───────┼──────┼──────┼─────────────────────┤
│ 2 │ FailedProcess │ error │ 0 │ N/A │ N/A │ N/A │
├───────┼───────────────┼──────────┼───────┼──────┼──────┼─────────────────────┤
│ 3 │ StoppedProcess│ stopped │ 0 │ N/A │ N/A │ N/A │
└───────┴───────────────┴──────────┴───────┴──────┴──────┴─────────────────────┘
-
Fetch the latest 10 lines and follow logs from any process:
srm logs ProcessName
-
View a specific number of lines and follow:
srm logs ProcessName --lines 50
-
View logs without following:
srm logs ProcessName --no-follow
-
View logs for all processes:
srm logs all
The SRM monitoring service automatically starts with the system, restoring any processes that were previously running. It also tracks the status of all processes and attempts to restart those marked with --restart
, up to a maximum of 15 restart attempts.
Note: The srm monitor
command is now intended to run automatically at system startup only and should not be called manually.
If, for any reason, SRM is no longer available in your $PATH
, or you want to rebuild the tool binary, you can re-run the setup command:
srm setup
This command will also re-register the monitoring service.
If you wish to completely remove SRM from your system:
srm destroy
This will:
- Remove SRM from your
$PATH
. - Delete any saved logs and generated files.
- Delete the compiled binaries from your system.
SRM relies on the ShellOut library to handle process execution, logging, and management. ShellOut enables SRM to use shell commands, run scripts, or execute binaries directly from Swift code.
For detailed help on each command and its options, use the --help
flag:
srm <command> --help
Example:
srm start --help
This will display usage instructions, available options, and examples for the command.
SRM is compatible with:
- Operating Systems: macOS and Linux.
- Shells:
bash
,zsh
, and other common shells.
- Process Names: If you don't specify a process name using
--name
, SRM will use the executable's name by default. - Process Statuses: SRM keeps track of each process's status (
running
,stopped
,error
), allowing you to monitor and debug processes effectively. - Viewing Logs for Failed Processes: You can view logs for processes that failed to start to help debug issues.
- Log Rotation: SRM automatically rotates logs when they exceed 5 MB to prevent log files from becoming too large.
- Auto-Restart: Use the
--restart
flag when starting a process to have SRM automatically restart it if it crashes. The monitoring service runs automatically to facilitate this feature. - Process Indexing: Use the index number from
srm list
to refer to processes in commands. - Aliases: Use
srm ls
as a shortcut forsrm list
.