123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #!/usr/bin/env sh
- export SYSTEMD_COLORS=0
- term=${ROFI_SYSTEMD_TERM-urxvtcd --geometry 100x20 -e}
- default_action=${ROFI_SYSTEMD_DEFAULT_ACTION-"list_actions"}
- function unit_files {
- systemctl "$1" list-unit-files --no-legend
- }
- # This is needed to list services started from template units
- function running_units {
- systemctl "$1" list-units --all --type=service --no-legend |
- awk '{print $1 " " $3}'
- }
- function get_units {
- { unit_files "--$1"; running_units "--$1"; } | sort -u -k1,1 |
- awk -v unit_type="$1" '{print $0 " " unit_type}'
- }
- enable="Alt+e"
- disable="Alt+d"
- stop="Alt+k"
- restart="Alt+r"
- tail="Alt+t"
- list_actions="Alt+l"
- all_actions="enable
- disable
- stop
- restart
- tail
- list_actions"
- function select_service_and_act {
- result=$(rofi -dmenu -i -p "Systemd Unit" \
- -kb-custom-1 "${enable}" \
- -kb-custom-2 "${disable}" \
- -kb-custom-3 "${stop}" \
- -kb-custom-4 "${restart}" \
- -kb-custom-5 "${tail}" \
- -kb-custom-6 "${list_actions}")
- rofi_exit="$?"
- case "$rofi_exit" in
- 1)
- action="exit"
- exit 1
- ;;
- 10)
- action="enable"
- ;;
- 11)
- action="disable"
- ;;
- 12)
- action="stop"
- ;;
- 13)
- action="restart"
- ;;
- 14)
- action="tail"
- ;;
- 15)
- action="list_actions"
- ;;
- *)
- action="$default_action"
- ;;
- esac
- selection="$(echo $result | sed -n 's/ \+/ /gp')"
- service_name=$(echo "$selection" | awk '{ print $1 }' | tr -d ' ')
- is_user="$(echo $selection | awk '{ print $3 }' )"
- case "$is_user" in
- user)
- user_arg="--user"
- command="systemctl $user_arg"
- ;;
- system)
- user_arg=""
- password=`eval rofi -dmenu -password -p "Password"`
- command="echo '$password' | sudo -S systemctl"
- ;;
- *)
- command="systemctl"
- esac
- to_run="$(get_command_with_args)"
- if [ ! -t 1 ] && [[ "$to_run" = *"journalctl"* ]]; then
- to_run="$term $to_run"
- fi
- echo "Running $to_run"
- eval "$to_run"
- }
- function get_command_with_args {
- case "$action" in
- "tail")
- echo "journalctl $user_arg -u '$service_name' -f"
- ;;
- "list_actions")
- action=$(echo "$all_actions" | rofi -dmenu -i -p "Select action: ")
- get_command_with_args
- ;;
- *)
- echo "$command $action '$service_name'"
- ;;
- esac
- }
- { get_units user; get_units system; } | column -tc 1 | select_service_and_act
|