]> arthur.ath.cx Git - sd-tool.git/blob - share/sdt-cmd-timer.inc.sh
Initial commit
[sd-tool.git] / share / sdt-cmd-timer.inc.sh
1 #!/bin/bash
2 #
3 # sd-tool: Helper Tool for systemd
4 # Copyright (c)2023 Alexander Barton (alex@barton.de)
5 #
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.
10 #
11
12 export DESCR="Helper commands for systemd timers"
13
14 sdt_cmd_timer() {
15         case "$1" in
16                 "create")
17                         shift
18                         sdt_timer_create "$@"
19                         return $?
20                         ;;
21                 "help"|"--help")
22                         sdt_timer_help
23                         return 0
24                         ;;
25                 *)
26                         sdt_timer_help
27                         return 2
28         esac
29 }
30
31 sdt_timer_create() {
32         if [[ $# -ne 1 || -z "$1" ]]; then
33                 sdt_error "Invalid unit name given!"
34                 return 2
35         fi
36
37         service_unit="${SYSTEMD_SYSTEM_D}/$1.service"
38         timer_unit="${SYSTEMD_SYSTEM_D}/$1.timer"
39
40         [[ -a "${service_unit}" ]] && sdt_abort "Service unit \"${service_unit}\" already exists!"
41         [[ -a "${timer_unit}" ]] && sdt_abort "Timer unit \"${timer_unit}\" already exists!"
42
43         # Create service template
44         sdt_verbose "Creating \"${service_unit}\" ..."
45         {
46                 echo "# $1.service"
47                 echo
48                 echo '[Unit]'
49                 echo "Description=$1 service"
50                 echo
51                 echo '[Service]'
52                 echo 'Type=exec'
53                 echo 'ExecStart=/bin/true'
54         } >"${service_unit}" || sdt_abort "Failed to create unit file: \"${service_unit}\"!"
55
56         # Create timer template
57         sdt_verbose "Creating \"${timer_unit}\" ..."
58         {
59                 echo "# $1.timer"
60                 echo
61                 echo '[Unit]'
62                 echo "Description=$1 service (timer)"
63                 echo
64                 echo '[Timer]'
65                 echo 'OnCalendar=Sun 2000-01-02 03:04:05'
66                 echo
67                 echo '[Install]'
68                 echo 'WantedBy=timers.target'
69         } >"${timer_unit}" || sdt_abort "Failed to create unit file: \"${timer_unit}\"!"
70
71         sdt_reload_systemd || return 1
72
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\""
77         return 0
78 }
79
80 sdt_timer_help() {
81         {
82                 echo "${PACKAGE_NAME} timer ..."
83                 echo
84                 echo ' create <unit-name>'
85                 echo '   Create a new systemd "<name>.timer" and "<name>.service" units.'
86                 echo
87         } >&2
88 }