I am trying to move various files present in a pendrive into their respective folders on PC. ie ,Music files into music folder,text files into Documents and so on.. I have written a script for that and it is working fine(all files are being moved into their respective folders) when I execute by typing ./cpphone.sh but when I use a udev rule the script not executing correctly (No files are being moved into their folders) however the echo's are being stored into the file testlog.txt. So the script is being executed but files are not being moved.Why is it so?. Script is owned by the root.
My Udev rule is ACTION=="add", RUN+="/lib/udev/cpphone.sh"
My script is
#!/bin/bash
echo Hello > /home/ArunReddy/testlog.txt
exec 1>/dev/null 2>&1
DIR=$(date +%d-%m-%y)
if [ ! -d /home/ArunReddy/Pictures/"$DIR" ];
then
cd /home/ArunReddy/Pictures/
mkdir $DIR
fi
if [ ! -d /home/ArunReddy/Pictures/"$DIR" ];
then
cd /home/ArunReddy/Documents/
mkdir $DIR
fi
if [ ! -d /home/ArunReddy/Videos/"$DIR" ];
then
cd /home/ArunReddy/Videos/
mkdir $DIR
fi
if [ ! -d /home/ArunReddy/Music/"$DIR" ];
then
cd /home/ArunReddy/Music/
mkdir $DIR
fi
cd /run/media/ArunReddy/121C-E137
mv *.png /home/ArunReddy/Pictures/$DIR/
echo pngfilecmd executed>/home/ArunReddy/testlog.txt
mv *.jpg /home/ArunReddy/Pictures/$DIR/
mv *.bmp /home/ArunReddy/Pictures/$DIR/
mv *.txt /home/ArunReddy/Documents/$DIR/
mv *.odt /home/ArunReddy/Documents/$DIR/
mv *.doc /home/ArunReddy/Documents/$DIR/
mv *.pptx /home/ArunReddy/Documents/$DIR/
mv *.pdf /home/ArunReddy/Documents/$DIR/
mv *.ppt /home/ArunReddy/Documents/$DIR/
mv *.docx /home/ArunReddy/Documents/$DIR/
mv *.avi /home/ArunReddy/Videos/$DIR/
mv *.flv /home/ArunReddy/Videos/$DIR/
mv *.MP4 /home/ArunReddy/Videos/$DIR/
mv *.mpeg /home/ArunReddy/Videos/$DIR/
mv *.MP3 /home/ArunReddy/Music/$DIR/
exit
2>> /home/ArunReddy/error
to each of themv commands. For example
mv *.jpg /home/ArunReddy/Pictures/$DIR/ 2>> /home/ArunReddy/error`. That will capture any error messages printed. Have a look at the resulting~ArunReddy/error
file and edit your question to include the errors. – terdon♦ Apr 21 at 12:37exec 1>/dev/null 2>&1
part, enclose your full script (well, starting from the line below the#!/bin/bash
) into{ ... } &> /home/ArunReddy/error
. This will log the stdout and stderr of your script into the file/home/ArunReddy/error
so that you can analyze the errors. – gniourf_gniourf Apr 22 at 9:25