rofi-systemd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env sh
  2. export SYSTEMD_COLORS=0
  3. term=${ROFI_SYSTEMD_TERM-urxvtcd --geometry 100x20 -e}
  4. default_action=${ROFI_SYSTEMD_DEFAULT_ACTION-"list_actions"}
  5. function unit_files {
  6. systemctl "$1" list-unit-files --no-legend
  7. }
  8. # This is needed to list services started from template units
  9. function running_units {
  10. systemctl "$1" list-units --all --type=service --no-legend |
  11. awk '{print $1 " " $3}'
  12. }
  13. function get_units {
  14. { unit_files "--$1"; running_units "--$1"; } | sort -u -k1,1 |
  15. awk -v unit_type="$1" '{print $0 " " unit_type}'
  16. }
  17. enable="Alt+e"
  18. disable="Alt+d"
  19. stop="Alt+k"
  20. restart="Alt+r"
  21. tail="Alt+t"
  22. list_actions="Alt+l"
  23. all_actions="enable
  24. disable
  25. stop
  26. restart
  27. tail
  28. list_actions"
  29. function select_service_and_act {
  30. result=$(rofi -dmenu -i -p "Systemd Unit" \
  31. -kb-custom-1 "${enable}" \
  32. -kb-custom-2 "${disable}" \
  33. -kb-custom-3 "${stop}" \
  34. -kb-custom-4 "${restart}" \
  35. -kb-custom-5 "${tail}" \
  36. -kb-custom-6 "${list_actions}")
  37. rofi_exit="$?"
  38. case "$rofi_exit" in
  39. 1)
  40. action="exit"
  41. exit 1
  42. ;;
  43. 10)
  44. action="enable"
  45. ;;
  46. 11)
  47. action="disable"
  48. ;;
  49. 12)
  50. action="stop"
  51. ;;
  52. 13)
  53. action="restart"
  54. ;;
  55. 14)
  56. action="tail"
  57. ;;
  58. 15)
  59. action="list_actions"
  60. ;;
  61. *)
  62. action="$default_action"
  63. ;;
  64. esac
  65. selection="$(echo $result | sed -n 's/ \+/ /gp')"
  66. service_name=$(echo "$selection" | awk '{ print $1 }' | tr -d ' ')
  67. is_user="$(echo $selection | awk '{ print $3 }' )"
  68. case "$is_user" in
  69. user)
  70. user_arg="--user"
  71. command="systemctl $user_arg"
  72. ;;
  73. system)
  74. user_arg=""
  75. password=`eval rofi -dmenu -password -p "Password"`
  76. command="echo '$password' | sudo -S systemctl"
  77. ;;
  78. *)
  79. command="systemctl"
  80. esac
  81. to_run="$(get_command_with_args)"
  82. if [ ! -t 1 ] && [[ "$to_run" = *"journalctl"* ]]; then
  83. to_run="$term $to_run"
  84. fi
  85. echo "Running $to_run"
  86. eval "$to_run"
  87. }
  88. function get_command_with_args {
  89. case "$action" in
  90. "tail")
  91. echo "journalctl $user_arg -u '$service_name' -f"
  92. ;;
  93. "list_actions")
  94. action=$(echo "$all_actions" | rofi -dmenu -i -p "Select action: ")
  95. get_command_with_args
  96. ;;
  97. *)
  98. echo "$command $action '$service_name'"
  99. ;;
  100. esac
  101. }
  102. { get_units user; get_units system; } | column -tc 1 | select_service_and_act