Last active
August 29, 2015 14:04
-
-
Save hktonylee/bca92ae458b98b74693b to your computer and use it in GitHub Desktop.
Run task/daemon in the background
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
#!/bin/bash | |
# **** You may add `exec` **** | |
# eg. | |
# exec python manage.py runserver 0.0.0.0:9000 |
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
#!/bin/bash | |
SCRIPT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
LOG_FILE=$SCRIPT_ROOT/'run_server.log' | |
PID_FILE=$SCRIPT_ROOT/'run_server.pid' | |
EXECUTABLE=$SCRIPT_ROOT/'run.sh' | |
EXE_ARGUMENTS=() |
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
#!/bin/bash | |
# kill first, also imported config | |
source 'server_stop.sh' | |
# and start in daemon | |
nohup "$EXECUTABLE" "${EXE_ARGUMENTS[@]}" > "$LOG_FILE" 2>&1 0<&- & | |
echo -n "" > "$PID_FILE" # clear the file | |
sleep 1; | |
allChildren=`ps x -o "%p %r %c" | grep -v "COMMAND" | grep $$ | cut -d' ' -f 1` | |
for child in $allChildren; do | |
name=`ps -o comm $child | head -2 | tail -1` | |
if [ "$name" != "COMMAND" ]; then | |
echo "PID: $child ($name)" | |
echo "" >> "$PID_FILE" | |
echo -n "$child" >> "$PID_FILE" | |
fi | |
done; | |
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
#!/bin/bash | |
source 'server_config.sh' | |
set +e | |
pid1=( | |
`cat $PID_FILE 2>&1` | |
) | |
# Additional PIDs | |
# pid1=( | |
# $pid1 | |
# `cat RUNNING_PID 2>/dev/null` | |
# ) | |
if [ "$?" -eq "0" ]; then | |
for pid in "${pid1[@]}" ; do | |
echo "Killing server ($pid)..." | |
kill "$pid" > /dev/null 2>&1 | |
done; | |
fi | |
# or `pkill -TERM -P "$pid1"` | |
set -e | |
while ps -p "$pid1" > /dev/null 2>&1 ; do | |
sleep 1; | |
done; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment