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 therc.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.
Best Answer
Managed to get it working by adding the following in my EC2 user-data;
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:And the following in
user-data
of the EC2 instance: