NextCloud – Rescan Music Library for All Users

NextCloud is a kick-ass piece of open-source software for personal cloud file sharing, backing up cell phones, email, and other forms of data, syncing contact lists, online collaboration, communication, and a variety of other use cases, but over the last few years, I’ve come across a few pitfalls and annoyances that required manual interventions. In doing so, I’ve lost quite a few man-hours of my pitiful existence on this planet scouring forums and GitHub posts, so let my loss be your gain.

Now, there is already a button in the Music app to scan the library to rescan the library, but the kicker is, it doesn’t always detect when changes are made, and there isn’t a way to recall the script anywhere in the user interface to the best of my knowledge.

Often, many of the NextCloud maintenance commands require using PHP to run the “occ” script as your web server user account, most of which, I could never I could never remember after a few months or weeks in between, so I threw together a few Bash scripts to make life easier.

The basic command to rescan the music library for all NextCloud users is pretty simple.

occ music:scan --rescan --all

Where it starts getting hairy, is trying to remember how to run the sudo command under the Web Server’s user account, and then keep track of the actual command in your head after any amount of time has elapsed. Especially if you don’t use it everyday, so that’s where our script comes in.

I created “rescanmusic.sh” just below the live public html directory where the NextCloud installation sits on the web server, so that it’s not accessible externally. Edit the variables accordingly to match your installation environment.

touch rescanmusic.sh
chown www-data:www-data rescanmusic.sh
chmod 740 rescanmusic.sh
nano rescanmusic.sh
#!/bin/bash
# Author: Nathan Thomas
# Date: 04/29/2021
# Rescan NextCloud Music Library for All Users

# User account the web server is running under
WEB_USER='www-data'
# NextCloud installation directory
WEB_DIR='/var/www/html'

sudo -u ${WEB_USER} php ${WEB_DIR}/occ music:scan --rescan --all
Ctrl+X to save
Run as follows:
./rescanmusic.sh

One Reply to “NextCloud – Rescan Music Library for All Users”

Leave a Reply