Blame
eb4d88 | Tebby Dog | 2025-07-06 21:43:46 | 1 | # Mount-Point-Service-Sync |
2 | ||||
372780 | Tebby Dog | 2025-07-06 22:32:27 | 3 | Skip the fairy-tale, just give me the code already --> [Click Here](https://teb.codes/2-Code/Bash/Mount-Point-Service-Sync#setup) |
f76050 | Tebby Dog | 2025-07-06 22:32:12 | 4 | |
32ed51 | Tebby Dog | 2025-07-06 21:44:47 | 5 | ## Intro |
6 | ---------------- |
|||
7 | ||||
eb4d88 | Tebby Dog | 2025-07-06 21:43:46 | 8 | 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. |
9 | ||||
10 | # MPSS (Mount Point Service Sync) |
|||
11 | ===================================== |
|||
12 | ||||
13 | 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. |
|||
14 | ||||
15 | ||||
16 | ### Basic Concept |
|||
17 | --------------- |
|||
18 | ||||
19 | 1. Crontab that runs this script regularly |
|||
20 | 2. Script checks for a file on the network share |
|||
21 | * If it does, ensure the service is running |
|||
22 | * If it does not, ensure the service is stopped |
|||
23 | ||||
24 | ## Setup |
|||
25 | -------- |
|||
26 | ||||
27 | ### Step 1: Download the Script |
|||
28 | ------------------------------ |
|||
29 | ||||
30 | Download the script from this git repository: |
|||
31 | ||||
32 | ```bash |
|||
33 | mkdir /opt/mpss |
|||
34 | git clone https://github.com/tebwritescode/mpss.git |
|||
35 | cp ./mpss/mpss.sh /opt/mpss/mpss.sh |
|||
36 | ``` |
|||
37 | ||||
edb864 | Tebby Dog | 2025-07-06 21:45:55 | 38 | Copy-Pasta is also an option |
39 | ||||
40 | ```bash |
|||
41 | #!/bin/bash |
|||
42 | #MPSS - Mount Point Service Sync |
|||
43 | # Disables/Enables plexmediaserver, tdarr_node service based on the existance of |
|||
44 | # a file on the network share |
|||
45 | ||||
46 | FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share |
|||
47 | SERVICE="service" #Service that can be started/stopped with systemctl |
|||
48 | LOG_FILE="/var/log/mpss.log" #Where to save the log |
|||
49 | MAX_LINES=200 #How many lines of logs to keep in the log file |
|||
50 | ||||
51 | # Get the current number of lines in the log file |
|||
52 | CURRENT_LINES=$(wc -l < "$LOG_FILE") |
|||
53 | ||||
54 | # If there are more than MAX_LINES, truncate the file to keep only the last MAX_LINES lines |
|||
55 | if [ $CURRENT_LINES -gt $MAX_LINES ]; then |
|||
56 | echo "$(date) INFO: MPSS: Trimming log file" >> /var/log/mpss.log 2>&1 |
|||
57 | tail -n $MAX_LINES $LOG_FILE >> mpss-temp.log |
|||
58 | mv mpss-temp.log "$LOG_FILE" |
|||
59 | fi |
|||
60 | ||||
61 | echo "$(date) INFO: MPSS: Attempting to mount all" >> /var/log/mpss.log 2>&1 |
|||
62 | sudo mount -a >> /var/log/mpss.log 2>&1 |
|||
63 | ||||
64 | if [ -e "${FILE}" ]; then |
|||
65 | # Start the service if the file exists |
|||
66 | echo "$(date) INFO: MPSS: ${FILE} found! Checking if ${SERVICE} is active and if it isn't starting it." >> /var/log/mpss.log 2>&1 |
|||
67 | sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 || systemctl start $SERVICE >> /var/log/mpss.log 2>&1 |
|||
68 | else |
|||
69 | # Stop the service if the file does not exist |
|||
70 | echo "$(date) INFO: MPSS: ${FILE} not found! Checking if ${SERVICE} is active and if it is stopping it." >> /var/log/mpss.log 2>&1 |
|||
71 | sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 && systemctl stop $SERVICE >> /var/log/mpss.log 2>&1 |
|||
72 | fi |
|||
73 | ``` |
|||
74 | ||||
eb4d88 | Tebby Dog | 2025-07-06 21:43:46 | 75 | ### Step 2: Customize the Script |
76 | ------------------------------- |
|||
77 | ||||
78 | Customize the script by modifying the variables at the top of the file: |
|||
79 | ||||
80 | ```bash |
|||
81 | nano /opt/mpss/mpss.sh |
|||
82 | ``` |
|||
83 | ||||
84 | Modify `FILE`, `SERVICE`, `LOG_FILE`, and `MAX_LINES` to match your setup. |
|||
85 | ||||
86 | Example: |
|||
87 | ```bash |
|||
88 | FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share |
|||
89 | SERVICE="plexmediaserver" #Service that can be started/stopped with systemctl |
|||
90 | LOG_FILE="/var/log/mpss.log" #Where to save the log |
|||
91 | MAX_LINES=200 #How many lines of logs to keep in the log file |
|||
92 | ``` |
|||
93 | ||||
94 | Save your changes with <kbd>ctrl</kbd> + <kbd>x</kbd>, then <kbd>y</kbd>, then <kbd>return</kbd>. |
|||
95 | ||||
96 | ### Step 3: Create the Log File |
|||
97 | --------------------------- |
|||
98 | ||||
99 | Create the log file: |
|||
100 | ||||
101 | ```bash |
|||
102 | touch /var/log/mpss.log |
|||
103 | ``` |
|||
104 | ||||
105 | ### Step 4: Create the Check File |
|||
106 | ------------------------------ |
|||
107 | ||||
108 | Create the check file on the network share: |
|||
109 | ||||
110 | ```bash |
|||
111 | touch /path/to/mount/MOUNT_POINT_ALIVE |
|||
112 | ``` |
|||
113 | ||||
114 | ### Step 5: Make the Script Executable |
|||
115 | ------------------------------------ |
|||
116 | ||||
117 | Make the script executable: |
|||
118 | ||||
119 | ```bash |
|||
120 | chmod +x /opt/mpss/mpss.sh |
|||
121 | ``` |
|||
122 | ||||
123 | ### Step 6: Schedule the Script with Crontab |
|||
124 | ----------------------------------------- |
|||
125 | ||||
126 | Schedule the script to run regularly using crontab. Add the following line to your crontab file (use `crontab -e` to edit): |
|||
127 | ||||
128 | ```bash |
|||
129 | */1 * * * * /opt/mpss/mpss.sh |
|||
130 | ``` |
|||
131 | ||||
132 | 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. |
|||
133 | ||||
134 | 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. |