diff --git a/README.md b/README.md index 127f25b..e6a3f41 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ - [st install](#st-install) - [st uninstall](#st-uninstall) - [st system](#st-system) + - [st systemctl](#st-systemctl) - [st network](#st-network) - [st telegram](#st-telegram) - [st tmux](#st-tmux) @@ -236,6 +237,23 @@ System management utilities for updates and diagnostics: | Show SSD SMART stats | `st system smart` | `st sys smart` | | Show disk usage stats | `st system disk` | `st sys disk` | +### st systemctl + +Systemd control utilities for managing services and viewing logs: + +| Description | Full Command | Alias | +| --------------------------------- | ------------------------------------------- | --------------- | +| Systemd Control Utilities | `st systemctl` | `st sc` | +| Start a systemd service | `st systemctl start ` | `st sc start` | +| Stop a systemd service | `st systemctl stop ` | `st sc stop` | +| Restart a systemd service | `st systemctl restart ` | `st sc restart` | +| Check status of a systemd service | `st systemctl status ` | `st sc status` | +| Enable a service to start on boot | `st systemctl enable ` | `st sc enable` | +| Disable a service from starting | `st systemctl disable ` | `st sc disable` | +| List all systemd services | `st systemctl list` | `st sc list` | +| View service logs | `st systemctl logs ` | `st sc logs` | +| Reload systemd manager config | `st systemctl daemon-reload` | `st sc daemon-reload` | + ### st network Network helper scripts: diff --git a/bashly.yml b/bashly.yml index a33559b..5b0273a 100644 --- a/bashly.yml +++ b/bashly.yml @@ -3,6 +3,7 @@ help: Server Tools CLI version: 1.0.0 commands: - import: src/config/system.yml + - import: src/config/systemctl.yml - import: src/config/install.yml - import: src/config/uninstall.yml - import: src/config/rclone.yml diff --git a/spells/systemctl/daemon-reload.sh b/spells/systemctl/daemon-reload.sh new file mode 100644 index 0000000..7e22a95 --- /dev/null +++ b/spells/systemctl/daemon-reload.sh @@ -0,0 +1,4 @@ + +log_info "Reloading daemon to apply systemd unit file changes" +systemctl daemon-reload +log_info "Daemon reloaded successfully." \ No newline at end of file diff --git a/spells/systemctl/disable.sh b/spells/systemctl/disable.sh new file mode 100644 index 0000000..c021fab --- /dev/null +++ b/spells/systemctl/disable.sh @@ -0,0 +1,10 @@ +SERVICE_NAME="${args[service]}" +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl disable " + exit 1 +fi + +log_info "Disabling systemd service from starting on boot: $SERVICE_NAME" +systemctl disable "$SERVICE_NAME" +log_info "Service '$SERVICE_NAME' disabled from starting on boot." \ No newline at end of file diff --git a/spells/systemctl/enable.sh b/spells/systemctl/enable.sh new file mode 100644 index 0000000..0c6f27c --- /dev/null +++ b/spells/systemctl/enable.sh @@ -0,0 +1,10 @@ +SERVICE_NAME="${args[service]}" +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl enable " + exit 1 +fi + +log_info "Enabling systemd service to start on boot: $SERVICE_NAME" +systemctl enable "$SERVICE_NAME" +log_info "Service '$SERVICE_NAME' enabled to start on boot." \ No newline at end of file diff --git a/spells/systemctl/list.sh b/spells/systemctl/list.sh new file mode 100644 index 0000000..3a03f7d --- /dev/null +++ b/spells/systemctl/list.sh @@ -0,0 +1,3 @@ +log_info "Listing all systemd services:" +systemctl list-units --type=service --all --no-pager +log_info "Service listing completed." diff --git a/spells/systemctl/logs.sh b/spells/systemctl/logs.sh new file mode 100644 index 0000000..b2b24aa --- /dev/null +++ b/spells/systemctl/logs.sh @@ -0,0 +1,22 @@ +SERVICE_NAME="${args[service]}" +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl logs " + exit 1 +fi + +log_info "Fetching logs for systemd service: $SERVICE_NAME" +journalctl -u "$SERVICE_NAME" --no-pager +log_info "Logs fetched." + +log_warn "Note: You can use 'journalctl -u $SERVICE_NAME -f' to follow the logs in real-time." + +log_info "Do you want to do that now? (y/n)" +read -r FOLLOW_LOGS +if [[ "$FOLLOW_LOGS" != "y" && "$FOLLOW_LOGS" != "Y" ]]; then + log_info "Exiting without following logs." + exit 0 +else + log_info "Following logs for service: $SERVICE_NAME" + journalctl -u "$SERVICE_NAME" -f +fi \ No newline at end of file diff --git a/spells/systemctl/restart.sh b/spells/systemctl/restart.sh new file mode 100644 index 0000000..47782d9 --- /dev/null +++ b/spells/systemctl/restart.sh @@ -0,0 +1,10 @@ +SERVICE_NAME="${args[service]}" +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl restart " + exit 1 +fi + +log_info "Restarting systemd service: $SERVICE_NAME" +systemctl restart "$SERVICE_NAME" +log_info "Service '$SERVICE_NAME' restarted." \ No newline at end of file diff --git a/spells/systemctl/start.sh b/spells/systemctl/start.sh new file mode 100644 index 0000000..5c321a2 --- /dev/null +++ b/spells/systemctl/start.sh @@ -0,0 +1,10 @@ +SERVICE_NAME="${args[service]}" +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl start " + exit 1 +fi + +log_info "Starting systemd service: $SERVICE_NAME" +systemctl start "$SERVICE_NAME" +log_info "Service '$SERVICE_NAME' started." \ No newline at end of file diff --git a/spells/systemctl/status.sh b/spells/systemctl/status.sh new file mode 100644 index 0000000..6203206 --- /dev/null +++ b/spells/systemctl/status.sh @@ -0,0 +1,14 @@ +SERVICE_NAME="${args[service]}" + +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl status " + exit 1 +fi + +log_info "Checking status of systemd service: $SERVICE_NAME" + + +systemctl status "$SERVICE_NAME" --no-pager + +log_info "Status check completed." \ No newline at end of file diff --git a/spells/systemctl/stop.sh b/spells/systemctl/stop.sh new file mode 100644 index 0000000..170e308 --- /dev/null +++ b/spells/systemctl/stop.sh @@ -0,0 +1,11 @@ +SERVICE_NAME="${args[service]}" + +# Check argument +if [[ -z "$SERVICE_NAME" ]]; then + log_error "Please provide a service name. Usage: systemctl stop " + exit 1 +fi + +log_info "Stopping systemd service: $SERVICE_NAME" +systemctl stop "$SERVICE_NAME" +log_info "Service '$SERVICE_NAME' stopped." \ No newline at end of file diff --git a/spells/ui.sh b/spells/ui.sh index f1670c9..58808e7 100644 --- a/spells/ui.sh +++ b/spells/ui.sh @@ -11,7 +11,7 @@ fi # Ordered Menu Definitions # ------------------------------- -MAIN_MENU_ORDER=(install uninstall system docker network ssh rclone restic resticprofile tmux tailscale config) +MAIN_MENU_ORDER=(install uninstall system systemctl docker network ssh rclone restic resticprofile tmux tailscale config) declare -A COMMAND_GROUPS=( [install]="Install popular packages" @@ -41,6 +41,7 @@ declare -A SUBCOMMANDS_order_resticprofile=( [0]=show [1]=snapshots [2]=stats [3 declare -A SUBCOMMANDS_order_tmux=( [0]=list-sessions [1]=new-session [2]=attach-session [3]=kill-session [4]=cheatsheet ) declare -A SUBCOMMANDS_order_tailscale=( [0]=up [1]=down [2]=status ) declare -A SUBCOMMANDS_order_config=( [0]=show [1]=edit [2]=verify [3]=source ) +declare -A SUBCOMMANDS_order_systemctl=( [0]=start [1]=stop [2]=restart [3]=status [4]=enable [5]=disable [6]=list [7]=logs [8]=daemon-reload ) # Subcommand descriptions per group declare -A SUBCOMMANDS_install=( [all]="Install all packages" ) @@ -59,6 +60,17 @@ declare -A SUBCOMMANDS_docker=( [start-all]="Start all containers" [manage-stacks]="Manage Compose stacks" ) + declare -A SUBCOMMANDS_systemctl=( + [start]="Start a systemd service" + [stop]="Stop a systemd service" + [restart]="Restart a systemd service" + [status]="Check the status of a systemd service" + [enable]="Enable a service to start on boot" + [disable]="Disable a service from starting on boot" + [list]="List all systemd services" + [logs]="View logs for a systemd service" + [daemon-reload]="Reload systemd manager configuration" + ) declare -A SUBCOMMANDS_network=( [interfaces]="Show interfaces" [linkspeed]="Link speed" @@ -248,6 +260,15 @@ cmd::tmux_cheatsheet() { st tmux cheatsheet; } cmd::tailscale_up() { st tailscale up; } cmd::tailscale_down() { st tailscale down; } +cmd::systemctl_start() { st systemctl start; } +cmd::systemctl_stop() { st systemctl stop; } +cmd::systemctl_restart() { st systemctl restart; } +cmd::systemctl_status() { st systemctl status; } +cmd::systemctl_enable() { st systemctl enable; } +cmd::systemctl_disable() { st systemctl disable; } +cmd::systemctl_list() { st systemctl list; } +cmd::systemctl_logs() { st systemctl logs; } +cmd::systemctl_daemon-reload(){ st systemctl daemon-reload; } cmd::tailscale_status() { st tailscale status; } cmd::rclone_dry-sync() { diff --git a/src/config/systemctl.yml b/src/config/systemctl.yml new file mode 100644 index 0000000..ac4cc53 --- /dev/null +++ b/src/config/systemctl.yml @@ -0,0 +1,93 @@ +name: systemctl +alias: sc +help: Systemd Control Utilities +examples: + - st systemctl start nginx + - st sc stop apache2 + - st systemctl restart mysql + - st sc status postgresql + - st systemctl enable docker + - st sc disable ufw + - st systemctl list + - st sc logs nginx --follow + - st systemctl daemon-reload +commands: + - name: start + help: Start a systemd service + examples: + - st systemctl start nginx + - st sc start apache2 + args: + - name: service + help: Name of the service to start + + - name: stop + help: Stop a systemd service + examples: + - st systemctl stop nginx + - st sc stop apache2 + args: + - name: service + help: Name of the service to stop + + - name: restart + help: Restart a systemd service + examples: + - st systemctl restart mysql + - st sc restart postgresql + args: + - name: service + help: Name of the service to restart + + - name: status + help: Check the status of a systemd service + examples: + - st systemctl status nginx + - st sc status apache2 + args: + - name: service + help: Name of the service to check status for + + - name: enable + help: Enable a systemd service to start on boot + alias: e + examples: + - st systemctl enable docker + - st sc enable ufw + args: + - name: service + help: Name of the service to enable + + - name: disable + help: Disable a systemd service from starting on boot + alias: d + examples: + - st systemctl disable docker + - st sc disable ufw + args: + - name: service + help: Name of the service to disable + + - name: list + help: List all systemd services and their statuses + alias: ls + examples: + - st systemctl list + - st sc list + + - name: logs + help: View logs for a systemd service + alias: lg + examples: + - st systemctl logs nginx --follow + - st sc logs apache2 --since "1 hour ago" + args: + - name: service + help: Name of the service to view logs for + + - name: daemon-reload + help: Reload systemd manager configuration + alias: dr + examples: + - st systemctl daemon-reload + - st sc daemon-reload \ No newline at end of file