2016-10-14 1 views
1

Je bâtis ma demande de démarrage de printemps comme exécutable en utilisant cette chaussure printemps configuration plugin Maven:fichier pid n'a pas été créé en cours d'exécution application de démarrage au printemps comme un service systemd

<plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
    <configuration> 
     <executable>true</executable> 
    </configuration> 
</plugin> 

alors j'ai créé le fichier de l'unité systemd suivante pour créer le service: Mais au démarrage de l'application, le fichier /var/run/ss7tm.pid n'est pas créé.

[Unit] 
Description=ss7tm 
After=syslog.target 

[Service] 
User=root 
PIDFile=/var/run/ss7tm.pid 
ExecStart=/root/ss7-1.0-SNAPSHOT.jar -Djava.security.egd=file:/dev/./urandom 
SuccessExitStatus=143 

[Install] 
WantedBy=multi-user.target 

Qu'est-ce qui ne va pas? J'utilise botte printemps 1.3.5

+0

Est-ce que vous exécutez votre application en tant que root? –

+0

yes, User = root dans [Service] – Max

Répondre

1

Comme on le voit dans la documentation de PIDFile= dans man systemd.service:

> systemd will not write to the file configured here 

Votre application doit écrire dans le fichier PID, systemd lit tout de lui.

Si vous ne savez pas où trouver la documentation pour une directive systemd particulière, vous pouvez vérifier man systemd.directives qui répertorie toutes les directives et où elles sont documentées. En outre, il n'est pas nécessaire d'inclure User=root. systemd s'exécute en tant que root par défaut.

0

Construire le fichier jar avec l'option spring boot maven plugin executable = true génère un fichier exécutable avec le script suivant dans les premiers octets du fichier. De ce que je vois ce script, lancez la commande java et créez un fichier pid.

Je suppose que la variable $ pid_file est obtenue à partir du fichier d'unité systemd, mais peut-être que je me trompe.

Je ne suis pas un expert du script shell, donc je ne suis pas sûr quelle partie du script est exécutée par systemd.

J'ai suivi cette documentation http://docs.spring.io/spring-boot/docs/1.3.5.RELEASE/reference/html/deployment-install.html

#!/bin/bash 
# 
# . ____   _   __ _ _ 
# /\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
# (()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
# \\/ ___)| |_)| | | | | || (_| | )))) 
# ' |____| .__|_| |_|_| |_\__, |//// 
# =========|_|==============|___/=/_/_/_/ 
# :: Spring Boot Startup Script :: 
# 

### BEGIN INIT INFO 
# Provides:   ss7 
# Required-Start: $remote_fs $syslog $network 
# Required-Stop:  $remote_fs $syslog $network 
# Default-Start:  2 3 4 5 
# Default-Stop:  0 1 6 
# Short-Description: ss7 
# Description:  Spring Boot Starter Parent 
# chkconfig:   2345 99 01 
### END INIT INFO 

[[ -n "$DEBUG" ]] && set -x 

# Initialize variables that cannot be provided by a .conf file 
WORKING_DIR="$(pwd)" 
# shellcheck disable=SC2153 
[[ -n "$JARFILE" ]] && jarfile="$JARFILE" 
[[ -n "$APP_NAME" ]] && identity="$APP_NAME" 

# Follow symlinks to find the real jar and detect init.d script 
cd "$(dirname "$0")" || exit 1 
[[ -z "$jarfile" ]] && jarfile=$(pwd)/$(basename "$0") 
while [[ -L "$jarfile" ]]; do 
    [[ "$jarfile" =~ init\.d ]] && init_script=$(basename "$jarfile") 
    jarfile=$(readlink "$jarfile") 
    cd "$(dirname "$jarfile")" || exit 1 
    jarfile=$(pwd)/$(basename "$jarfile") 
done 
jarfolder="$(dirname "$jarfile")" 
cd "$WORKING_DIR" || exit 1 

# Source any config file 
configfile="$(basename "${jarfile%.*}.conf")" 
# shellcheck source=/dev/null 
[[ -r "${jarfolder}/${configfile}" ]] && source "${jarfolder}/${configfile}" 

# Initialize PID/LOG locations if they weren't provided by the config file 
[[ -z "$PID_FOLDER" ]] && PID_FOLDER="/var/run" 
[[ -z "$LOG_FOLDER" ]] && LOG_FOLDER="/var/log" 
! [[ -x "$PID_FOLDER" ]] && PID_FOLDER="/tmp" 
! [[ -x "$LOG_FOLDER" ]] && LOG_FOLDER="/tmp" 

# Set up defaults 
[[ -z "$MODE" ]] && MODE="auto" # modes are "auto", "service" or "run" 
[[ -z "$USE_START_STOP_DAEMON" ]] && USE_START_STOP_DAEMON="true" 

# Create an identity for log/pid files 
if [[ -z "$identity" ]]; then 
    if [[ -n "$init_script" ]]; then 
    identity="${init_script}" 
    else 
    identity=$(basename "${jarfile%.*}")_${jarfolder//\//} 
    fi 
fi 

# Initialize log file name if not provided by the config file 
[[ -z "$LOG_FILENAME" ]] && LOG_FILENAME="${identity}.log" 


# ANSI Colors 
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; } 
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; } 
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; } 

