I'm writing a script for the server at work (running Ubuntu) which allows a person to plug in an SD card into an SD card reader and have all the photos automatically transfer to the server's shared drive. The card reader itself will remain connected to the server at all times. After following some tutorials, I decided to use dmesg to check when an SD card was inserted. The following was printed to dmesg when an SD card was inserted into the reader: Code: [100869.719118] sd 2:0:0:0: [sdc] 7729152 512-byte logical blocks: (3.95 GB/3.68 GiB) [100869.719850] sd 2:0:0:0: [sdc] Assuming drive cache: write through [100869.721729] sd 2:0:0:0: [sdc] Assuming drive cache: write through [100869.721742] sdc: sdc1 Based on the above, I decided to grep for "sdc: sdc1" to start the process. My script so far is below: Code: #!/bin/sh while true do until dmesg | grep "sdc: sdc1" | grep -v grep do sleep 5 done mount /dev/sdc1 /home/hdcadmin/sdcard cp -r /home/hdcadmin/sdcard/DCIM /home/samba/shares/shared/Photos chown -R nobody /home/samba/shares/shared/Photos chgrp -R nogroup /home/samba/shares/shared/Photos chmod -R 777 /home/samba/shares/shared/Photos sync umount /dev/sdc1 done # End Script What I need to do now is pause execution after unmounting the SD card until the SD card, and not run the code for copying until an SD card has been reinserted. Unfortunately, comparing dmesg before and after removing the SD card shows no difference, so there's no way for the script to know when the card's been removed. Can anyone help me further with this? Should I be using a utility other than dmesg for this?
The only thing I'd consider is waiting for user input at some point. if it's automated, how do you know when everything on the card's been copied across successfully without some kind of screen output? Could you add a line after sync (and forgive me here, I don't know what it would be in scripting) that says 'echo "Copy complete, press any key before removing SDcard"' and waiting on user input. That way the user knows it's copied, the system doesn't check the card again, and everyone's happy?
The server is meant to run headlessly, and the idea is that the user should just be able to plug the SD card in, come back in 2-3 mins (there won't be more than ~100MB of data transferred in any session), check to make sure "the light isn't flashing" and pull the card out.
You could have another wait loop where it just sits until the memory card is no longer present. Have you looked at one of the auto mounting daemons? I suspect they have various ways of hooking in to them so you can do what you want on mount and dismount.