El archivo recién creado se convierte en 0 kb (los datos se sobrescriben a nada) al reiniciar en Linux

¡Tengo un problema extraño que me está volviendo loco! La tarea en cuestión es iniciar un conjunto de archivos durante el primer inicio de sesión del usuario "root" y otro conjunto de archivos durante el segundo inicio de sesión del mismo usuario. Decidí usar los archivos ".profile" y ".bashrc" y volver a cargar el archivo ".bashrc" hacia el final de la tarea durante el primer inicio de sesión.

Durante el primer inicio de sesión, creo una clave privada y una solicitud de firma de certificado, y llamo a una API para obtener el certificado. Guardo este certificado y clave privada en una ubicación de archivo y luego modifico el ".bashrc" para invocar el segundo conjunto de archivos, que hacen uso de este certificado y clave para autenticar una aplicación para ejecutar.

El problema es que el certificado y la clave se sobrescriben y se vuelven nulos al azar después del primer arranque. Adjunto el código a continuación para su revisión.

PRIMER CONJUNTO DE ARCHIVOS

Script ".profile"

# .bash_profile
umask 022
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

Script ".bashrc"

/myFolder/backgroundTask1.sh &
/myFolder/certificateGenerator.sh

script backgroundTask1.sh

pipe=/myFolder/testpipe
if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

while true
do
    ## Do some status LED blinking task here
done &

while true
do
    if read line < $pipe; then
        if [[ "$line" == 'success' ]]; then
           ## Kill the background LED blinking task created in the above while loop
           kill $!
           rm $pipe
           exit
        elif [[ "$line" == 'failed' ]]; then
           kill $!
           rm $pipe
           exit
        fi
    fi
done

script certificateGenerator.sh

TENGA EN CUENTA LAS ÚLTIMAS POCAS LÍNEAS DONDE MODIFIQUE EL ESCRITO DE BASHRC

Tenga en cuenta también los archivos /anotherFolder/myKey.key y /anotherFolder/myCert.crt

#!/bin/bash
## Named pipe location for communicating to backgroundTask1
pipe=/myFolder/testpipe
openssl req -new -newkey rsa:2048 -nodes -out certificateSigningRequest.csr -keyout /anotherFolder/myKey.key -subj "/C=myCountry/ST=myState/L=myCity/O=myCompany/OU=myOU/CN=myDevice"
cert_req=$(<$certificateSigningRequest.csr)

## Get AD token from Azure for talking to my custom API hosted on Azure
response=$(curl -o - -s -w "%{http_code}\n" -X POST \
    https://login.microsoftonline.com/myCompany.onmicrosoft.com/oauth2/token \
    -H 'content-type: application/x-www-form-urlencoded' \
    -d 'grant_type=client_credentials&resource=https%3A%2F%2Fmanagement.core.windows.net%2F&client_id=myClientID&client_secret=mySecret')
if [ $?==0 ]; then
    status=$(echo $response | tail -c 4)
    body=${response::-3}
    token=$(echo $body | jq -r '.access_token')
fi

## Send CSR to my custom API to get certificate
response=$(jq -n --arg csr "$cert_req" \
             '{
                 cert: {
                     csr: $csr
                 }
             }' |
         curl -o - -s -w "%{http_code}\n" -X POST \
             https://myCustomAPI.azurewebsites.net/api/v1/customEndpoint \
             -H "authorization: Bearer $token" \
             -H "content-type: application/json" \
             -d @-
)        

## Parse the response to find out if the request succeeded
if [ $?==0 ]; then
    destCertDir=/anotherFolder/myCert.crt
    status=$(echo $response | tail -c 4)
    body=${response::-3}
    cert=$(echo $body | jq -r '.certificate')
    if [ "$status" == "$http_success" ]; then
        echo "$cert" > "$destCertDir"
        ## Change .bashrc for next boot
        echo '/myFolder/backgroundTask2.sh &' > ~/.bashrc
        echo '/myFolder/applicationAuthenticator.sh' >> ~/.bashrc
        echo "success" > $pipe
        exit
    fi
fi

SEGUNDO CONJUNTO DE ARCHIVOS

Script ".profile"

# .bash_profile
umask 022
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

Script ".bashrc"

/myFolder/backgroundTask2.sh &
/myFolder/applicationAuthenticator.sh

Script backgroundTask2.sh

pipe=/myFolder/testpipe2
if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

while true
do
    ## Do some status LED blinking task here
done &

while true
do
    if read line < $pipe; then
        if [[ "$line" == 'success' ]]; then
           ## Kill the background LED blinking task created in the above while loop
           kill $!
           rm $pipe
           exit
        elif [[ "$line" == 'failed' ]]; then
           kill $!
           rm $pipe
           exit
        fi
    fi
done

script applicationAuthenticator.sh

TENGA EN CUENTA CÓMO MODIFICO BASHRC PARA INICIAR NORMAL DESDE EL PRÓXIMO REINICIAR HACIA EL FINAL DE ESTE ESCRITO

#!/bin/bash
## Named pipe location for communicating to backgroundTask2
pipe=/myFolder/testpipe2

response=$(curl https://myProduct/myCustomAPI.com \
           --cert /anotherFoler/myCert.crt --key /anotherFolder/myKey.key \
           -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
           -d 'data=xxx')
if [[ $response == 204 ]; then
    echo '' > ~/.bashrc
    echo "success" > $pipe
    exit
else
    echo "failed" > $pipe
    exit
fi

Problema Aunque el primer conjunto de archivos crea la clave y el certificado, se sobrescriben en NULL después del primer reinicio.

Para asegurarme de que existen antes de reiniciar, voy a la ubicación "/ anotherFolder" y verifico los archivos físicamente. Tienen la clave completa y el certificado antes de reiniciar. Cuando reinicio y veo que la secuencia de comandos falla, los mismos archivos de clave y certificado (que tenían datos reales antes del reinicio) ahora tienen valores NULL.

Respuestas a la pregunta(0)

Su respuesta a la pregunta