A template with handy scripts to test and run programs as a daemon in the background, with stdout
& stderr
logs and log rotation built-in. The target program needs zero modification.
Requires Python 3.6+ for log rotation.
Note
All relative paths are relative to the location of the scripts.
- Use this template to create a new repository; or copy the scripts to your existing project (any location is fine, but do place them together).
- Open
start.sh
and modifyCMD
to set the command to run. Add setup commands (change directory, activate virtual environment, etc.) if needed. - (Optional) Modify
startd.sh
to change the logging and rotation settings.# log directory LOG_DIR=logs # --- # log file (will be rotated) LOG_PATH=$LOG_DIR/server.log # max log size in MB MAX_LOG_SIZE=5 # --- # log file of log rotation script (will be rotated) ROTATE_FILE=$LOG_DIR/rotate.log # max log size for rotation output in MB MAX_ROTATION_LOG_SIZE=5 # --- # python executable used to run log rotation script PYTHON_CMD=python3 # current directory CURRENT_DIR=$(dirname ${BASH_SOURCE[0]}) # log rotation script LOG_ROTATION_PY=$CURRENT_DIR/rotatelog.py # start script START_SCRIPT=$CURRENT_DIR/start.sh
./start.sh
: Run the command in the terminal.
./startd.sh
: Run the command as a daemon process in the background (return if already running), and also start log rotation using rotatelog.py
. This script is dependent on start.sh
and rotatelog.py
.
- both processes are run in the background with their PIDs stored in
.vars/server.pid
and.vars/rotate.pid
, and they communicate using a fifo file.vars/.fifo
. Do NOT manually modify or delete these files. - server logs are stored in
logs/server.log
and rotated every 5MB (can be changed instartd.sh
) - rotation logs are stored in
logs/rotate.log
and rotated every 5MB (can be changed instartd.sh
)
./statusd.sh
: Check the status of the command and the log rotation process running in the background.
./stopd.sh
: Stop the command and the log rotation process running in the background.