Is the following script right??? It was doing OK until yesterday!
I've written the following script to help a cron job running every minute and calling some other script which may cause a command to be executed more than once, the expected behaviour is to have 4 process of the job, anything less or more should be causing a restart, so please let me know if the following script is right or not?
#! /bin/bash
case "$(pidof command | wc -w)" in
0) echo "Restarting command: $(date)" >> log
/usr/bin/sh x.sh
;;
4) # all ok
echo "All OK" >> log
;;
*) for pid in $(ps -ef | grep "x" | grep "y" | awk '{print $2}'); do kill -9 $pid; done
echo "Removed PIDs and RESTARTING FFMPEG: $(date)" >> /root/afarinEidSTs/log
/usr/bin/sh x.sh
;;
esac
It will not kill proccesses even if they get more than 4, why?
In log file, I will just see the result of "Restarting command: $(date)"
.
It is so weird, because pidof ffmpeg | wc -w
will display 23, but the script is not killing all and restarting them again!
I have also tried using ([0-3])
instead of 0)
as @jordanm said, but didn't help.
[0-3]
. – jordanm Apr 3 '17 at 6:18[0-3]
be the same as*)
including1-2-3
since not mentioned in first conditions? – Parsa Samet Apr 3 '17 at 6:20pidof command
, when the command you're actually running is/usr/bin/sh x.sh
.... won't the number of PIDs be always 0? – muru Apr 3 '17 at 7:25x.sh
is callingcommand
inside itself, sopidof command
will show the number ofcommand
notx.sh
. – Parsa Samet Apr 3 '17 at 7:27command
a script or a linux built in or ac/java
binary? – ss_iwe Apr 3 '17 at 7:30