1

I'm following youtube videos and online courses, learning about Linux command, enhancing skills, trying to get good. Currently trying to understand scheduling.

My Raspberry Pi is running owncloud on headless Raspbian. I want to schedule it to put owncloud in to maintenance mode, apt-get update then apt-get upgrade, then take owncloud out of maintenance mode, once a week. Owncloud (php) commands should be issued from user www-data, whereas apt-get should be run as root. So far, I've tried this:

sudo -u www-data crontab -e

Adding the line 0 7 * * 1 php /var/www/owncloud/occ maintenance:mode --on

then

sudo crontab -e

adding line 1 7 * * 1 apt-get update && apt-get upgrade -y

But here I get stuck. How do I tell the system to bring owncloud out of maintenance mode after apt-get upgrade -y returns exit status 0? I thought about making the entry in root's crontab as

1 7 * * 1 apt-get update && apt-get upgrade && php /var/www/owncloud/occ maintenance:mode --off

but maintenance:mode --off fails as it isn't run by user www-data. I could schedule the last command to run minutes later as www-data but I'd rather it run when apt-get upgrade -y gives exit status 0. Is there a way of doing this?

1

su and friends to the rescue! su, sudo, and super can all be used here. su is traditional, sudo is the easiest, and super has advantages if you want users to be able to do parts of this.

Since you specifically mentioned testing the exit status of apt-get upgrade you would use 1 7 * * 1 apt-get update && apt-get upgrade && sudo -u www-data php /var/www/owncloud/occ maintenance:mode --off.

Now this is getting a bit long so following thrig's advice and putting it in a script has advantages, but his example has two shortcomings: it does not check return values and the maintenance mode on command is directly followed by the next command, and I assume that there was a reason for the delay between entering maintenance mode and starting apt-get, so you might want something more like this:

#!/bin/sh
set -e
sudo -u www-data php /var/www/owncloud/occ maintenance:mode --on || sudo -u www-data php /var/www/owncloud/occ maintenance:mode --on
sleep 1m || true
apt-get update || apt-get update
apt-get upgrade -y || apt-get install
sudo -u www-data php /var/www/owncloud/occ maintenance:mode --off || sudo -u www-data php /var/www/owncloud/occ maintenance:mode --off

But you are off to a good start.

  • With set -e if the updates fail then the system remains in maintenance mode as the --off will not run; this may or may not be desirable. – thrig Jul 3 '17 at 17:43
  • yup, @thrig, I thought about that, and I agree that the desirability is debatable, but I could not think of any good that would com from a failed upgrade modifying a package used by the app and the app scrambling all it's data when you restart. On the other hand some self repair is not too hard . . . – hildred Jul 3 '17 at 17:50
  • Thanks for the suggestions! There's no real reason to separate the maintenance:mode --on job except that it is in crontab for user www-data. So, answer is forming in my mind along the lines of 0 7 * * 1 sudo -u www-data php /var/www/owncloud/occ maintenance:mode --on && apt-get update && apt-get upgrade && sudo -u www-data php /var/www/owncloud/occ maintenance:mode --off (the whole command works unscheduled) I was unsure as to how sudo is handled in a scheduled task. My aim was to have owncloud remain in maintenance mode if the update fails as a way of alerting me to an error. – David1618 Jul 3 '17 at 23:29
  • @David1618, when sudo is run by root no password is needed in a standard configuration because, well root. – hildred Jul 3 '17 at 23:45
0

You probably want a single script that performs all the necessary steps in sequence:

#!/bin/sh
sudo -u www-data php /var/www/owncloud/occ maintenance:mode --on
apt-get update && apt-get upgrade -y
sudo -u www-data php /var/www/owncloud/occ maintenance:mode --off

And then run that from root's crontab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.