Compile jdk image with Supervisor+Dockerfile

1, Bash script set command

1. Introduction

Set command is an important part of bash script, but it is often ignored, resulting in problems in the security and maintainability of the script. When bash executes the script, it will create a new Shell. If there is an error in the execution process, bash will ignore the error and continue to execute. Bash only displays an error and does not terminate the execution. The environment variables of set and Shell will be displayed directly

#Show all environment variables
set
#View environment variable bytes
set | wc -l

2. set common commands

2.1 set -u

When executing a script, Bash ignores a non-existent variable by default

set -u is used to change this behavior. If the script adds it to the header, it will report an error when encountering a non-existent variable and stop execution;

-There is another way to write u - o nounset. The two are equivalent

#!/usr/bin/env bash
set -u
echo $a
echo bar
#Operation results
$ bash script.sh
bash: script.sh:Line 4: a: Unbound variable

2.2 set -x

By default, after the script is executed, the screen only displays the running results and nothing else. If multiple commands are executed continuously, their running results will be output continuously. Sometimes it's hard to tell what commands produce a certain paragraph

set -x is used to output the executed command line before running the result

-There is another way to write x - o xtrace

#!/usr/bin/env bash
set -x
echo bar
#Run the result. If no statement is run, one will be displayed
$ bash script.sh
+ echo bar
bar

2.3 set -e

When this variable is set, the script terminates execution whenever an error occurs

#!/usr/bin/env bash
set -e
foo
echo bar
#Execution result, script sending error, stop execution
$ bash script.sh
script.sh:Line 4: foo: Command not found

set -e determines whether a command fails according to the return value. However, the non-zero return value of some commands may not indicate failure, or the developer hopes that the script will continue to execute in case of command failure. At this time, set -e can be closed temporarily. After the command is executed, set -e can be reopened;

set +e means to turn off the - e option, and set -e means to turn on the - e option again.

-There is another way to write e - o errexit

2.4 set -o pipefail

One exception to set -e is that it does not apply to pipeline commands. The so-called pipeline command means that multiple sub commands are combined into a large command through the pipeline operator (|). Bash will take the return value of the last subcommand as the return value of the whole command. In other words, as long as the last sub command does not fail, the pipeline command will always execute successfully, so the commands behind it will still execute, and set -e will fail.

#!/usr/bin/env bash
set -eo pipefail
foo | echo a
echo bar

2.5 summary

In general, the above four parameters of the set command are generally used together

# Writing method I
set -euxo pipefail

# Writing method 2
set -eux
set -o pipefail

#Run the bash command
bash -euxo pipefail script.sh

2, Supervisor practice

Introduction to the official website: http://supervisord.org/

1. Introduction

Supervisor is a set of general process management program developed in Python. It can change an ordinary command-line process into a background daemon, monitor the process status, and automatically restart in case of abnormal exit. It starts these managed processes as subprocesses of supervisor through fork/exec, so as long as the path of the executable file of the process to be managed is written in the configuration file of supervisor. It also realizes that when the child process hangs, the parent process can accurately obtain the hanging information of the child process, and can choose whether to start and alarm by itself. Supervisor also provides a function to set a non root user for supervisor or each child process, and this user can manage its corresponding process.

2. Supervisor installation

Official installation documentation

It is recommended to use system tools for installation. It will start automatically after startup. Use yum for Centos and apt get for Ubuntu

yum install supervisor
apt install supervisor

The second way is to install with pip

pip install supervisor

3. Profile description

http://supervisord.org/configuration.html

3.1 introduction

  • supervisor configuration file: / etc / supervisor conf
    Using echo_ supervisord_ conf > supervisord. Conf generate default configuration file
    Note: the configuration file of supervisor is incomplete by default, but in most cases, the basic functions mentioned above have been met by default.
  • Subprocess configuration file path: / etc / Supervisor d/
    Note: the default subprocess configuration file is in ini format, which is in / etc / supervisor.exe In conf, [include] configure / etc / Supervisor d/*. ini

3.2 supervisor.conf configuration file description

[unix_http_server]
file=/tmp/supervisor.sock   ;UNIX socket Documents, supervisorctl Can use
;chmod=0700                 ;socket Document mode,The default is 0700
;chown=nobody:nogroup       ;socket Document owner,Format: uid:gid
 
;[inet_http_server]         ;HTTP Server, providing web Management interface
;port=127.0.0.1:9001        ;Web Manage background running IP And ports. If you open to the public network, you need to pay attention to security
;username=user              ;User name of login management background
;password=123               ;Password for login management background
 
[supervisord]
logfile=/tmp/supervisord.log ;Log file, default is $CWD/supervisord.log
logfile_maxbytes=50MB        ;Log file size, exceeding rotate,Default 50 MB,If set to 0, it means that the size is not limited
logfile_backups=10           ;The number of reserved backups of log files is 10 by default. If it is set to 0, it means no backup
loglevel=info                ;Log level, default info,other: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid file
nodaemon=false               ;Whether to start in the foreground. The default is false,Namely daemon Start by
minfds=1024                  ;The minimum value of the file descriptor that can be opened. The default value is 1024
minprocs=200                 ;The minimum number of processes that can be opened. The default is 200
 
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; Connect supervisor through UNIX socket, and the path is the same as UNIX_ http_ The file of server part is consistent
;serverurl=http://127.0.0.1:9001 ;  Connect to supervisor via HTTP
 
; [program:xx]Is the configuration parameter of the managed process, xx Is the name of the process
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; Program start command
autostart=true       ; stay supervisord It also starts automatically when starting
startsecs=10         ; If there is no abnormal exit after 10 seconds of startup, it means that the process is started normally. The default is 1 second
autorestart=true     ; Automatic restart after program exit,Optional values:[unexpected,true,false],Default to unexpected,Indicates that the process is restarted after being killed unexpectedly
startretries=3       ; The number of automatic retries after startup failure. The default is 3
user=tomcat          ; Which user is used to start the process? The default is root
priority=999         ; The process startup priority is 999 by default, and the one with small value will be started first
redirect_stderr=true ; hold stderr Redirect to stdout,default false
stdout_logfile_maxbytes=20MB  ; stdout Log file size, default 50 MB
stdout_logfile_backups = 20   ; stdout The number of log file backups. The default is 10
; stdout The log file cannot be started normally when the specified directory does not exist, so you need to create the directory manually( supervisord (log files are created automatically)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;Default to false,Whether to send to this process group when the process is killed stop Signals, including child processes
killasgroup=false     ;Default to false,Send to process group kill Signals, including child processes
 
;Include other configuration files
[include]
files = relative/directory/*.ini    ;You can specify one or more to.ini End profile

3.3 description of sub process configuration file

Write a configuration file for the sub process (program) to be managed and put it in / etc / Supervisor D / directory, with ini as extension

#Project name
[program:blog]
#Script directory
directory=/opt/bin
#Script execution command
command=/usr/bin/python /opt/bin/test.py

#Whether or not supervisor is started by default when it is True
autostart=true
#When the program exits, the program will not restart automatically. The default is unexpected. Set the automatic restart after the subprocess hangs up. There are three options: false,unexpected and true. If it is false, it will not be restarted under any circumstances. If it is unexpected, it is only when the exit code of the process is not defined in the following exit codes
autorestart=false
#This option is the number of seconds after the child process is started. If the status is running, we think it has been started successfully. The default value is 1
startsecs=1

#User identity of the script run 
user = test

#Log output 
stderr_logfile=/tmp/blog_stderr.log 
stdout_logfile=/tmp/blog_stdout.log 
#Redirect stderr to stdout. The default is false
redirect_stderr = true
#stdout log file size, 50MB by default
stdout_logfile_maxbytes = 20MB
#Number of stdout log file backups
stdout_logfile_backups = 20

4. supervisor command description

#Start the server first
#unix:///var/run/supervisor.sock no such file indicates that you are satisfied and start the server
supervisord -c /etc/supervisord.conf
systemctl stop supervisor.service

supervisorctl status        #View the status of all processes
supervisorctl stop es       #Stop es
supervisorctl start es      #Start es
supervisorctl restart       #Restart es
supervisorctl update        #After the configuration file is modified, use this command to load a new configuration
supervisorctl reload        #Restart all programs in the configuration
#You can also directly enter the shell interaction interface
supervisorctl

The sub process status chart can be viewed with status

3, Dockerfile compilation jdk instance

FROM openjdk:19-slim-buster
USER root
#DEBIAN_FRONTEND is an environment variable that tells the operating system where to get user input. If it is set to "non interactive", you can run the command directly,
#There is no need to request input from the user (all operations are non interactive). This is particularly useful when running the apt get command
ENV DEBIAN_FRONTEND noninteractive
ADD ./supervisord.conf /tmp/supervisord_add.conf
RUN set -ex \
&& apt-get update\
&& apt-get install -y python-pip \
&& pip install supervisor \
&& echo_supervisord_conf > /etc/supervisord.conf \
&& cat /tmp/supervisord_add.conf >> /etc/supervisord.conf \
&& mkdir -p /var/log/lamp \
&& rm /tmp/supervisord_add.conf
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
CMD ["supervisord","-c","/etc/supervisord.conf"]

Here, supervisor is used to manage the background process, which is similar to shell script, but this function is more powerful

# Set it not to run the supervisor process in the background, which is a foreground process for the container
nodaemon=true
# Redirect stderr to stdout. The default is false
redirect_stderr=true

# Declare the code block of the new program
[program:student]
# Command to start the program declared in this code block
command=java -Dfile.encoding=utf-8 -jar /opt/lamp/student.jar --server.port=8080
stdout_logfile=/var/log/lamp/student.log

Keywords: Linux bash

Added by mikesmith76 on Sun, 13 Feb 2022 02:04:02 +0200