[SOLVED] I want check every minute with ping to u/mount folders on my system
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
yes, shellcheck reports this posix syntax issue (In POSIX sh, [[ ]] is undefined.) and other problems too.
Actually ( ) means a new process (subshell) and your while loop is running inside it. kill -s kill will terminate $pid, but not the subshell (as far as I see). By the way, would be better to use sigterm instead of sigkill. Anyway, you need to optimize this script by using functions instead of repeating the same line(s) again and again. Also would be better to format it (use for example shfmt) to make it more readable.
Thanks for remember me, the shellsheck site exist XD, i ever forget
well right now the code is this :
Code:
#!/bin/sh
PID_FILE="/run/rc_cifshare.pid"
unset cifs_stop
unset cifs_start
cifs_stop() {
IP="192.168.1.10" ; PC="Geremia"
Carpeta="Inukaze" ; Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1) ; if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then umount -f -l "/cifs/$PC/$Carpeta"; fi
Carpeta="Inukaze-en-Familia" ; Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1) ; if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then umount -f -l "/cifs/$PC/$Carpeta"; fi
Carpeta="Josmar" ; Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1) ; if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then umount -f -l "/cifs/$PC/$Carpeta"; fi
Carpeta="Temporal" ; Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1) ; if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then umount -f -l "/cifs/$PC/$Carpeta"; fi
IP="192.168.1.11" ; PC="Edna"
Carpeta="SSD-Gaby" ; Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1) ; if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then umount -f -l "/cifs/$PC/$Carpeta"; fi
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
kill -s KILL "$PID"
fi
rm -f "$PID_FILE"
else
echo "Service is not running"
fi
}
cifs_start() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "Service is already runing with PID $PID"
return
else
# Clean up invalid PID file
rm -f "$PID_FILE"
fi
fi
# Store the current process ID
echo $$ > "$PID_FILE"
(
while :
do
Entrada=$(echo "$1" | tr -s '[:upper:]' '[:lower:]')
if [ "$Entrada" = "stop" ]
then
cifs_stop
break
fi
Geremia=$(ping -4 -c 1 192.168.1.10 | grep -oP '\d+ received' | cut -d' ' -f1)
Edna=$(ping -4 -c 1 192.168.1.11 | grep -oP '\d+ received' | cut -d' ' -f1)
if [ "$Edna" = "1" ]; then
PC="Edna"
IP="192.168.1.11"
Carpeta="SSD-Gaby"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ -z "$Montura" ]; then
mkdir -p "/cifs/$PC/$Carpeta"
mount -t cifs "//$IP/$Carpeta" "/cifs/$PC/$Carpeta" -o "credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL"
fi
fi
if [ "$Edna" = "0" ]; then
PC="Edna"
IP="192.168.1.11"
Carpeta="SSD-Gaby"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then
umount -f -l "/cifs/$PC/$Carpeta"
fi
fi
if [ "$Geremia" = "1" ]; then
PC="Geremia"
IP="192.168.1.10"
Carpeta="Inukaze"
#Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1 | sed 's/-en-Familia//g')
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1 | tail -n 1)
if [ -z "$Montura" ]; then
mkdir -p "/cifs/$PC/$Carpeta"
mount -t cifs "//$IP/$Carpeta" "/cifs/$PC/$Carpeta" -o "vers=1.0,credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL"
fi
Carpeta="Inukaze-en-Familia"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ -z "$Montura" ]; then
mkdir -p "/cifs/$PC/$Carpeta"
mount -t cifs "//$IP/$Carpeta" "/cifs/$PC/$Carpeta" -o "vers=1.0,credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL"
fi
Carpeta="Josmar"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ -z "$Montura" ]; then
mkdir -p "/cifs/$PC/$Carpeta"
mount -t cifs "//$IP/$Carpeta" "/cifs/$PC/$Carpeta" -o "vers=1.0,credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL"
fi
Carpeta="Temporal"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ -z "$Montura" ]; then
mkdir -p "/cifs/$PC/$Carpeta"
mount -t cifs "//$IP/$Carpeta" "/cifs/$PC/$Carpeta" -o "vers=1.0,credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL"
fi
fi
if [ "$Geremia" = "0" ]; then
PC="Geremia"
IP="192.168.1.10"
Carpeta="Inukaze"
#Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1 | sed 's/-en-Familia//g')
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1 | tail -n 1)
if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then
umount -f -l "/cifs/$PC/$Carpeta"
fi
# Force Manual Mount :
# mount -t cifs //192.168.1.10/Inukaze /cifs/Geremia/Inukaze -o vers=1.0,credentials=/home/inukaze/.smb-credenciales,uid=1000,gid=1000,rw,nounix,iocharset=utf8,file_mode=0777,dir_mode=0777,domain=REDLOCAL
Carpeta="Inukaze-en-Familia"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then
umount -f -l "/cifs/$PC/$Carpeta"
fi
Carpeta="Josmar"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then
umount -f -l "/cifs/$PC/$Carpeta"
fi
Carpeta="Temporal"
Montura=$(findmnt | grep "//$IP/$Carpeta" | awk '{print $1,$2}' | cut -b 7- | cut -d " " -f 1)
if [ "$Montura" = "/cifs/$PC/$Carpeta" ]; then
umount -f -l "/cifs/$PC/$Carpeta"
fi
fi
sleep 60
done
)
}
case "$1" in
start)
cifs_start "$@"
;;
stop)
cifs_stop
;;
status)
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "Service is running with PID $PID"
else
echo "Service is not running, but PID file exists"
rm -f "$PID_FILE"
fi
else
echo "Service is not running"
fi
;;
*)
echo "Usage: $0 start|stop|status"
exit 1
;;
esac
exit 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.