diff --git a/scripts/auto-cpufreq-install.sh b/scripts/auto-cpufreq-install.sh index a1923208..405dd903 100755 --- a/scripts/auto-cpufreq-install.sh +++ b/scripts/auto-cpufreq-install.sh @@ -3,93 +3,104 @@ # auto-cpufreq daemon install script # reference: https://github.com/AdnanHodzic/auto-cpufreq # Thanks to https://github.com/errornonamer for openrc fix -echo -e "\n------------------ Running auto-cpufreq daemon install script ------------------" -if [[ $EUID != 0 ]]; -then - echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')\n" - exit 1 -fi +MID="$((`tput cols` / 2))" -# First argument is the "sv" path, second argument is the "service" path this -# only exist because the path between distros may vary -runit_ln() { - echo -e "\n* Deploy auto-cpufreq runit unit file" - mkdir "$1"/sv/auto-cpufreq - cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-runit "$1"/sv/auto-cpufreq/run - chmod +x "$1"/sv/auto-cpufreq/run +echo +printf "%0.s─" $(seq $((MID-(${#1}/2)-2))) +printf " Running auto-cpufreq daemon install script " +printf "%0.s─" $(seq $((MID-(${#1}/2)-2))) +echo; echo - echo -e "\n* Creating symbolic link ($2/service/auto-cpufreq -> $1/sv/auto-cpufreq)" - ln -s "$1"/sv/auto-cpufreq "$2"/service -} +# root check +if ((EUID != 0)); then + echo; echo "Must be run as root (i.e: 'sudo $0')."; echo + exit 1 +fi -# sv commands -sv_cmd() { - echo -e "\n* Stopping auto-cpufreq daemon (runit) service" - sv stop auto-cpufreq - echo -e "\n* Starting auto-cpufreq daemon (runit) service" - sv start auto-cpufreq - sv up auto-cpufreq +# First argument is the init name, second argument is the start command, third argument is the enable command +function auto_cpufreq_install { + echo -e "\n* Starting auto-cpufreq daemon ($1) service" + $2 + echo -e "\n* Enabling auto-cpufreq daemon ($1) at boot" + $3 } -# Installation for runit, we still look for the distro because of the path may -# vary. -if [ "$(ps h -o comm 1)" = "runit" ];then - if [ -f /etc/os-release ];then - eval "$(cat /etc/os-release)" - case $ID in - void) - runit_ln /etc /var - sv_cmd - ;; - artix) - # Note: Artix supports other inits than runnit - runit_ln /etc/runit /run/runit - sv_cmd - ;; - *) - echo -e "\n* Runit init detected but your distro is not supported\n" - echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" - esac - fi -# Install script for systemd -elif [ "$(ps h -o comm 1)" = "systemd" ];then - echo -e "\n* Deploy auto-cpufreq systemd unit file" - cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service +case "$(ps h -o comm 1)" in + dinit) + echo -e "\n* Deploying auto-cpufreq (dinit) unit file" + cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-dinit /etc/dinit.d/auto-cpufreq - echo -e "\n* Reloading systemd manager configuration" - systemctl daemon-reload + auto_cpufreq_install "dinit" "dinitctl start auto-cpufreq" "dinitctl enable auto-cpufreq" + ;; + init) + ls /etc/inittab + if [ -e "/etc/inittab" ]; then + echo -e "\n* Deploy auto-cpufreq sysvinit unit file" + cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-sysvinit /etc/init.d/auto-cpufreq + chmod +x /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-s6/run + chmod +x /etc/init.d/auto-cpufreq + + auto_cpufreq_install "sysvinit" "service auto-cpufreq start" "update-rc.d auto-cpufreq defaults" + else + echo -e "\n* Deploying auto-cpufreq openrc unit file" + cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-openrc /etc/init.d/auto-cpufreq + chmod +x /etc/init.d/auto-cpufreq - echo -e "\n* Stopping auto-cpufreq daemon (systemd) service" - systemctl stop auto-cpufreq + auto_cpufreq_install "openrc" "rc-service auto-cpufreq start" "rc-update add auto-cpufreq" + fi + ;; + runit) + # First argument is the "sv" path, second argument is the "service" path + runit_ln() { + echo -e "\n* Deploying auto-cpufreq (runit) unit file" + mkdir "$1"/sv/auto-cpufreq + cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-runit "$1"/sv/auto-cpufreq/run + chmod +x "$1"/sv/auto-cpufreq/run - echo -e "\n* Starting auto-cpufreq daemon (systemd) service" - systemctl start auto-cpufreq + echo -e "\n* Creating symbolic link ($2/service/auto-cpufreq -> $1/sv/auto-cpufreq)" + ln -s "$1"/sv/auto-cpufreq "$2"/service - echo -e "\n* Enabling auto-cpufreq daemon (systemd) service at boot" - systemctl enable auto-cpufreq -# Install script for openrc -elif [ "$(ps h -o comm 1)" = "init" ];then - echo -e "\n* Deploying auto-cpufreq openrc unit file" - cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-openrc /etc/init.d/auto-cpufreq - chmod +x /etc/init.d/auto-cpufreq + auto_cpufreq_install "runit" - echo -e "Starting auto-cpufreq daemon (openrc) service" - rc-service auto-cpufreq start + sv start auto-cpufreq + sv up auto-cpufreq + } + + if [ -f /etc/os-release ];then + eval "$(cat /etc/os-release)" + case $ID in + void) runit_ln /etc /var;; + artix) runit_ln /etc/runit /run/runit;; + *) + echo -e "\n* Runit init detected but your distro is not supported\n" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" + esac + fi + ;; + systemd) + echo -e "\n* Deploying auto-cpufreq systemd unit file" + cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service - echo -e "\n* Enabling auto-cpufreq daemon (openrc) service at boot" - rc-update add auto-cpufreq -# Install script for s6 -elif [ "$(ps h -o comm 1)" = "s6-svscan" ];then - echo -e "\n* Deploying auto-cpufreq s6 unit file" + echo -e "\n* Reloading systemd manager configuration" + systemctl daemon-reload + + auto_cpufreq_install "systemd" "systemctl start auto-cpufreq" "systemctl enable auto-cpufreq" + ;; + s6-svscan) + echo -e "\n* Deploying auto-cpufreq (s6) unit file" cp -r /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-s6 /etc/s6/sv/auto-cpufreq + echo -e "\n* Add auto-cpufreq service (s6) to default bundle" s6-service add default auto-cpufreq - echo -e "Starting auto-cpufreq daemon (s6) service" - s6-rc -u change auto-cpufreq default + + auto_cpufreq_install "s6" "s6-rc -u change auto-cpufreq default" + echo -e "\n* Update daemon service bundle (s6)" s6-db-reload -else - echo -e "\n* Unsupported init system detected, could not install the daemon\n" - echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" -fi + ;; + *) + echo -e "\n* Unsupported init system detected, could not install the daemon\n" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" + ;; +esac diff --git a/scripts/auto-cpufreq-remove.sh b/scripts/auto-cpufreq-remove.sh index 659e7ab2..031fda38 100755 --- a/scripts/auto-cpufreq-remove.sh +++ b/scripts/auto-cpufreq-remove.sh @@ -4,72 +4,77 @@ # reference: https://github.com/AdnanHodzic/auto-cpufreq # Thanks to https://github.com/errornonamer for openrc fix -echo -e "\n------------------ Running auto-cpufreq daemon removal script ------------------" +MID="$((`tput cols` / 2))" -if [[ $EUID != 0 ]]; then - echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')\n" - exit 1 -fi +echo +printf "%0.s─" $(seq $((MID-(${#1}/2)-2))) +printf " Running auto-cpufreq daemon removal script " +printf "%0.s─" $(seq $((MID-(${#1}/2)-2))) +echo; echo -# First argument is the "sv" path, second argument is the "service" path -rm_sv() { - echo -e "\n* Stopping auto-cpufreq daemon (runit) service" - sv stop auto-cpufreq +# root check +if ((EUID != 0)); then + echo; echo "Must be run as root (i.e: 'sudo $0')."; echo + exit 1 +fi - echo -e "\n* Removing auto-cpufreq daemon (runit) unit files" - rm -rf "$1"/sv/auto-cpufreq - rm -rf "$2"/service/auto-cpufreq +# First argument is the init name, second argument is the stop command, third argument is the disable command and the fourth is the "service" path +function auto_cpufreq_remove { + echo -e "\n* Stopping auto-cpufreq daemon ($1) service" + $2 + echo -e "\n* Disabling auto-cpufreq daemon ($1) at boot" + $3 + echo -e "\n* Removing auto-cpufreq daemon ($1) unit file" + rm $4 } -# Remove service for runit -if [ "$(ps h -o comm 1)" = "runit" ];then - if [ -f /etc/os-release ];then - eval "$(cat /etc/os-release)" - case $ID in - void) - rm_sv /etc /var ;; - artix) - rm_sv /etc/runit /run/runit ;; - *) - echo -e "\n* Runit init detected but your distro is not supported\n" - echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" - - esac - fi -# Remove service for systemd -elif [ "$(ps h -o comm 1)" = "systemd" ];then - echo -e "\n* Stopping auto-cpufreq daemon (systemd) service" - systemctl stop auto-cpufreq - - echo -e "\n* Disabling auto-cpufreq daemon (systemd) at boot" - systemctl disable auto-cpufreq +case "$(ps h -o comm 1)" in + dinit) auto_cpufreq_remove "dinit" "dinitctl stop auto-cpufreq" "dinitctl disable auto-cpufreq" "/etc/dinit.d/auto-cpufreq";; + init) + ls /etc/inittab + if [ -e "/etc/inittab" ]; then + rm /etc/init.d/auto-cpufreq + auto_cpufreq_remove "sysvinit" "service auto-cpufreq stop" "update-rc.d -f auto-cpufreq remove" + else + auto_cpufreq_remove "openrc" "rc-service auto-cpufreq stop" "rc-update del auto-cpufreq" "/etc/init.d/auto-cpufreq" + fi +;; + + runit) + # First argument is the "sv" path, second argument is the "service" path + rm_sv() { + auto_cpufreq_remove "runit" "sv stop auto-cpufreq" "" "-rf $1/sv/auto-cpufreq $2/service/auto-cpufreq" + } - echo -e "\n* Removing auto-cpufreq daemon (systemd) unit file" - rm /etc/systemd/system/auto-cpufreq.service + if [ -f /etc/os-release ]; then + . /etc/os-release + case $ID in + void) rm_sv /etc /var;; + artix) rm_sv /etc/runit /run/runit;; + *) + echo -e "\n* Runit init detected but your distro is not supported\n" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" + ;; + esac + fi + ;; + systemd) + auto_cpufreq_remove "systemd" "systemctl stop auto-cpufreq" "systemctl disable auto-cpufreq" "/etc/systemd/system/auto-cpufreq.service" echo -e "\n* Reloading systemd manager configuration" systemctl daemon-reload - echo -e "reset failed" + echo "reset failed" systemctl reset-failed -elif [ "$(ps h -o comm 1)" = "init" ];then - echo -e "\n* Stopping auto-cpufreq daemon (openrc) service" - rc-service auto-cpufreq stop - - echo -e "\n* Disabling auto-cpufreq daemon (openrc) at boot" - rc-update del auto-cpufreq - - echo -e "\n* Removing auto-cpufreq daemon (openrc) unit file" - rm /etc/init.d/auto-cpufreq -# Remove service for s6 -elif [ "$(ps h -o comm 1)" = "s6-svscan" ];then - echo -e "\n* Disabling auto-cpufreq daemon (s6) at boot" - s6-service delete default auto-cpufreq - echo -e "\n* Removing auto-cpufreq daemon (s6) unit file" - rm -rf /etc/s6/sv/auto-cpufreq + ;; + s6-svscan) + auto_cpufreq_remove "s6" "" "s6-service delete default auto-cpufreq" "-rf /etc/s6/sv/auto-cpufreq" + echo -e "\n* Update daemon service bundle (s6)" - s6-db-reload -else - echo -e "\n* Unsupported init system detected, could not remove the daemon\n" - echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" -fi + s6-db-reload + ;; + *) + echo -e "\n* Unsupported init system detected, could not remove the daemon" + echo -e "\n* Please open an issue on https://github.com/AdnanHodzic/auto-cpufreq\n" + ;; +esac diff --git a/scripts/auto-cpufreq-sysvinit b/scripts/auto-cpufreq-sysvinit new file mode 100644 index 00000000..6bfb5622 --- /dev/null +++ b/scripts/auto-cpufreq-sysvinit @@ -0,0 +1,98 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: auto-cpufreq +# Required-Start: $local_fs $network $named $time $syslog +# Required-Stop: $local_fs $network $named $time $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: Automatic CPU speed & power optimizer for Linux +### END INIT INFO + +SCRIPT="/usr/local/share/auto-cpufreq/scripts/auto-cpufreq-s6/run" +RUNAS=root + +PIDFILE=/var/run/auto-cpufreq.pid +LOGFILE=/var/log/auto-cpufreq.log + +start() { + if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then + echo 'Service already running' >&2 + return 1 + fi + echo 'Starting service…' >&2 + local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!" + su -c "$CMD" $RUNAS > "$PIDFILE" + # Try with this command line instead of above if not workable + # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE" + + sleep 2 + PID=$(cat $PIDFILE) + if pgrep -u $RUNAS -f $NAME > /dev/null + then + echo "$NAME is now running, the PID is $PID" + else + echo '' + echo "Error! Could not start $NAME!" + fi +} + +stop() { + if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then + echo 'Service not running' >&2 + return 1 + fi + echo 'Stopping service…' >&2 + kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" + echo 'Service stopped' >&2 +} + +uninstall() { + echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " + local SURE + read SURE + if [ "$SURE" = "yes" ]; then + stop + rm -f "$PIDFILE" + echo "Notice: log file was not removed: $LOGFILE" >&2 + update-rc.d -f $NAME remove + rm -fv "$0" + else + echo "Abort!" + fi +} + +status() { + printf "%-50s" "Checking auto-cpufreq..." + if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then + PID=$(cat $PIDFILE) + if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then + printf "%s\n" "The process appears to be dead but pidfile still exists" + else + echo "Running, the PID is $PID" + fi + else + printf "%s\n" "Service not running" + fi +} + + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status + ;; + uninstall) + uninstall + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|status|restart|uninstall}" +esac