I am invoking a binary file using shell script. Binary file is basically establish a socket client server. this binary works fine initially but after 60mints its is getting stop. I need to check if it is not running then start it again. here is my code :
sleep 3
busyboxValue=busybox
commandpid=`$busyboxValue pidof command`
echo ${commandpid} > /sdcard/CommandProcess.txt
if [ $commandpid -gt 0 ]
then
echo -16 > /proc/$commandpid/oom_adj
echo -1000 > /proc/$commandpid/oom_score_adj
fi
while [ true ]
do
commandpid=`$busyboxValue pidof command`
if [ $commandpid -gt 0 ]
then
echo "command binary still running"
else
echo "command binary Exited so restarting it"
/data/local/command 1200 &
commandpid=`$busyboxValue pidof command`
echo ${commandpid} > /sdcard/CommandProcess.txt
echo -16 > /proc/$commandpid/oom_adj
echo -1000 > /proc/$commandpid/oom_score_adj
fi
sleep 10
done
You can use the python supervisor utility, which will take care of re-starting a dead program for you.
https://pypi.python.org/pypi/supervisor
Related
I've been following a tutorial on connecting a raspberry pi to the AWS greengrass and I keep getting a segmentation fault on the final step. AWS provided me with this greengrassd shell script however when i run it I'm getting a segmentation fault. I have no idea why its throwing this error so any help would be appreciated.
AWS Greengrass Tutorial / RaspberryPi
Error
pi#raspberrypi:/greengrass/ggc/packages/1.1.0 $ sudo ./greengrassd start
Setting up greengrass daemon
Validating execution environment
Found cgroup subsystem: cpu
Found cgroup subsystem: cpuacct
Found cgroup subsystem: blkio
Found cgroup subsystem: memory
Found cgroup subsystem: devices
Found cgroup subsystem: freezer
Found cgroup subsystem: net_cls
Starting greengrass daemon./greengrassd: line 158: 2254 Segmentation fault nohup $COMMAND > /dev/null 2> $CRASH_LOG < /dev/null
Greengrass daemon 2254 failed to start
greengrassd script
#!/usr/bin/env bash
##########Environment Requirement for Greengrass Daemon##########
# by default, the daemon assumes it's going to be launched from a directory
# that has the following structure:
# GREENGRASS_ROOT/
# greengrassd
# bin/daemon
# configuration/
# group/group.json
# certs/server.crt
# lambda/
# system_lambda1/...
# system_lambda2/...
# root cgroup has to be mounted separately, this script doesn't do that for you.
#################################################################
set -e
PWD=$(cd $(dirname "$0"); pwd)
GGC_PKG_HOME=$(readlink -f $PWD)
GG_HOME=$(cd $GGC_PKG_HOME/../../; pwd)
CRASH_LOG=$GG_HOME/var/log/crash.log
GGC_ROOT_FS=$GGC_PKG_HOME/ggc_root
PID_FILE=/var/run/greengrassd.pid
FS_SETTINGS=/proc/sys/fs
GGC_GROUP=ggc_group
GGC_USER=ggc_user
MAX_DAEMON_KILL_WAIT_SECONDS=60
RETRY_SIGTERM_INTERVAL_SECONDS=20
if [ -z "$COMMAND" ]; then
COMMAND="$GGC_PKG_HOME/bin/daemon -core-dir=$GGC_PKG_HOME -greengrassdPid=$$"
fi
# Function ran as part of initial setup
setup() {
echo "Setting up greengrass daemon"
mkdir -p $GGC_ROOT_FS
# Mask greengrass directory for containers
mknod $GGC_ROOT_FS/greengrass c 1 3 &>/dev/null || true
mkdir -p $(dirname "$CRASH_LOG")
}
validatePlatformSecurity() {
if [[ -f $FS_SETTINGS/protected_hardlinks &&
-f $FS_SETTINGS/protected_symlinks ]]; then
PROT_HARDLINK_VAL=$(cat $FS_SETTINGS/protected_hardlinks)
PROT_SOFTLINK_VAL=$(cat $FS_SETTINGS/protected_symlinks)
if [[ "$PROT_HARDLINK_VAL" -ne 1 || "$PROT_SOFTLINK_VAL" -ne 1 ]]; then
echo "AWS Greengrass detected insecure OS configuration: No hardlink/softlink protection enabled." | tee -a $CRASH_LOG
exit 1
fi
fi
}
validateEnvironment() {
echo "Validating execution environment"
# ensure all commands that the installation script is going to use are available
if ! type grep >/dev/null ; then
echo "grep command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type cat >/dev/null ; then
echo "cat command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type awk >/dev/null ; then
echo "awk command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type id >/dev/null ; then
echo "id command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type ps >/dev/null ; then
echo "ps command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type sqlite3 >/dev/null ; then
echo "sqlite3 command is NOT on the path or is NOT installed on the system"
exit 1
fi
# the script needs to be run as root
if [ ! $(id -u) = 0 ]; then
echo "The script needs to be run using sudo"
exit 1
fi
if ! id $GGC_USER >/dev/null ; then
echo "${GGC_USER} doesn't exist. Please add a user ${GGC_USER} on the system"
exit 1
fi
if ! grep -q $GGC_GROUP /etc/group ; then
echo "${GGC_GROUP} doesn't exist. Please add a group ${GGC_GROUP} on the system"
exit 1
fi
# ensure that kernel supports cgroup
if [ ! -e /proc/cgroups ]; then
echo "The kernel in use does NOT support cgroup."
exit 1
fi
# assume that all kernel supported subsystems, which are listed in /proc/cgroups, are going to be used
# so check whether all of them are mounted.
for d in `awk '$4 == 1 {print $1}' /proc/cgroups`; do
if cat /proc/self/cgroup | grep -q $d; then
echo "Found cgroup subsystem: $d"
else
# exit with error if can't find cgroup
echo "The cgroup subsystem is not mounted: $d"
exit 1
fi
done
}
finish() {
pid=$1
echo "$pid" > $PID_FILE
echo ""
echo -e "\e[0;32mGreengrass successfully started with PID: $pid\e[0m"
exit 0
}
start() {
setup
if [[ $INSECURE -ne 1 ]]; then
validatePlatformSecurity
fi
validateEnvironment
trap 'finish $pid' SIGUSR1
echo ""
echo -n "Starting greengrass daemon"
if nohup $COMMAND >/dev/null 2>$CRASH_LOG < /dev/null &
then
pid=$!
# sleep 10 seconds to wait for daemon to start or exit
sleep 10 &
wait $!
echo ""
echo "Greengrass daemon $pid failed to start"
echo -e "\e[0;31m$(cat $CRASH_LOG)\e[0m"
exit 1
else
echo "Failed to start Greengrass daemon"
exit 1
fi
}
version() {
$GGC_PKG_HOME/bin/daemon --version
}
stop() {
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
echo "Stopping greengrass daemon of PID: $PID"
if [ ! -e "/proc/$PID" ]; then
rm $PID_FILE
echo "Process with pid $PID does not exist already"
return 0
fi
echo -n "Waiting"
kill "$PID" > /dev/null 2>&1
total_sleep_seconds=0
until [ "$total_sleep_seconds" -ge "$MAX_DAEMON_KILL_WAIT_SECONDS" ]; do
sleep 1
# If the pid no longer exists, we're done, remove the pid file and exit. Otherwise, just increment the loop counter
if [ ! -e "/proc/$PID" ]; then
rm $PID_FILE
echo -e "\nStopped greengrass daemon, exiting with success"
break
else
total_sleep_seconds=$(($total_sleep_seconds+1))
echo -n "."
fi
# If it has been $RETRY_SIGTERM_INTERVAL_SECONDS since the last SIGTERM, send SIGTERM
if [ $(($total_sleep_seconds % $RETRY_SIGTERM_INTERVAL_SECONDS)) -eq "0" ]; then
kill "$PID" > /dev/null 2>&1
fi
done
if [ $total_sleep_seconds -ge $MAX_DAEMON_KILL_WAIT_SECONDS ] && [ -e "/proc/$PID" ]; then
# If we are here, we never exited in the previous loop and the pid still exists. Exit with failure.
kill -9 "$PID" > /dev/null 2>&1
echo -e "\nProcess with pid $PID still alive after timeout of $MAX_DAEMON_KILL_WAIT_SECONDS seconds. Forced kill process, exiting with failure."
exit 1
fi
fi
}
usage() {
echo ""
echo "Usage: $0 [FLAGS] {start|stop|restart}"
echo ""
echo -e "[FLAGS]: \n -i, --insecure \t Run GGC in insecure mode without hardlink/softlink protection, (highly discouraged for production use) \n -v, --version \t\t Outputs the version of GGC."
echo ""
exit 1
}
if [[ $# -eq 0 ]]; then
usage
fi
for var in "$#"
do
case "$var" in
-v|--version)
version
exit 0
;;
esac
done
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i|--insecure)
mkdir -p $(dirname "$CRASH_LOG")
echo "Warning! You are running in insecure mode, this is highly discouraged!" | tee -a $CRASH_LOG
INSECURE=1
;;
-h|--help)
usage
;;
start)
stop
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
esac
shift
done
#Jim Maybe check the model of Pi you are using?
It seems that the Pi version of Greengrass is for ARMv7-A. I got this problem too and I'm using an older Model 1 B+ which is ARMv6Z (https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications).
The error we're seeing for line 158 is the ./greengrassd script waiting for the actual process to run:
sudo /greengrass/ggc/packages/1.1.0/bin/daemon -core-dir=/greengrass/ggc/packages/1.1.0 -greengrassdPid=641
/greengrass/ggc/packages/1.1.0/bin/daemon is the binary. If you run the above command directly in the console it exits with the same segmentation fault error.
AWS do recommend using the Pi 3 so I'm guessing it will work on that.
I'm trying to create a script which will only continue when pinging is unresponsive.
I'm running into 2 main issues. One being that it will require 2 CTL-C commands to end the script, and the other issue being that the script will never end and will need killing.
Here are my attempts;
rc=0
until [ $rc -gt 0 ]
do
ping 69.69.69.69 > /dev/null 2>&1
rc=$?
done
## Here will be the rest of code to be executed
I feel this one above is very close as it requires the 2 CTL-C commands and continues.
Here is something I found on SO but can't get to work at all.
counter=0
while [ true ]
do
ping -c 1 69.69.69.69 > /dev/null 2>&1
if [ $? -ne 0 ] then
let "counter +=1"
else
let "counter = 0"
fi
if [ $counter -eq 10 ] then
echo "Here should be executed once pinging is down"
fi
done
Any assistance is greatly appreciated, Thank you.
Try this:
while ping -c 1 -W 20 "$host" >& /dev/null
do
echo "Host reachable"
sleep 10 # do not ping too often
done
echo "Host unreachable within 20 seconds"
There's a few issues with that:
Firstly the if statements are wrong
Use this format
if [ $? -ne 0 ] then;
or this other format
if [ $? -ne 0 ]
then
Secondly I suspect that your pings are not timing out
ping -c 1 -w 2 69.69.69.69 > /dev/null 2>&1
might be helpfull
Thirdly your loop will continue incrementing counter even after 10. You might want to quit after 10
while [ $counter -le 10 ]
If you would be happy with executing something if ping times out after x seconds without any response it could be all condensed (ten seconds in the example below):
ping -c 1 -w 10 69.69.69.69 >/dev/null 2>&1 || echo "run if ping timedout without any response"
Can any one let me know how to re-run the process automatically again if it gets dies. Below is my code which i have tired but it is giving me an error :
Edited Code :
commandpid=`$busyboxValue pidof command`
echo ${commandpid} > /sdcard/CommandProcess.txt
if [ $commandpid -gt 0 ]
then
echo -16 > /proc/$commandpid/oom_adj
echo -1000 > /proc/$commandpid/oom_score_adj
fi
while [ true ]
do
echo "PID $commandpid"
if [ ps -p $commandpid > /dev/null ]
then
echo "[$(date)] :Process is already running with PID $commandpid"
else
echo "[$(date)] :Process is not running"
/data/local/command 1200 &
commandpid=`$busyboxValue pidof command`
echo ${commandpid} > /sdcard/CommandProcess.txt
fi
sleep 10
done
I have updated the code but still with the above code multiple process id i am getting. Dose it mean my else part code is keep executing? If yes the what changes i need to do to stop it?
Can please any one help me ?
To repeatedly run a command, regardless of exit status:
while :; do command; done
To repeatedly run a command until a successful exit:
until command; do :; done
I am trying to generate an issue regarding HashMap.put function.I have written a test code which will run more than 100 threads.. by using jstack or kill I'am able to get the thread dump of a particular thread of my process..The problem is I can not capture the thread dump immediately, I want all the thread dumps to be logged in a file until the process ends.Is there any linux command or shell script that can write to do this?
#!/bin/bash
if [ $# -eq 0 ]; then
echo >&2 "Usage: jstackSeries [ <count> [ <delay> ] ]"
echo >&2 " Defaults: count = 10, delay = 1 (seconds)"
exit 1
fi
pid=$1 # required
count=${2:-10} # defaults to 10 times
delay=${3:-1} # defaults to 1 second
while [ $count -gt 0 ]
do
jstack $pid >jstack.$pid.$(date +%H%M%S.%N)
sleep $delay
let count--
echo -n "."
done
refer here:
http://howtodoinjava.com/2012/12/19/how-to-get-thread-dump-in-linux-using-jstack/
I'm trying to develop a test case for a program, and would like to fail the test case if it runs over 4 seconds.
How can this be done on linux? (I'm using Ubuntu)
I know I can time the execution and fail it time > 4, but that's just a bad approach.
Thanks!
(shell script solution)
Runs your testcase in background, get the PId of it, and checks after 4 seconds if the process is still running.
wait_seconds=4
interval_seconds=0.5
run_your_test_case &
pid=$!
max=`expr "$wait_seconds / $interval_seconds"`
for (( I=0; I<$max; I++ ));do
if kill -0 $pid >/dev/null;then
echo 'test failed'
else
echo 'test ok'
break
fi
sleep $interval_seconds
done
Final solution:
1 ./slowprogram.sh >/dev/null &
2 pid=$!
3 exitbreak=0
4 for c in {0..4}; do
5 sleep 1
6 kill -0 $pid 2>/dev/null
7 if [ $? -ne 0 ] ;then
8 exitbreak=1
9 break
10 fi
11 done
12 if [ $exitbreak == 1 ]; then
13 echo '[ OK ]'
14 else
15 echo '[FAIL]'
16 kill -9 $pid 2>/dev/null
17 fi
You could also do something like <command> & sleep 5 && kill ${!}