aboutsummaryrefslogtreecommitdiffstats
path: root/docs/basic-script-openrc/README.md
blob: 73dd50381a24473a77d72e177c0f2c997e7c4969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Basic init yt-local for openrc

## Prerequisites

- System with OpenRC installed and configured.
- Administrative privileges (doas or sudo).
- `ytlocal` script located at `/usr/sbin/ytlocal` and application files in an accessible directory.

## Service Installation

1. **Create the OpenRC service script** `/etc/init.d/ytlocal`:

   ```sh
   #!/sbin/openrc-run
   # Distributed under the terms of the GNU General Public License v3 or later
   name="yt-local"
   pidfile="/var/run/ytlocal.pid"
   command="/usr/sbin/ytlocal"

   depend() {
       use net
   }

   start_pre() {
       if [ ! -f /usr/sbin/ytlocal ]; then
           eerror "Please create script file of ytlocal in '/usr/sbin/ytlocal'"
           return 1
       else
           return 0
       fi
   }

   start() {
       ebegin "Starting yt-local"
       start-stop-daemon --start --exec "${command}" --pidfile "${pidfile}"
       eend $?
   }

   reload() {
       ebegin "Reloading ${name}"
       start-stop-daemon --signal HUP --pidfile "${pidfile}"
       eend $?
   }

   stop() {
       ebegin "Stopping ${name}"
       start-stop-daemon --quiet --stop --exec "${command}" --pidfile "${pidfile}"
       eend $?
   }
   ```

   > [!NOTE]
   > Ensure the script is executable:
   >
   > ```sh
   > doas chmod a+x /etc/init.d/ytlocal
   > ```

2. **Create the executable script** `/usr/sbin/ytlocal`:

   ```bash
   #!/usr/bin/env bash

   # Change the working directory according to your installation path
   # Example: if installed in /usr/local/ytlocal, use:
   cd /home/your-path/ytlocal/   # <-- MODIFY TO YOUR PATH
   source venv/bin/activate
   python server.py > /dev/null 2>&1 &
   echo $! > /var/run/ytlocal.pid
   ```

   > [!WARNING]
   > Run this script only as root or via `doas`, as it writes to `/var/run` and uses network privileges.

   > [!TIP]
   > To store the PID in a different location, adjust the `pidfile` variable in the service script.

   > [!IMPORTANT]
   > Verify that the virtual environment (`venv`) is correctly set up and that `python` points to the appropriate version.

   > [!CAUTION]
   > Do not stop the process manually; use OpenRC commands (`rc-service ytlocal stop`) to avoid race conditions.

   > [!NOTE]
   > When run with administrative privileges, the configuration is saved in `/root/.yt-local`, which is root‑only.

## Service Management

- **Status**: `doas rc-service ytlocal status`
- **Start**: `doas rc-service ytlocal start`
- **Restart**: `doas rc-service ytlocal restart`
- **Stop**: `doas rc-service ytlocal stop`
- **Enable at boot**: `doas rc-update add ytlocal default`
- **Disable**: `doas rc-update del ytlocal`

## Post‑Installation Verification

- Confirm the process is running: `doas rc-service ytlocal status`
- Inspect logs for issues: `doas tail -f /var/log/ytlocal.log` (if logging is configured).

## Troubleshooting Common Issues

- **Service fails to start**: verify script permissions, correct `command=` path, and that the virtualenv exists.
- **Port conflict**: adjust the server’s port configuration before launching.
- **Import errors**: ensure all dependencies are installed in the virtual environment.

[!IMPORTANT]
Keep the service script updated when modifying startup logic or adding new dependencies.