Hosted Server - How can occ commands be run without sudo?

I am running Nextcloud on a hosted system - 1and1 (the US version of 1und1). In upgrading to Nextcloud 14, I encountered this error:

The database is missing some indexes. Due to the fact that adding indexes on big tables could take some time they were not added automatically. By running “occ db:add-missing-indices” those missing indexes could be added manually while the instance keeps running. Once the indexes are added queries to those tables are usually much faster.

How can I run “occ db:add-missing-indices” when I have don’t have the ability to execute this command as the HTTP user?

One of the NextCloud developers is probably going to kill me for suggesting such an ugly hack…

However you could try dropping this someplace 1and1 has defined as a CGI script directory:

#!/bin/bash
echo Content-Type: text/plain
echo
echo Attempting to run occ
nohup /usr/bin/php7.1-cli /full_path_to_your_website/nextcloud/occ db:add-missing-indices
echo Initiated attempted resulted in exit code $?

Then you can visit the cgi script to cause occ to run as the web server user. Hopefully the nohup will allow the process to proceed even beyond whatever CGI script timeout limit 1and1 has set. If nohup doesn’t allow it to continue to completion, I am guessing this has the potential to corrupt your nextcloud database so you may want to make a backup of it before trying. Failure to complete may also lock you nextcloud in maintenance mode?

Otherwise, if you aren’t feeling like trying ugly hacks, try contacting 1and1 support and they may have a recommendation.

1 Like

Hi,

I am on an hosted environment as well (not on 1 und / and 1) and I used a cronjob (if available) to run that command.
My hoster uses plesk. So I was running occ as a php script with the arguments db:add-missing-indices. That did the trick.

Thank you for the answers. As I understand what needs to be done is that the occ command needs to be executed by getting the webserver (in my case, 1and1’s Apache server) for the command to be effective.

Here’s what I did, using the suggestions. I am at the limit of my understanding / not understanding of how all of this works so comments on how to improve what I did are welcomed.

I modified my .htaccess file with the following:

Options +ExecCGI
AddHandler cgi-script .sh

This allows shell scripts to be executed from the browser and my source for this information was from here:

https://www.siteground.com/kb/how_to_run_simple_shell_scripts_directly_from_a_browser/

I then used the suggested shell script and created a file “fixindex.sh” in my nextcloud root directory.:

#!/bin/bash
echo Content-Type: text/plain
echo
echo Attempting to run occ
nohup /usr/bin/php7.1-cli /full_path_to_your_website/nextcloud/occ db:add-missing-indices
echo Initiated attempted resulted in exit code $?

To make the script executable, I changed the permissions to 755:
chmod 755 fixindex.sh

To determine my path for my website, I used pwd. For 1and1, it takes the form of:
/kunden/homepages/account#/htdocs/sitename/nextcloud/

I then went to my browser and executed:
https://sitename/nextcloud/fixindex.sh

The browser returned

Attempting to run occ
Initiated attempted resulted in exit code 127

I tired to troubleshoot why I was getting an exit code of 127 which is when a command is not found within your PATH but could not eliminate the error message.

Oddly enough, the Administrative panel of Nextcloud no longer says there are missing indexes so it is a mystery to me as to how the command ran correctly even though I was receiving the error code.

Again, I appreciate the help and what is generally useful is that I now have a method for running occ commands without having command line ability to run as the HTTP user.

Does anyone know how the command ran even though I was obtaining an error code that it wasn’t?

2 Likes

Hi, I ran the script to me the error 126 is returned, did you solve it? thank you so much

No, I never figured out why I was receiving the error codes. I had two versions that I tried, one with the nohup command and one without it.

Trying several changes of the shell script in rapid succession, one of them must have worked and my database indexes were corrected. Unfortunately, I never figured out which version worked and having other things to worry about, I moved on.

I would suggest examining your syntax and path to see if you can spot any errors you might have missed.

Please see