Created
January 18, 2017 10:18
-
-
Save spritle/7544ddd1e48b6cdf34a7e434d0c41da2 to your computer and use it in GitHub Desktop.
Monitor Sidekiq up or down with Shell Script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process='sidekiq' # process name, you can use this to monitor any process | |
project_path="/home/ubuntu/apps/demo/" # This is the one you got to change on your side for your server. | |
#change it based on your configs. | |
start_service="bundle exec sidekiq -d -C /home/ubuntu/apps/demo/config/sidekiq.yml -i 0 -P /home/ubuntu/apps/demo/tmp/pids/sidekiq.pid -L /home/ubuntu/apps/demo/log/sidekiq.log" | |
# Method to simply store last run time in monitor.log file. | |
function update_last_run() { | |
current_time=$(date +'%d/%m/%Y %H:%M:%S') | |
echo "Last run: $current_time" > monitor.log | |
} | |
# Method to start the service, simply we need to navigate to project folder and run the start process command. $? will return status code of last command, if is not equal to zero then some thing went wrong. Successful command will always return 0 as exit code. | |
function start_service() { | |
cd $project_path && $start_service | |
if [ "$?" == "0" ] | |
then | |
echo "$process started successfully" | |
else | |
echo "Could not start process $process" | |
fi | |
} | |
function check_process() | |
{ | |
process_id=$(/bin/ps -fu $USER| grep $process | grep -v "grep" | awk '{print $2}') | |
echo "checking process $process" | |
# if $process_id is empty, then it got stopped and you have to start it again | |
if [ "$process_id" == "" ] | |
then | |
echo "Process is not running" | |
echo "Starting the process" | |
start_service | |
else | |
echo "Process is running, PID: $process_id" | |
fi | |
update_last_run | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment