a question for admin sys…
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…
Thanks for your advice.
Can you explain what you are doing in your “infinite loop shell”? That sounds like fairly bad practice to begin with.
If you want to take some action when a file changes you can use a systemd path unit.
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.
let consider I have no extented admin (root) rights…
I’m aware that running a backup shell in a loop (and maybe forgetting about it? is that what you’re thinking? ) isn’t great, but I’ll assume the risk.
You don’t need root rights for any of the things I mentioned. systemd units can be run as a normal user with
Also, if you really don’t want to use systemd units you could still use
inotify or something similar instead of running a continuous loop.
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:
Description=Monitor the .bashrc for changes
Then you create a matching service unit that runs the command:
Description=Makes a backup of .bashrc when it changes
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.
I understand the principle,
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
-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
[falke@falke-macbookair72 user]$ cat sauvED.service
Description=Mon script de sauvegarde avec boucle infinie
the daemond-service has been relauched beforehand
→ I’m going to cut my hedge, see you later.
Like I said above, you don’t need sudo rights
systemctl --user enable --now sauvED.service
It is not found because you didn’t pass
ok @dalto, this time I’ll go a little further:
[falke@falke-macbookair72 user]$ sudo systemctl --user enable --now sauvED.service
Failed to connect to bus: No medium found
you probably have the solution…
if not you cut more need in my script of infinite loop? Are the options in the service file enough?
this part I mean ?
You shouldn’t use
what an idiot
ps aux | sauvED.service shows sauvED.service
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
This would make more sense I think. Disable the service. Modify it to be a oneshot and then use a timer instead.
i did’nt think so far
but well it doesn’t run , service not present …
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
email@example.com 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.
falke@falke-macbookair72 SAUVEGARDE]$ ps aux | grep sauvED.service
falke 8720 0.0 0.0 6604 2688 pts/1 S+ 20:12 0:00 grep sauvED.service
no , i don’t see it either with ps …
Two things to consider:
- You should use
--user with your systemctl command
- You are letting it run and then constantly restarting it so it may not be running when you check
Take a look at it with
systemctl --user status sauvED.service to see what is happening
i success to run it not using option start --now…
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.
If you want it to repeatedly, use a timer.
ok i will try also…
removing the file + recreating it does’nt recreate a new incremental back-up it may be she shell it
may be also systemd
what I really don’t understand , why people keep using cron jobs, if they aren’t sure for the system ?
So far I understand systemd verify the consistency of the running task ?
Can you share the script you are running?
ok I must anonymize it beforehand