Skip to content

Instantly share code, notes, and snippets.

@satyadeepk
Last active April 27, 2016 17:01
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyadeepk/11393407 to your computer and use it in GitHub Desktop.
Save satyadeepk/11393407 to your computer and use it in GitHub Desktop.
Drain Parse.com logs to a file
#!/bin/sh
# Restartes the parse log if it crashes
# stdbuf flushes the Parse log buffer so they come in real time
until stdbuf -oL /usr/local/bin/parse log -f > info.log; do
echo "Parse log crashed with exit code $?. Respawning.." >&2
sleep 1
done
#!/bin/sh
cd /home/satyadeep/MyCloudCode/server
echo "Starting Parse log Checker..."
file=prod-info.log
prevsize=0
while true; do
actualsize=$(wc -c "$file" | cut -f 1 -d ' ')
if [ $actualsize -eq $prevsize ]; then
kill $(ps aux | grep './[l]og-prod' | awk '{print $2}')
kill $(ps aux | grep '[l]og -f prod-Addodoc' | awk '{print $2}')
sleep 10
echo "$(date) : Parse log not working! Restarting log script..." >> statuslog.log
./log-parse.sh &
fi
prevsize=$actualsize
sleep 330 #6 minutes
done
#!/bin/sh -e
#
# /etc/rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
test -e /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
#Start parse logging at startup
/home/satyadeep/MyCloudCode/server/log-parse.sh &
/usr/local/bin/remote_syslog -c /home/satyadeep/MyCloudCode/logs/config/logs.yml
/home/satyadeep/MyCloudCode/server/logcheck.sh &
exit 0
@satyadeepk
Copy link
Author

There is one little problem with parse log -f though. It buffers the stdout output and hence the final few lines are not tailed immediately.

Possible workaround:
http://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdin-is-interactive-not-a-pipe

http://empty.sourceforge.net/

@satyadeepk
Copy link
Author

Fixed the stdout buffer issue by using the stdbuf command. It works like magic now! :)
https://www.gnu.org/software/coreutils/manual/html_node/stdbuf-invocation.html

@jeveloper
Copy link

Correction #1 command is parse logs -f
#2 log-prod-info.sh should be log-parse.sh

Another issue is , this should be using an init.d or service , can't be something that happens upon login of a user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment