Blame
|
1 | # Mount-Point-Service-Sync |
||||||
| 2 | ||||||||
| 3 | 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. |
|||||||
| 4 | ||||||||
| 5 | # MPSS (Mount Point Service Sync) |
|||||||
| 6 | ===================================== |
|||||||
| 7 | ||||||||
| 8 | 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. |
|||||||
| 9 | ||||||||
| 10 | ## Usage Example |
|||||||
| 11 | ---------------- |
|||||||
| 12 | ||||||||
| 13 | I run several services in my linux environment that depend on data on a network share to function. Occasionally that share goes offline for some reason. This script was written to ensure those services are shut down when the mount point becomes unavailable. |
|||||||
| 14 | ||||||||
| 15 | ### Basic Concept |
|||||||
| 16 | --------------- |
|||||||
| 17 | ||||||||
| 18 | 1. Crontab that runs this script regularly |
|||||||
| 19 | 2. Script checks for a file on the network share |
|||||||
| 20 | * If it does, ensure the service is running |
|||||||
| 21 | * If it does not, ensure the service is stopped |
|||||||
| 22 | ||||||||
| 23 | ## Setup |
|||||||
| 24 | -------- |
|||||||
| 25 | ||||||||
| 26 | ### Step 1: Download the Script |
|||||||
| 27 | ------------------------------ |
|||||||
| 28 | ||||||||
| 29 | Download the script from this git repository: |
|||||||
| 30 | ||||||||
| 31 | ```bash |
|||||||
| 32 | mkdir /opt/mpss |
|||||||
| 33 | git clone https://github.com/tebwritescode/mpss.git |
|||||||
| 34 | cp ./mpss/mpss.sh /opt/mpss/mpss.sh |
|||||||
| 35 | ``` |
|||||||
| 36 | ||||||||
| 37 | ### Step 2: Customize the Script |
|||||||
| 38 | ------------------------------- |
|||||||
| 39 | ||||||||
| 40 | Customize the script by modifying the variables at the top of the file: |
|||||||
| 41 | ||||||||
| 42 | ```bash |
|||||||
| 43 | nano /opt/mpss/mpss.sh |
|||||||
| 44 | ``` |
|||||||
| 45 | ||||||||
| 46 | Modify `FILE`, `SERVICE`, `LOG_FILE`, and `MAX_LINES` to match your setup. |
|||||||
| 47 | ||||||||
| 48 | Example: |
|||||||
| 49 | ```bash |
|||||||
| 50 | FILE="/path/to/mount/MOUNT_POINT_ALIVE" #The location of the file on the share |
|||||||
| 51 | SERVICE="plexmediaserver" #Service that can be started/stopped with systemctl |
|||||||
| 52 | LOG_FILE="/var/log/mpss.log" #Where to save the log |
|||||||
| 53 | MAX_LINES=200 #How many lines of logs to keep in the log file |
|||||||
| 54 | ``` |
|||||||
| 55 | ||||||||
| 56 | Save your changes with <kbd>ctrl</kbd> + <kbd>x</kbd>, then <kbd>y</kbd>, then <kbd>return</kbd>. |
|||||||
| 57 | ||||||||
| 58 | ### Step 3: Create the Log File |
|||||||
| 59 | --------------------------- |
|||||||
| 60 | ||||||||
| 61 | Create the log file: |
|||||||
| 62 | ||||||||
| 63 | ```bash |
|||||||
| 64 | touch /var/log/mpss.log |
|||||||
| 65 | ``` |
|||||||
| 66 | ||||||||
| 67 | ### Step 4: Create the Check File |
|||||||
| 68 | ------------------------------ |
|||||||
| 69 | ||||||||
| 70 | Create the check file on the network share: |
|||||||
| 71 | ||||||||
| 72 | ```bash |
|||||||
| 73 | touch /path/to/mount/MOUNT_POINT_ALIVE |
|||||||
| 74 | ``` |
|||||||
| 75 | ||||||||
| 76 | ### Step 5: Make the Script Executable |
|||||||
| 77 | ------------------------------------ |
|||||||
| 78 | ||||||||
| 79 | Make the script executable: |
|||||||
| 80 | ||||||||
| 81 | ```bash |
|||||||
| 82 | chmod +x /opt/mpss/mpss.sh |
|||||||
| 83 | ``` |
|||||||
| 84 | ||||||||
| 85 | ### Step 6: Schedule the Script with Crontab |
|||||||
| 86 | ----------------------------------------- |
|||||||
| 87 | ||||||||
| 88 | Schedule the script to run regularly using crontab. Add the following line to your crontab file (use `crontab -e` to edit): |
|||||||
| 89 | ||||||||
| 90 | ```bash |
|||||||
| 91 | */1 * * * * /opt/mpss/mpss.sh |
|||||||
| 92 | ``` |
|||||||
| 93 | ||||||||
| 94 | 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. |
|||||||
| 95 | ||||||||
| 96 | 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. |
|||||||