简体   繁体   中英

Running a bash script with an infinite while loop at startup and in the background on a Raspberry Pi

I've made a small bash script that I want to run on startup of my Raspberry Pi. I would like this script to run constantly on the machine because I have a mounted USB drive that keeps disconnecting at random times. I use it for media storage and would like to find out at what times it's disconnecting and then remount the drive.

Here is the script

#!/bin/bash
while : 
do 
if mountpoint -q /media/Media
then
    continue
else
    echo $(date) >> log.txt
    sudo mount /dev/sda1 /media/Media
fi
sleep 1
done

Basically I would like this to run when the Raspberry Pi boots and have it continuously running in the background at all times.

Can anyone give me some help doing this? Thank you.

Using inotifywait

sudo inotifywait -d -e unmount /media/Media/some_file | while read unmounted; do 
    sudo mount /dev/sda1 /media/Media
done

Or maybe:

while true; do
    sudo inotifywait -e unmount /media/Media/some_file | read unmounted
    sudo mount /dev/sda1 /media/Media
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM