Blame

eb4d88 Tebby Dog 2025-07-06 21:43:46
Added intro
1
# Mount-Point-Service-Sync
2
32ed51 Tebby Dog 2025-07-06 21:44:47
Formatting
3
## Intro
4
----------------
5
eb4d88 Tebby Dog 2025-07-06 21:43:46
Added intro
6
Plex kept shitting the bed every time the SMB share randomly dropped offline. After getting sick of jumping in to fix it manually over and over, I hacked together a script that shuts down the Plex service when the share bails, tries to remount the damn thing, and fires Plex back up once the share is stable again.
7
8
# MPSS (Mount Point Service Sync)
9
=====================================
10
11
MPSS dynamically controls start/stop of services (plexmediaserver, tdarr_node, Tdarr_Uptime, emby-server) based on presence or absence of "MOUNT_POINT_ALIVE" file on a network share. It logs actions to /var/log/mpss.log and trims the log to 200 lines.
12
13
14
### Basic Concept
15
---------------
16
17
1. Crontab that runs this script regularly
18
2. Script checks for a file on the network share
19
* If it does, ensure the service is running
20
* If it does not, ensure the service is stopped
21
22
## Setup
23
--------
24
25
### Step 1: Download the Script
26
------------------------------
27
28
Download the script from this git repository:
29
30
```bash
31
mkdir /opt/mpss
32
git clone https://github.com/tebwritescode/mpss.git
33
cp ./mpss/mpss.sh /opt/mpss/mpss.sh
34
```
35
edb864 Tebby Dog 2025-07-06 21:45:55
Added copy pasta
36
Copy-Pasta is also an option
37
38
```bash
39
#!/bin/bash
40
#MPSS - Mount Point Service Sync
41
# Disables/Enables plexmediaserver, tdarr_node service based on the existance of
42
# a file on the network share
43
44
FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share
45
SERVICE="service" #Service that can be started/stopped with systemctl
46
LOG_FILE="/var/log/mpss.log" #Where to save the log
47
MAX_LINES=200 #How many lines of logs to keep in the log file
48
49
# Get the current number of lines in the log file
50
CURRENT_LINES=$(wc -l < "$LOG_FILE")
51
52
# If there are more than MAX_LINES, truncate the file to keep only the last MAX_LINES lines
53
if [ $CURRENT_LINES -gt $MAX_LINES ]; then
54
echo "$(date) INFO: MPSS: Trimming log file" >> /var/log/mpss.log 2>&1
55
tail -n $MAX_LINES $LOG_FILE >> mpss-temp.log
56
mv mpss-temp.log "$LOG_FILE"
57
fi
58
59
echo "$(date) INFO: MPSS: Attempting to mount all" >> /var/log/mpss.log 2>&1
60
sudo mount -a >> /var/log/mpss.log 2>&1
61
62
if [ -e "${FILE}" ]; then
63
# Start the service if the file exists
64
echo "$(date) INFO: MPSS: ${FILE} found! Checking if ${SERVICE} is active and if it isn't starting it." >> /var/log/mpss.log 2>&1
65
sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 || systemctl start $SERVICE >> /var/log/mpss.log 2>&1
66
else
67
# Stop the service if the file does not exist
68
echo "$(date) INFO: MPSS: ${FILE} not found! Checking if ${SERVICE} is active and if it is stopping it." >> /var/log/mpss.log 2>&1
69
sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 && systemctl stop $SERVICE >> /var/log/mpss.log 2>&1
70
fi
71
```
72
eb4d88 Tebby Dog 2025-07-06 21:43:46
Added intro
73
### Step 2: Customize the Script
74
-------------------------------
75
76
Customize the script by modifying the variables at the top of the file:
77
78
```bash
79
nano /opt/mpss/mpss.sh
80
```
81
82
Modify `FILE`, `SERVICE`, `LOG_FILE`, and `MAX_LINES` to match your setup.
83
84
Example:
85
```bash
86
FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share
87
SERVICE="plexmediaserver" #Service that can be started/stopped with systemctl
88
LOG_FILE="/var/log/mpss.log" #Where to save the log
89
MAX_LINES=200 #How many lines of logs to keep in the log file
90
```
91
92
Save your changes with <kbd>ctrl</kbd> + <kbd>x</kbd>, then <kbd>y</kbd>, then <kbd>return</kbd>.
93
94
### Step 3: Create the Log File
95
---------------------------
96
97
Create the log file:
98
99
```bash
100
touch /var/log/mpss.log
101
```
102
103
### Step 4: Create the Check File
104
------------------------------
105
106
Create the check file on the network share:
107
108
```bash
109
touch /path/to/mount/MOUNT_POINT_ALIVE
110
```
111
112
### Step 5: Make the Script Executable
113
------------------------------------
114
115
Make the script executable:
116
117
```bash
118
chmod +x /opt/mpss/mpss.sh
119
```
120
121
### Step 6: Schedule the Script with Crontab
122
-----------------------------------------
123
124
Schedule the script to run regularly using crontab. Add the following line to your crontab file (use `crontab -e` to edit):
125
126
```bash
127
*/1 * * * * /opt/mpss/mpss.sh
128
```
129
130
Note: Be aware that some versions of cron do not support all times you can create on crontab.guru, you will get an error when saving the file.
131
132
That's it! Your MPSS script should now be set up to automatically start/stop services based on the presence or absence of the `MOUNT_POINT_ALIVE` file on your network share.