Shutdown and Restart Properly - Raspberry Pi
After using like 5 different Raspberry Pi's with multiple setups, OS, SD cards, and methods of restarting, I had found I was restarting and shutting down wrong and frequently destroying or losing files that were recently open or near to others in the data structure. I lost my entire first setup after 40 or so hours of learning and work due to using init 6 to restart the pi. You will find people that will tell you this is safe. They are correct-- most of the time. You can use the reboot command also but it does the same thing and is also unsafe.
My solution was to create a restart script that stops any services I can, then kills any running programs that could interfere with my file system if forced to shutdown too early, then waits 15 seconds and uses the shutdown command after the pause. I made this script reboot.sh to avoid data corruption on my pi (it happened 4 or 5 times!). Feel free to copy and modify to your taste.
echo "Beginning forced shutdown script" #unmount my network share (cifs) umount -f /jane #cron triggers scripts all the time on my setup, kill it! service cron stop ##service running my python buttons listen script service pybuttons stop killall python service mpd stop service ntp stop service alsa-utils stop service networking stop service ssh stop killall fbi killall samba killall mpd killall mpc killall vim killall vi killall omxplayer killall sftp-server killall python #unmount my network share (cifs) again just in case! umount -f /jane sleep 15 shutdown -h -r now