2

I'm trying to set up a Raspberry Pi to check a repo on startup and then fire up a node script with forever.

I got the second part working, but I tried a dozen git commands with no success.

Here is my crontab that I access like so:

crontab -u pi -e

@reboot /bin/sh /home/pi/code/script.sh

Now my script has -rwxr-xr-x access rights and goes like so:

#!/bin/sh
cd /home/pi/code
/usr/bin/sudo -u pi -H /usr/bin/git pull origin master

/usr/bin/sudo -u pi -H /home/pi/.nvm/v0.11.11/bin/forever start /home/pi/code/server.js

Forever starts the server.js on reboot, no problem, but the repo never gets updated. If I run the script using sh /home/pi/code/script.sh it triggers git pull correctly... I initially set up an alias for git pull to be git up like it is recommended, but figured it might be my problem and I went back to the simplest version I could. Still no success.

Any input is welcome.

EDIT: the output of the crontab indicates connectivity issue: Could not resolve host: bitbucket.org how can I wait for network to be setup before I run the script?

  • could you check the mails of root if there is some error, and what error? (xmail if you like UI of 70s, "less /var/spool/mail/root" a still better "interface") – Giacomo Catenazzi Mar 2 '16 at 8:18
  • Why are you using sudo? Doesn't the crontab belong to the user pi? Unless it is being run by root, that will hang, waiting for a password. In any case, try adding 2>>/home/pi/cronlog at the end of the crontab command. Then, reboot and check the contents of /home/pi/cronlog. If anything is there, edit it into your question. – terdon Mar 2 '16 at 8:23
1

Instead of using crontab, you should create a simple service that depends on network:

#!/bin/bash
# /etc/init.d/XY

### BEGIN INIT INFO
# Provides:          XY
# Required-Start:    $syslog $network
# Required-Stop:     $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This service is used to start XY
### END INIT INFO


case "$1" in 
    start)
        echo "Updating XY"
        cd /home/pi/code
        /usr/bin/sudo -u pi -H /usr/bin/git pull origin master
        echo "Starting server.js"
        /usr/bin/sudo -u pi -H /home/pi/.nvm/v0.11.11/bin/forever start /home/pi/code/server.js
        ;;
    stop)
        echo "Stopping XY"

        ;;
    *)
        echo "Usage: /etc/init.d/XY start|stop"
        exit 1
        ;;
esac
exit 0

Put it into the service folder and make it executable:

sudo chmod +x /etc/init.d/XY
  • The issue with that is that this script would be "written in stone", I'd like to keep the script itself under source control in case I want to add commands to it later. (The idea is that I won't have direct access to the Pi). But I guess I can just have the init.d fire up a shell script! – LukeS Mar 2 '16 at 9:08
  • Yes, then just put your commands in a source controlled script and execute only that from the service. – Phlogi Mar 2 '16 at 9:16
  • I'm trying this out right now – LukeS Mar 2 '16 at 9:37
1

After getting help to debug and trying out Phlogi's solution without success, I decided to go back to the original crontab and just add code to wait for the network interface to be ready. Here is what the script looks like now:

#!/bin/sh

while ! ping -c 1 -W 1 bitbucket.org; do
    echo "Waiting for bitbucket - network interface might be down..."
    sleep 1
done

cd /home/pi/code && /usr/bin/sudo -u pi -H git checkout master && /usr/bin/sudo -u pi -H git up

/usr/bin/sudo -u pi -H /home/pi/.nvm/v0.11.11/bin/forever start    /home/pi/code/server.js

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.