# Mount-Point-Service-Sync Skip the fairy-tale, just give me the code already --> [Click Here](https://teb.codes/2-Code/Bash/Mount-Point-Service-Sync#setup) ## Intro ---------------- 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. # MPSS (Mount Point Service Sync) ===================================== 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. ### Basic Concept --------------- 1. Crontab that runs this script regularly 2. Script checks for a file on the network share * If it does, ensure the service is running * If it does not, ensure the service is stopped ## Setup -------- ### Step 1: Download the Script ------------------------------ Download the script from this git repository: ```bash mkdir /opt/mpss git clone https://github.com/tebwritescode/mpss.git cp ./mpss/mpss.sh /opt/mpss/mpss.sh ``` Copy-Pasta is also an option ```bash #!/bin/bash #MPSS - Mount Point Service Sync # Disables/Enables plexmediaserver, tdarr_node service based on the existance of # a file on the network share FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share SERVICE="service" #Service that can be started/stopped with systemctl LOG_FILE="/var/log/mpss.log" #Where to save the log MAX_LINES=200 #How many lines of logs to keep in the log file # Get the current number of lines in the log file CURRENT_LINES=$(wc -l < "$LOG_FILE") # If there are more than MAX_LINES, truncate the file to keep only the last MAX_LINES lines if [ $CURRENT_LINES -gt $MAX_LINES ]; then echo "$(date) INFO: MPSS: Trimming log file" >> /var/log/mpss.log 2>&1 tail -n $MAX_LINES $LOG_FILE >> mpss-temp.log mv mpss-temp.log "$LOG_FILE" fi echo "$(date) INFO: MPSS: Attempting to mount all" >> /var/log/mpss.log 2>&1 sudo mount -a >> /var/log/mpss.log 2>&1 if [ -e "${FILE}" ]; then # Start the service if the file exists echo "$(date) INFO: MPSS: ${FILE} found! Checking if ${SERVICE} is active and if it isn't starting it." >> /var/log/mpss.log 2>&1 sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 || systemctl start $SERVICE >> /var/log/mpss.log 2>&1 else # Stop the service if the file does not exist echo "$(date) INFO: MPSS: ${FILE} not found! Checking if ${SERVICE} is active and if it is stopping it." >> /var/log/mpss.log 2>&1 sudo systemctl is-active --quiet $SERVICE >> /var/log/mpss.log 2>&1 && systemctl stop $SERVICE >> /var/log/mpss.log 2>&1 fi ``` ### Step 2: Customize the Script ------------------------------- Customize the script by modifying the variables at the top of the file: ```bash nano /opt/mpss/mpss.sh ``` Modify `FILE`, `SERVICE`, `LOG_FILE`, and `MAX_LINES` to match your setup. Example: ```bash FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share SERVICE="plexmediaserver" #Service that can be started/stopped with systemctl LOG_FILE="/var/log/mpss.log" #Where to save the log MAX_LINES=200 #How many lines of logs to keep in the log file ``` Save your changes with <kbd>ctrl</kbd> + <kbd>x</kbd>, then <kbd>y</kbd>, then <kbd>return</kbd>. ### Step 3: Create the Log File --------------------------- Create the log file: ```bash touch /var/log/mpss.log ``` ### Step 4: Create the Check File ------------------------------ Create the check file on the network share: ```bash touch /path/to/mount/MOUNT_POINT_ALIVE ``` ### Step 5: Make the Script Executable ------------------------------------ Make the script executable: ```bash chmod +x /opt/mpss/mpss.sh ``` ### Step 6: Schedule the Script with Crontab ----------------------------------------- Schedule the script to run regularly using crontab. Add the following line to your crontab file (use `crontab -e` to edit): ```bash */1 * * * * /opt/mpss/mpss.sh ``` 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. 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.