Does an infinite loop shell in a cron make sense?

@dalto ok I just verified it the shell itself runs perfectly but not via systemd…

[falke@falke-macbookair72 Bureau]$ systemctl --user status sauvED.service
● sauvED.service - Mon script de sauvegarde avec boucle infinie
Loaded: loaded (/home/falke/.config/systemd/user/sauvED.service; enabled; preset: enabled)
Active: active (running) since Sat 2023-06-17 21:32:54 CEST; 1min 39s ago
Main PID: 16318 (sauvED_ctrl_pro)
Tasks: 2 (limit: 9380)
Memory: 728.0K
CPU: 160ms
CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/sauvED.service
├─16318 /bin/bash /home/falke/Bureau/sauvED_ctrl_proprio.sh
└─16431 sleep 1

juin 17 21:32:54 falke-macbookair72 systemd[640]: Started Mon script de sauvegarde avec boucle infinie.

ok , it’s definitively that , the loop shell itselt work.

when i launch it through systemd I see the shell running but it doesn’t do any backup at all anymore,

the systemd unit functions perfectly for exple if I kill its process it recreates a new process immediatly as configured in the service unit…

ps : I have given the rights 774 to the backup shell , nothing better.

I can’t help you with this in theory. I need to see the script.

@dalto I change it with full paths (I used ./SAUVEGARDE before) it seems that systemd must have real paths

but still stuck, maybe an environement problème rights on the directories … o recognizing the shell variables ?

#!/bin/bash

# Répertoire de sauvegarde
repertoire_sauvegarde="/home/falke/Bureau/SAUVEGARDE"

# Logins habilités
logins_habilites=(cifa camr )

# Fonction pour effectuer une sauvegarde
perform_backup() {
    fichier_edition="$1"
    timestamp=$(date +"%Y%m%d_%H%M%S")
    sauvegarde="$repertoire_sauvegarde/$(basename "$fichier_edition")_$timestamp"

    cp "$fichier_edition" "$sauvegarde"
    echo "Sauvegarde complète effectuée : $sauvegarde"

    # Supprimer la sauvegarde précédente
    previous_backup=$(ls -t "$repertoire_sauvegarde" | grep "$(basename "$fichier_edition")_" | tail -n +2)
    if [[ -n "$previous_backup" ]]; then
        rm "$repertoire_sauvegarde/$previous_backup"
        echo "Sauvegarde précédente supprimée : $previous_backup"
    fi
}

# Création du répertoire de sauvegarde s'il n'existe pas
mkdir -p "$repertoire_sauvegarde"

# Boucle infinie
while true; do
    for login in "${logins_habilites[@]}"; do
        fichier_edition="ED.${login: -3}"

        # Vérifier si le fichier d'édition existe et appartient à l'un des logins habilités
        if [ -f "$fichier_edition" ] && [[ " ${logins_habilites[@]} " =~ " $login " ]]; then
            echo "Le fichier $fichier_edition est bien présent et appartient à un login habilité"

            # Effectuer une sauvegarde pour cet utilisateur
            perform_backup "$fichier_edition"
        fi
    done

    # Temps d'attente entre chaque itération (ici 1 minute)
    sleep 1
done

At this point, fichier_edition contains something like ED.ifa.

You probably need to either cd into the appropriate directory or use an absolute path here.

Look variable sauvegarde use the variable repertoire sauvegarde then you have the full path

That one has the full path but you are effectively doing this:

cp ED.ifa /home/falke/Bureau/SAUVEGARDE/ED.ifa_20230617_153824

If you aren’t in the right place, that isn’t going to work.

That’s the right place for the backup

It is the source that is problematic, not the destination.

1 Like

Ah but cp /home/falke/Bureau/ED.ifa …

That’s what u mean

1 Like

I’m so blöd…

ok @dalto it works yet with cp fullpath/ED.ifa

but something is weird, when I temporarily stop the service , and had a line to ED.ifa

and restart the service, it happens … nothing…

I think I’m going crazy

in systemctl status it seems to work correctly, but in the backup directory, nothing.

[falke@falke-macbookair72 Bureau]$ systemctl --user status  sauvED.service
● sauvED.service - Mon script de sauvegarde avec boucle infinie
     Loaded: loaded (/home/falke/.config/systemd/user/sauvED.service; enabled; preset: enabled)
     Active: active (running) since Sat 2023-06-17 23:24:24 CEST; 9min ago
   Main PID: 67254 (sauvED_ctrl_pro)
      Tasks: 2 (limit: 9380)
     Memory: 888.0K
        CPU: 9.731s
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/sauvED.service
             ├─67254 /bin/bash /home/falke/Bureau/sauvED_ctrl_proprio.sh
             └─73075 sleep 1

juin 17 23:33:59 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde précédente supprimée : ED.ifa_20230617_233358
juin 17 23:34:00 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Le fichier ED.ifa est bien présent et appartient à un login habilité
juin 17 23:34:00 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde complète effectuée : /home/falke/Bureau/SAUVEGARDE/ED.ifa_20230617_233400
juin 17 23:34:00 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde précédente supprimée : ED.ifa_20230617_233359
juin 17 23:34:01 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Le fichier ED.ifa est bien présent et appartient à un login habilité
juin 17 23:34:01 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde complète effectuée : /home/falke/Bureau/SAUVEGARDE/ED.ifa_20230617_233401
juin 17 23:34:01 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde précédente supprimée : ED.ifa_20230617_233400
juin 17 23:34:02 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Le fichier ED.ifa est bien présent et appartient à un login habilité
juin 17 23:34:02 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde complète effectuée : /home/falke/Bureau/SAUVEGARDE/ED.ifa_20230617_233402
juin 17 23:34:02 falke-macbookair72 sauvED_ctrl_proprio.sh[67254]: Sauvegarde précédente supprimée : ED.ifa_20230617_233401

(I know the feeling when you can’t tell the forest from the trees… :wink: )