3 # sd-tool: Helper Tool for systemd
4 # Copyright (c)2023 Alexander Barton (alex@barton.de)
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
12 export DESCR="Helper commands for systemd timers"
32 if [[ $# -ne 1 || -z "$1" ]]; then
33 sdt_error "Invalid unit name given!"
37 service_unit="${SYSTEMD_SYSTEM_D}/$1.service"
38 timer_unit="${SYSTEMD_SYSTEM_D}/$1.timer"
40 [[ -a "${service_unit}" ]] && sdt_abort "Service unit \"${service_unit}\" already exists!"
41 [[ -a "${timer_unit}" ]] && sdt_abort "Timer unit \"${timer_unit}\" already exists!"
43 # Create service template
44 sdt_verbose "Creating \"${service_unit}\" ..."
49 echo "Description=$1 service"
53 echo 'ExecStart=/bin/true'
54 } >"${service_unit}" || sdt_abort "Failed to create unit file: \"${service_unit}\"!"
56 # Create timer template
57 sdt_verbose "Creating \"${timer_unit}\" ..."
62 echo "Description=$1 service (timer)"
65 echo 'OnCalendar=Sun 2000-01-02 03:04:05'
68 echo 'WantedBy=timers.target'
69 } >"${timer_unit}" || sdt_abort "Failed to create unit file: \"${timer_unit}\"!"
71 sdt_reload_systemd || return 1
73 sdt_info "You can run the following command to edit both new units:"
74 sdt_info_cmd_root "systemctl edit --full \"$1.service\" \"$1.timer\""
75 sdt_info "Use the following command to enable the timer:"
76 sdt_info_cmd_root "systemctl enable --now \"$1.timer\""
82 echo "${PACKAGE_NAME} timer ..."
84 echo ' create <unit-name>'
85 echo ' Create a new systemd "<name>.timer" and "<name>.service" units.'