3

I have scanned various resources and tried various things, but to no avail.

Here's the situation;

I have an EC2 instance which runs a node.js app. I also want to run a bash script (A loop with intermediate sleep) in the background which checks if there are any new changes to the git repository of the node app.

In the user-data part of my instance, I have this;

#!/bin/bash
su - ec2-user -c 'cd sample-node-app; node index.js'
su - ec2-user -c 'bash /home/ec2-user/check_git.sh &'

The script, adapted from here

#!/bin/bash
while true
do

#move into your git repo
cd ~/sample-node-app;

git fetch;
LOCAL=$(git rev-parse HEAD);
REMOTE=$(git rev-parse @{u});

#if our local revision id doesn't match the remote, we will need to pull the changes
if [ $LOCAL != $REMOTE ]; then
    #pull and merge changes
    git pull origin master;
    git reset --hard origin/master
    killall node

    npm start;
fi
sleep 5
done
  • Tried suggestions from here, here and adding the file path to rc.local. When I did the rc.local part, the node app terminated automatically on each start.
  • Have set permissions for the script using chmod +x check_git.sh.
  • Tried variations of user-data input like bash /home/ec2-user/check_git.sh and ./check_git.sh (I have #!/bin/bash at the top of the script.)

Thanks in advance.

  • 1
    Including the script would be very useful. – Simon Greenwood Dec 2 '17 at 17:02
  • @SimonGreenwood just included it there. – MNS Dec 2 '17 at 17:28
1

Managed to get it working by adding the following in my EC2 user-data;

#cloud-config

runcmd:
 - rm -rf /var/lib/cloud/*
 - su - ec2-user -c 'bash check_git.sh'
 - su - ec2-user -c 'cd sample-node-app; node index.js'

Haven't found a reason as to why it works yet, but working on it.

EDIT

I managed to break the above for some reason. Worked at times, but not consistently. Eventually, I resorted to the following;

In check_git.sh, I have an if-statement to check and run the app:

if ! pgrep -x "node" > /dev/null
then
    node index.js &
fi

And the following in user-data of the EC2 instance:

#!/bin/bash
sudo rm -rf /var/lib/cloud/*
su - ec2-user -c 'bash /home/ec2-user/check_git.sh'
  • At a guess, the original command is failing because $? is non-zero for some reason. For auto-starting node apps, pm2 or forever might be better options than a bash script. – Simon Greenwood Dec 3 '17 at 9:00
  • What do you mean by $? is non-zero? Also, I managed to break it again, I believe it was beginners luck when it was working last night. – MNS Dec 3 '17 at 15:55
  • 1
    $? is the exit status of a process, but it's not correct here as while ... true should loop until the process is killed. Does the script run from the command line? I would handle the node process with a process manager, and for that matter your git query would also work with systemd if you are on CentOS 7 or a recent Debian/Ubuntu. – Simon Greenwood Dec 3 '17 at 16:24
  • Thank you @SimonGreenwood for the explanation and suggestion. It runs perfectly from the command line. I shall tinker around with it. I am running into issues with node and npm paths (unable to run sudo npm . . . to install pm2) for some reason despite following this – MNS Dec 3 '17 at 16:35
  • It's likely that the runcmd shell doesn't have the right environment set up so doesn't know where node is. Try adding the path to the binary in in full or set up an environment for the script (not sure how you'd do this with AWS but it should be possible). – Simon Greenwood Dec 3 '17 at 17:16

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.