# Utility functions 
checkPermissions() { 
    touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; return 4; } 
    touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; return 4; } 
} 

isRunning() { 
    ps -p "$1" &> /dev/null 
} 

await_file() { 
    end=$(date +%s) 
    let "end+=10" 
    while [[ ! -s "$1" ]] 
    do 
    now=$(date +%s) 
    if [[ $now -ge $end ]]; then 
     break 
    fi 
    sleep 1 
    done 
} 

# Determine the script mode 
action="run" 
if [[ "$MODE" == "auto" && -n "$init_script" ]] || [[ "$MODE" == "service" ]]; then 
    action="$1" 
    shift 
fi 

# Build the pid and log filenames 
if [[ "$identity" == "$init_script" ]] || [[ "$identity" == "$APP_NAME" ]]; then 
    PID_FOLDER="$PID_FOLDER/${identity}" 
fi 
pid_file="$PID_FOLDER/${identity}.pid" 
log_file="$LOG_FOLDER/$LOG_FILENAME" 

# Determine the user to run as if we are root 
# shellcheck disable=SC2012 
[[ $(id -u) == "0" ]] && run_user=$(ls -ld "$jarfile" | awk '{print $3}') 

# Find Java 
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then 
    javaexe="$JAVA_HOME/bin/java" 
elif type -p java > /dev/null 2>&1; then 
    javaexe=$(type -p java) 
elif [[ -x "/usr/bin/java" ]]; then 
    javaexe="/usr/bin/java" 
else 
    echo "Unable to find Java" 
    exit 1 
fi 

# Build actual command to execute 
command="$javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS $*" 

# Action functions 
start() { 
    if [[ -f "$pid_file" ]]; then 
    pid=$(cat "$pid_file") 
    isRunning "$pid" && { echoYellow "Already running [$pid]"; return 0; } 
    fi 
    do_start "[email protected]" 
} 

do_start() { 
    working_dir=$(dirname "$jarfile") 
    pushd "$working_dir" > /dev/null 
    if [[ -n "$run_user" ]]; then 
    mkdir "$PID_FOLDER" &> /dev/null 
    checkPermissions || return $? 
    chown "$run_user" "$PID_FOLDER" 
    chown "$run_user" "$pid_file" 
    chown "$run_user" "$log_file" 
    if [ $USE_START_STOP_DAEMON = true ] && type start-stop-daemon > /dev/null 2>&1; then 
     arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS "[email protected]") 
     start-stop-daemon --start --quiet \ 
     --chuid "$run_user" \ 
     --name "$identity" \ 
     --make-pidfile --pidfile "$pid_file" \ 
     --background --no-close \ 
     --startas "$javaexe" \ 
     --chdir "$working_dir" \ 
     -- "${arguments[@]}" \ 
     >> "$log_file" 2>&1 
     await_file "$pid_file" 
    else 
     su -s /bin/sh -c "$command >> \"$log_file\" 2>&1 & echo \$!" "$run_user" > "$pid_file" 
    fi 
    pid=$(cat "$pid_file") 
    else 
    checkPermissions || return $? 
    $command >> "$log_file" 2>&1 & 
    pid=$! 
    disown $pid 
    echo "$pid" > "$pid_file" 
    fi 
    [[ -z $pid ]] && { echoRed "Failed to start"; return 1; } 
    echoGreen "Started [$pid]" 
} 

stop() { 
    [[ -f $pid_file ]] || { echoYellow "Not running (pidfile not found)"; return 0; } 
    pid=$(cat "$pid_file") 
    isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; } 
    do_stop "$pid" "$pid_file" 
} 

do_stop() { 
    kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; } 
    for i in $(seq 1 60); do 
    isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; } 
    [[ $i -eq 30 ]] && kill "$1" &> /dev/null 
    sleep 1 
    done 
    echoRed "Unable to kill process $1"; 
    return 1; 
} 

restart() { 
    stop && start 
} 

force_reload() { 
    [[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; return 7; } 
    pid=$(cat "$pid_file") 
    rm -f "$pid_file" 
    isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 7; } 
    do_stop "$pid" "$pid_file" 
    do_start 
} 

status() { 
    [[ -f "$pid_file" ]] || { echoRed "Not running"; return 3; } 
    pid=$(cat "$pid_file") 
    isRunning "$pid" || { echoRed "Not running (process ${pid} not found)"; return 1; } 
    echoGreen "Running [$pid]" 
    return 0 
} 

run() { 
    pushd "$(dirname "$jarfile")" > /dev/null 
    $command 
    result=$? 
    popd > /dev/null 
    return "$result" 
} 

# Call the appropriate action function 
case "$action" in 
start) 
    start "[email protected]"; exit $?;; 
stop) 
    stop "[email protected]"; exit $?;; 
restart) 
    restart "[email protected]"; exit $?;; 
force-reload) 
    force_reload "[email protected]"; exit $?;; 
status) 
    status "[email protected]"; exit $?;; 
run) 
    run "[email protected]"; exit $?;; 
*) 
    echo "Usage: $0 {start|stop|restart|force-reload|status|run}"; exit 1; 
esac 

exit 0 
+0

Ce script, lancé par systemd, ne crée * pas * un fichier pid: systemd l'appelle avec l'argument "run". –