programming a shell that does an infinite loop in nohup in a cron does it make sense ?
That’s the question I’m asking myself. Here’s my problem: I’ve designed an infinite loop shell. It consists in sniffing the evolution of the contents of one (or more) user files, which are constantly changing. This file is regularly deleted by a program, but I want to keep track of its latest version. My shell works. As I don’t want it to use my console, I launch it in nohup.
Another aspect of the question is that this looping shell should not run h24… On the one hand, I don’t want to tie up server resources all the time, and on the other, there’s a risk that the process will be killed during a nohup maintenance operation. However, I want to be sure that it’s running the next day during office hours, and that I don’t forget to restart it, either because it’s been interrupted and/or I’ve forgotten to restart it (this is an important safeguard).
so I thought I’d run it in nohup and in a cron. But I wonder if an infinite loop and a task scheduler like cron aren’t antinomymous? On the other hand, I’m also wondering: if the shell is launched at regular intervals, I’ll have several instances of the same task doing the same thing. I hope this won’t mess up my backup…
That being said, it you are fixed on using your “infinite loop shell” I would think a systemd service would be a better way to manage it than the cron.
thank you for these expert answers. Could you explain a little more how systemd would behave in relation to my shell (or maybe you don’t need a shell :-)?
and inotify? It all sounds good to me, but until recently our server ran with system V init :-). We migrated to a national platform, and since then I’ve been seeing systemd processes. You add to that a certain conservatism. Already, when I make a shell, everyone shudders, preferring for example to launch cp 5 times by hand than to go through the shell mill. So telling them about my systemd unit… I’m going to give it a try at home, as long as you tell me a little more about it
I don’t fully understand what your shell does so I can’t give you a super specific answer. However, I can explain how a path unit works.
A systemd path unit watches for changes in a file or directory. When a change occurs, it runs a command via a service unit. You don’t need a looping script, it only runs when the file changes.
For example, let’s say you want to save a copy of your .bashrc whenever it changes. You could create a path unit that looks like this:
[Unit]
Description=Monitor the .bashrc for changes
[Path]
PathChanged=/home/falke/.bashrc
[Install]
WantedBy=default.target
Then you create a matching service unit that runs the command:
[Unit]
Description=Makes a backup of .bashrc when it changes
[Service]
Type=oneshot
ExecStart=/usr/bin/rsync -avh --backup /home/falke/.bashrc /home/falke/.bashrc-old
Of course, you don’t have to use rsync here, that is just an example. You could run a different command or call a script.
two clarifications: I don’t know in advance the file to be followed, let’s say the file is fich.userXX
for the initials of the user’s name, from the moment it’s created I save it (I save its state incrementally, e.g. adding a line) and I have a single backup with the complete state.
When it’s deleted, the user generates another one, a new process, a new incremental backup (YYYYMMDD-HHMMSS) just until it disappears and so on…
Another big question: units are launched with sudo, but at work I don’t have sudo rights… I think that’s the end of it, unfortunately. But at home no better lol
[falke@falke-macbookair72 user]$ ls -rtla
total 20
-rw-r--r-- 1 falke falke 195 24 févr. 18:15 eos-update-notifier.timer.bak
-rw-r--r-- 1 falke falke 188 24 févr. 18:15 eos-update-notifier.service.bak
drwxr-xr-x 1 falke falke 8 28 avril 23:19 ..
-rw-r--r-- 1 falke falke 188 29 avril 12:38 eos-update-notifier.service
-rw-r--r-- 1 falke falke 212 24 mai 11:36 eos-update-notifier.timer
drwxr-xr-x 1 falke falke 50 24 mai 11:36 timers.target.wants
drwxr-xr-x 1 falke falke 290 17 juin 17:09 .
-rw-r--r-- 1 falke falke 217 17 juin 17:17 sauvED.service
[falke@falke-macbookair72 user]$ sudo systemctl enable sauvED.service
Failed to enable unit: Unit file sauvED.service does not exist.
[falke@falke-macbookair72 user]$ pwd
/home/falke/.config/systemd/user
[falke@falke-macbookair72 user]$ cat sauvED.service
[Unit]
Description=Mon script de sauvegarde avec boucle infinie
# After=network.target
[Service]
ExecStart=/home/falke/Bureau/sauvED_ctrl_proprio.sh
Restart=always
RestartSec=1
[Install]
WantedBy=default.target
for the other part of my question, with this conf of service no need to create a loop in the shell ? The shell restart every second (that is what I need)
I guess maybe I can link it with a timer of systemd to make it run in certain periods of time
falke@falke-macbookair72 SAUVEGARDE]$ systemctl list-units --type=service --state=running
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
colord.service loaded active running Manage, Install and Generate Color Profiles
cups-browsed.service loaded active running Make remote CUPS printers available locally
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
lightdm.service loaded active running Light Display Manager
NetworkManager.service loaded active running Network Manager
polkit.service loaded active running Authorization Manager
power-profiles-daemon.service loaded active running Power Profiles daemon
rtkit-daemon.service loaded active running RealtimeKit Scheduling Policy Service
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running User Login Management
systemd-timesyncd.service loaded active running Network Time Synchronization
systemd-udevd.service loaded active running Rule-based Manager for Device Events and Files
udisks2.service loaded active running Disk Manager
upower.service loaded active running Daemon for power management
user@1000.service loaded active running User Manager for UID 1000
wpa_supplicant.service loaded active running WPA supplicant
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
19 loaded units listed.
it work but. If i kill the shell process itself the service doesn’t restart the shell , i have to manually restart the service… It’s not really my idea.