Monitor a process [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
Is there a way I a process can be monitor and if it were to die, can a script be called which in turn will bring the process back up ?

#!/bin/bash
while true
do
if pgrep myprocess >/dev/null ;then
echo "up"
else
echo "down"
/path/to/myprocess &
fi
sleep 60
done
or you could remove the while loop and sleep and put the script in a cron job set to run every minute

monit.
http://mmonit.com/monit/

I wrote one of these a while ago called relight. There also exist more robust solutions, but this one is simple and works for me.

The easiest thing to do is have the initial parent do the monitoring. EG,
#!/bin/sh
while true; do
cmd
# When you get here the process has died. start
# the loop again and restart it
done
This script is liable to be killed so you might want
to trap signals, but the same will be true of any
monitor that you might write. You will probably
also want to insert a delay if cmd is terminating
immediately, or add some logging (call logger
after you call cmd). There's no need to get fancy.

Using a command that you specify in a simple configuration file, Supervisor can start, monitor, and restart a process that unexpectedly dies.
Consider the following Supervisor configuration file fragment in /etc/supervisor/conf.d/forever.conf which displays the date and time every second:
[program:forever]
command=/bin/bash -c 'while true; do echo "Current time is `date`"; sleep 1; done;'
Program forever starts with PID 15474:
derek#derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever
forever RUNNING pid 15474, uptime 0:00:17
derek#derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever
Current time is Fri Jul 7 17:11:10 EDT 2017
Current time is Fri Jul 7 17:11:11 EDT 2017
Current time is Fri Jul 7 17:11:12 EDT 2017
Current time is Fri Jul 7 17:11:13 EDT 2017
Current time is Fri Jul 7 17:11:14 EDT 2017
Current time is Fri Jul 7 17:11:15 EDT 2017
Current time is Fri Jul 7 17:11:16 EDT 2017
Current time is Fri Jul 7 17:11:17 EDT 2017
Current time is Fri Jul 7 17:11:18 EDT 2017
Current time is Fri Jul 7 17:11:19 EDT 2017
Current time is Fri Jul 7 17:11:20 EDT 2017
Current time is Fri Jul 7 17:11:21 EDT 2017
Current time is Fri Jul 7 17:11:22 EDT 2017
Current time is Fri Jul 7 17:11:23 EDT 2017
Current time is Fri Jul 7 17:11:24 EDT 2017
Current time is Fri Jul 7 17:11:25 EDT 2017
Kill the forever process and Supervisor restarts it automatically with new process ID 15760:
derek#derek-lubuntu:~/Projects/fire$ sudo kill 15474
derek#derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever
forever RUNNING pid 15760, uptime 0:00:02
derek#derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever
Current time is Fri Jul 7 17:11:21 EDT 2017
Current time is Fri Jul 7 17:11:22 EDT 2017
Current time is Fri Jul 7 17:11:23 EDT 2017
Current time is Fri Jul 7 17:11:24 EDT 2017
Current time is Fri Jul 7 17:11:25 EDT 2017
Current time is Fri Jul 7 17:11:26 EDT 2017
Current time is Fri Jul 7 17:11:27 EDT 2017
Current time is Fri Jul 7 17:11:28 EDT 2017
Current time is Fri Jul 7 17:11:29 EDT 2017
Current time is Fri Jul 7 17:11:30 EDT 2017
Current time is Fri Jul 7 17:11:31 EDT 2017
Current time is Fri Jul 7 17:11:32 EDT 2017
Current time is Fri Jul 7 17:11:33 EDT 2017
Current time is Fri Jul 7 17:11:34 EDT 2017
Current time is Fri Jul 7 17:11:35 EDT 2017
Current time is Fri Jul 7 17:11:36 EDT 2017

If you are using SysV system (not Upstart), you can put the process do respawn at inittab.
Just edit your /etc/inittab file and add a line like this:
proc:12345:respawn:/path/to/process

There are number of ways of getting the task done:
As suggested by others - Run a script to check if process is running, restart the process if not running. To check you if process is running or not you can use pgrep <process name> | wc -l
Use watch command to run a script after some interval to check if process is running, if not then restart the process
Create a parent process, that will always look for the child process, if child process crashes or stops, parent will be notified, then restarts new process.

systemd is a sophisticated process manager available on most major Linux distributions.

Related

How to find which process periodicitly write to a file?

There is a file in /tmp/echo_test.txt, and some process periodicitly write timestamp to this file one minute.
I checked process list and crontab, but found nothing. Is there a way to find the process quickily?
Thanks you.
My OS is Centos 6, the text like below:
Mon Oct 19 10:47:01 CST 2020
Mon Oct 19 10:48:01 CST 2020
Mon Oct 19 10:49:01 CST 2020
Mon Oct 19 10:50:01 CST 2020
Mon Oct 19 10:51:01 CST 2020
Mon Oct 19 10:52:01 CST 2020
If it's a single, continuous process, lsof or fuser should help.
Otherwise, it's probably a cron job or at job, or someone put together a while loop with a sleep 60 in it that's fork+exec'ing.

How can I customise an ISO image of Webconverger with a Linux shell script?

I want to write a simple shell script that downloads the latest ISO image of Webconverger and changes the boot parameters of grub in /boot/live.cfg.
So this is what I have done so far:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
chmod -R 777 devfolder/
rm devfolder/boot/live.cfg
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://www.google.de/" \
> devfolder/boot/live.cfg
# 4. Create a new ISO image
mkisofs -o new.iso -l devfolder
rm -r devfolder/
The generated ISO does not boot.
I asume the reasons for the problem are, that I had to run the whole script with root privileges and that I need a more complex mkisofs command.
Does anybody know how to generate a proper ISO image for Webconverger with a shell script? Or is there an alternative method for the whole process? Something like ISO Master but for the command line?
Update
I have replaced the code snippets with the actual shell script. So it is easier to test. And here is the command line output (without the wget part):
mount: /dev/loop0 is write-protected, mounting read-only
sending incremental file list
./
boot/
boot/boot.bin
boot/boot.cat
boot/live.cfg
boot/syslinux/
boot/syslinux/ldlinux.c32
boot/syslinux/libcom32.c32
boot/syslinux/libutil.c32
boot/syslinux/splash.png
boot/syslinux/syslinux.cfg
boot/syslinux/vesainfo.c32
boot/syslinux/vesamenu.c32
live/
live/initrd.img
live/vmlinuz
live/filesystem.git/
live/filesystem.git/HEAD
live/filesystem.git/config
live/filesystem.git/description
live/filesystem.git/packed-refs
live/filesystem.git/shallow
live/filesystem.git/branches/
live/filesystem.git/hooks/
live/filesystem.git/hooks/applypatch-msg.sample
live/filesystem.git/hooks/commit-msg.sample
live/filesystem.git/hooks/post-update.sample
live/filesystem.git/hooks/pre-applypatch.sample
live/filesystem.git/hooks/pre-commit.sample
live/filesystem.git/hooks/pre-push.sample
live/filesystem.git/hooks/pre-rebase.sample
live/filesystem.git/hooks/pre-receive.sample
live/filesystem.git/hooks/prepare-commit-msg.sample
live/filesystem.git/hooks/update.sample
live/filesystem.git/info/
live/filesystem.git/info/exclude
live/filesystem.git/objects/
live/filesystem.git/objects/info/
live/filesystem.git/objects/pack/
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.idx
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.pack
live/filesystem.git/refs/
live/filesystem.git/refs/heads/
live/filesystem.git/refs/heads/master
live/filesystem.git/refs/remotes/
live/filesystem.git/refs/remotes/origin/
live/filesystem.git/refs/remotes/origin/HEAD
live/filesystem.git/refs/tags/
live/filesystem.git/refs/tags/initial-revision
sent 598,528,659 bytes received 726 bytes 239,411,754.00 bytes/sec
total size is 598,380,033 speedup is 1.00
I: -input-charset not specified, using utf-8 (detected in locale settings)
1.71% done, estimate finish Tue Jul 10 20:12:54 2018
3.42% done, estimate finish Tue Jul 10 20:12:54 2018
5.13% done, estimate finish Tue Jul 10 20:13:13 2018
6.84% done, estimate finish Tue Jul 10 20:13:08 2018
8.55% done, estimate finish Tue Jul 10 20:13:05 2018
10.26% done, estimate finish Tue Jul 10 20:13:03 2018
11.97% done, estimate finish Tue Jul 10 20:13:02 2018
13.68% done, estimate finish Tue Jul 10 20:13:01 2018
15.39% done, estimate finish Tue Jul 10 20:13:00 2018
17.10% done, estimate finish Tue Jul 10 20:12:59 2018
18.81% done, estimate finish Tue Jul 10 20:12:59 2018
20.52% done, estimate finish Tue Jul 10 20:12:58 2018
22.23% done, estimate finish Tue Jul 10 20:12:58 2018
23.94% done, estimate finish Tue Jul 10 20:12:58 2018
25.65% done, estimate finish Tue Jul 10 20:12:57 2018
27.36% done, estimate finish Tue Jul 10 20:12:57 2018
29.07% done, estimate finish Tue Jul 10 20:12:57 2018
30.78% done, estimate finish Tue Jul 10 20:12:57 2018
32.49% done, estimate finish Tue Jul 10 20:12:57 2018
34.20% done, estimate finish Tue Jul 10 20:12:56 2018
35.91% done, estimate finish Tue Jul 10 20:12:56 2018
37.62% done, estimate finish Tue Jul 10 20:12:56 2018
39.33% done, estimate finish Tue Jul 10 20:12:56 2018
41.04% done, estimate finish Tue Jul 10 20:12:56 2018
42.75% done, estimate finish Tue Jul 10 20:12:56 2018
44.46% done, estimate finish Tue Jul 10 20:12:56 2018
46.17% done, estimate finish Tue Jul 10 20:12:56 2018
47.88% done, estimate finish Tue Jul 10 20:12:56 2018
49.59% done, estimate finish Tue Jul 10 20:12:56 2018
51.30% done, estimate finish Tue Jul 10 20:12:55 2018
53.02% done, estimate finish Tue Jul 10 20:12:55 2018
54.72% done, estimate finish Tue Jul 10 20:12:55 2018
56.44% done, estimate finish Tue Jul 10 20:12:55 2018
58.14% done, estimate finish Tue Jul 10 20:12:55 2018
59.86% done, estimate finish Tue Jul 10 20:12:55 2018
61.56% done, estimate finish Tue Jul 10 20:12:55 2018
63.28% done, estimate finish Tue Jul 10 20:12:55 2018
64.98% done, estimate finish Tue Jul 10 20:12:55 2018
66.70% done, estimate finish Tue Jul 10 20:12:55 2018
68.40% done, estimate finish Tue Jul 10 20:12:55 2018
70.12% done, estimate finish Tue Jul 10 20:12:55 2018
71.82% done, estimate finish Tue Jul 10 20:12:55 2018
73.54% done, estimate finish Tue Jul 10 20:12:55 2018
75.24% done, estimate finish Tue Jul 10 20:12:55 2018
76.96% done, estimate finish Tue Jul 10 20:12:55 2018
78.66% done, estimate finish Tue Jul 10 20:12:55 2018
80.38% done, estimate finish Tue Jul 10 20:12:55 2018
82.08% done, estimate finish Tue Jul 10 20:12:55 2018
83.80% done, estimate finish Tue Jul 10 20:12:55 2018
85.50% done, estimate finish Tue Jul 10 20:12:55 2018
87.22% done, estimate finish Tue Jul 10 20:12:55 2018
88.92% done, estimate finish Tue Jul 10 20:12:55 2018
90.64% done, estimate finish Tue Jul 10 20:12:55 2018
92.34% done, estimate finish Tue Jul 10 20:12:55 2018
94.06% done, estimate finish Tue Jul 10 20:12:55 2018
95.76% done, estimate finish Tue Jul 10 20:12:55 2018
97.48% done, estimate finish Tue Jul 10 20:12:55 2018
99.18% done, estimate finish Tue Jul 10 20:12:55 2018
Total translation table size: 0
Total rockridge attributes bytes: 0
Total directory bytes: 30720
Path table size(bytes): 222
Max brk space used 2b000
292387 extents written (571 MB)
And this what I get in VirtualBox:
Click here!
So the full answer to the question is:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://duckduckgo.com nobrand" \
| sudo tee devfolder/boot/live.cfg
# 4. Create a new ISO image
sudo mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
sudo isohybrid new.iso
sudo rm -r devfolder/
For some reason dd did not work for Webconverger images. Even the with the untouched latest.iso it was not possible to create a bootable USB flash drive. So I used Win32DiskImager.
What you want to do is to generate an El Torito bootable CD with an MBR in its first bytes.
mkisofs(8) needs some special options to create an "El Torito" bootable CD:
mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
Explaination:
-r -J: Enable Joliet and Rock Ridge meta data.
-l: Allow long filenames.
-V: Set volume ID. Optional.
-b: Specify the "El Torito" boot image.
-c: Specify the "El Torito" boot catalog.
-no-emul-boot: Specify that this is an "El Torito" bootable CD.
-boot-load-size: number of 512 bytes sector to load (4 is what usually works)
-boot-info-table: modify the original boot image appending the Boot Info Table
Then, if you want to be able to just dd the file on a USB drive you will need to write an MBR onto the ISO:
isohybrid new.iso

Monitor the rate of log file writes matching a pattern

I have a timestamped log file that I am watching. Every so often the log writes a message, "OK"
How could I capture the number of "OK"s written per second? E.g. output like
10.5 OK/sec
9 OK/sec
20 OK/sec
The log is pretty big, its not practical to do a grep on the entire file. I started with
tail -f my.log | grep OK
which is helpful but doesn't quite get me the rate.
Thanks!
First solution below is great (and other is good too), I altered it a bit and this is what I came up with
tail -f my.log | perl -lne 'if(/(\d+:\d+:\d+).*OK/) { print $1; }'| uniq -c
When your log is in form like:
Wed Jul 10 22:44:00 CEST 2013 OK
Wed Jul 10 22:44:00 CEST 2013 OK
Wed Jul 10 22:44:00 CEST 2013 OK
Wed Jul 10 22:44:00 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
Wed Jul 10 22:44:01 CEST 2013 OK
so, in form timestamp OK, you can try the simple:
tail -f logfile | uniq -c
and you will get the next result:
4 Wed Jul 10 22:44:00 CEST 2013 OK
8 Wed Jul 10 22:44:01 CEST 2013 OK
^
+---- number of OK in the same timestamp
Here's a quick script to do it:
#! /bin/bash
log="/var/log/messages"
seconds="1"
filter="OK"
while true; do
echo "$(timeout $seconds tail -n0 -f $log|grep $filter|wc -l) OK/sec"
done
There's a bit of a race condition where it might lose one or two lines during the loop, but it is unlikely. Also, not every Linux distro has timeout installed by default, so you might want to check.

Using genisoimage with less status messages

I have a script that does several things. One of which is to use genisoimage to create an iso for a bluray. It gives annoying status messages like this:
26.13% done, estimate finish Mon Dec 24 12:31:54 2012
26.17% done, estimate finish Mon Dec 24 12:31:51 2012
26.22% done, estimate finish Mon Dec 24 12:31:52 2012
26.26% done, estimate finish Mon Dec 24 12:31:53 2012
26.31% done, estimate finish Mon Dec 24 12:31:55 2012
26.35% done, estimate finish Mon Dec 24 12:31:52 2012
26.40% done, estimate finish Mon Dec 24 12:31:53 2012
26.45% done, estimate finish Mon Dec 24 12:31:54 2012
26.49% done, estimate finish Mon Dec 24 12:31:51 2012
26.54% done, estimate finish Mon Dec 24 12:31:52 2012
These messages do not help me. Is there an easy way to turn off these messages?
Did you try to search the manpage of genisoimage?
-quiet This makes genisoimage even less verbose. No progress output
will be provided.

Real-time display of `date` changes on Linux

On an embedded Linux device that does not present /dev/rtc*, how can I set off a console window writing the value of the Real-Time Clock to the console, on the tick, every time it changes?
Results would be like:
$ **someCmd**
Mon Mar 14 16:43:22 UTC 2011
Mon Mar 14 16:43:23 UTC 2011
Mon Mar 14 16:43:24 UTC 2011
Mon Mar 14 16:43:25 UTC 2011
Mon Mar 14 16:43:26 UTC 2011
etc.
The device is armv5tejl running BusyBox v1.13.3.
Use the watch commad, try this is:
watch -n 1 date
I don't know how much the BusyBox shell supports, but in sh you could do something like this:
{ while true ; do date ; sleep 0.1 ; done } | uniq

Resources